Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using javax API to send file contents 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();
}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic