How do I read text from a serial port?
Posted
by user2164
on Stack Overflow
See other posts from Stack Overflow
or by user2164
Published on 2009-01-27T17:38:58Z
Indexed on
2010/03/20
19:21 UTC
Read the original article
Hit count: 330
I am trying to read data off of a Windows serial port through Java. I have the javax.comm libraries and am able to get some data but not correct data. When I read the port into a byte array and convert it to text I get a series of characters but no real text string. I have tried to specify the byte array as being both "UTF-8" and "US-ASCII". Does anyone know how to get real text out of this?
Here is my code:
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.println("Reading from " + portId.getName() + ": ");
System.out.println("Read " + numBytes + " bytes");
}
System.out.println(new String(readBuffer));
System.out.println(new String(readBuffer, "UTF-8"));
System.out.println(new String(readBuffer, "US-ASCII"));
the output of the first three lines will not let me copy and paste (I assume because they are not normal characters). Here is the output of the Hex: 78786000e67e9e60061e8606781e66e0869e98e086f89898861878809e1e9880
I am reading from a Hollux GPS device which does output in string format. I know this for sure because I did it through C#.
The settings that I am using for communication which I know are right from the work in the C# app are: Baud Rate: 9600 Databits: 8 Stop bit: 1 parity: none
© Stack Overflow or respective owner