Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing to COM1

 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used javax API to write a java program which takes input from a flat file, and prints it on COM1.
Now if i use Windows HyperTerm, everything is fine, but when i use a properitory receiver i get garbage , is there anything i need to be aware of while sending data over Serial port ?

Here is my main()

package esecurity.tools.SerialTester;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SerialTest {
public static void usage()
{
System.out.println("usage: SerialTester <filename>");
System.exit(1);
}

public static void main(String args[]) {

BufferedReader in = null;
CommPortIdentifier portId = null;

SerialPort sPort = null;
String fileName = null;
String str;
OutputStream os = null;

// Command Line Help
if ((args.length > 0)
&& (args[0].equals("-h")
|| args[0].equals("-help"))) {
usage();
}

// Command Line Arguments
if (args.length == 0) {
usage();
}
if (args.length > 0)
fileName = args[0];
//System.out.println(port);
if (args.length > 1) {
fileName = args[1];
}


try {
portId = CommPortIdentifier.getPortIdentifier("COM1");
sPort = (SerialPort) portId.open("SerialDemo", 30000);
sPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
os = sPort.getOutputStream();
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (PortInUseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}

try {
in = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while ((str = in.readLine()) != null) {
//char data[]=str.getBytes();
for (int i = 0;i< str.length();i++){
os.write((int)str.charAt(i));

}

//os.write(str+"\n");
//os.write(10);
//System.out.println(str.getBytes()[0]);

}
} catch (IOException e4) {
e4.printStackTrace();
}
sPort.close();
}

}
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...this doesn't seem like a "beginner" question. Perhaps you should try at least the intermediate forum, and probably the advanced one. There may even be other more specialized forums here that will be more appropriate for your question. If so, you will more likely find the answers there.

Layne
 
Kamal Ahmed
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I did post in Advaced Java , but no response
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic