I am trying to read serial data for my Java application, which I am developing on Linux (Ubuntu 12.10). This is my code:
try {
portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
SimpleRead reader = new SimpleRead();
} catch (Exception e) {
TimeStamp=new Date().toString();
System.out.println(TimeStamp+": ttyS0 "+portId);
System.out.println(TimeStamp+": msg1 - "+e);
}
SimpleRead constructor and other required code:
public SimpleRead() {
try {
TimeStamp=new Date().toString();
serialPort = (SerialPort) portId.open("SimpleRead", 2000);
System.out.println(TimeStamp + ": " + portId.getName() + " opened for scanner input");
} catch (PortInUseException e) {
System.out.println(e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
System.out.println(e);
}
System.out.println("Input Stream set");
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println(e);
}
System.out.println("Event listener added");
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setDTR(false);
serialPort.setRTS(false);
} catch (UnsupportedCommOperationException e) {
System.out.println(e);
}
System.out.println("Parameters written");
readThread = new Thread(this);
readThread.start();
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e);
}
}
@Override
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("Hello");
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
System.out.println("Hello");
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
if (numBytes>=16)
break;
}
//System.out.print(new String(readBuffer));
TimeStamp = new java.util.Date().toString();
System.out.println(TimeStamp + ": scanned input received:" + new String(readBuffer));
inputStream.close();
} catch (IOException e) {
System.out.println(e);
}
break;
}
}
For some reason, there is no serial data being read from the port, since the "Hello" in the body of the serialEvent() method is not printed in the output screen. Please do help me out here!
EDIT Code taken from: http://www.java-samples.com/showtutorial.php?tutorialid=11 And yes, the device was configured for the given parameters before attempting to read from it.
New Edit I am using Java SE 8 Development Kit for Linux x64 platform, and the Java Communications API taken from http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm. My IDE is Netbeans 8.1.
So it turns out that I have to use the 32-bit version of Java 1.8 for accessing serial ports. My IDE on Linux would not accept the 32-bit libraries on a 64-bit machine even when forced to (by changing the "jdk_home" path in netbeans.conf
file), hence I was facing problems accessing the serial port.
I switched my Operating System to Windows 7, though I'm still on a 64-bit system, since Netbeans IDE 8.1 on Windows can run in both 64-bit and 32-bit modes with the respective source libraries. So I installed JDK 1.8 (32-bit) for Windows and then ran my code in the IDE, after referring to Javax.comm API on 64-bit Windows for getting it to work. Now, data from the serial port is read properly.