I have a device sends some data to COM port, byte stream is a human readable ASCII character stream. I tried to read the data stream from a port reader and I can read it easily like,
Param Flags Value Unit [min-max]
WBC E 0.00 10^9/l [4.00-10.00] LYM 0.00 10^9/l [1.30-4.00]
But when I'm trying to read from a java program as byte and converting to ASCII, still it shows as jargon. e.g. ���v�</p>
I changed array size and try various options to convert array to ASCII.
Row out is like
4C 4C 59 20 41 55 54 4F 4D 41 54 45 44 20 48 45 LLY AUTOMATED HE
Guidance to read n proper format is highly appreciated.
Code is
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
String line = "";
BufferedReader portReader;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "COM6";
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port: " + defaultPort);
portFound = true;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {
e.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
System.out.println("BI");
case SerialPortEvent.OE:
System.out.println("OE");
case SerialPortEvent.FE:
System.out.println("FE");
case SerialPortEvent.PE:
System.out.println("PE");
case SerialPortEvent.CD:
System.out.println("CD");
case SerialPortEvent.CTS:
System.out.println("CTS");
case SerialPortEvent.DSR:
System.out.println("DSR");
case SerialPortEvent.RI:
System.out.println("RI");
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("OUTPUT_BUFFER_EMPTY");
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("DATA_AVAILABLE");
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print("The Read Bytes from SerialPort are");
System.out.write(readBuffer);
System.out.println();
}
System.out.print(new String(readBuffer));
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}