I have the following routine that is the entry point for accessing serial ports in the usb4java API running in windows. Any ideas what can be wrong.
import javax.comm.*
public class SimpleJSComRead
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
if (portList.nextElement()==null) System.out.println("Is Null");
}
}
Concurrently, this works using jssc. I'm able to read valid data through this interface.
import jssc.SerialPort;
import jssc.SerialPortException;
public class SimpleJSComRead {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM6");
try { serialPort.openPort();
} catch (SerialPortException e) {
e.printStackTrace();
}
if (serialPort.isOpened()) System.out.println("opened successfully");
try { serialPort.closePort();
} catch (SerialPortException e) {
e.printStackTrace();
}
}
}
I tracked down the fact that the javax.comm library is not fully implemented or supported. I find it strange that a basic function like reading from a serial port should be a difficult task for Java whereas hosting web applications is duplicated by various APIs. I expect serial communication is well supported in Python and C++ as these are more used by the scientific community.
The additional solution I found was to utilize RXTX API... one and two . The process involves downloading the zipped distribution and extracting the dlls and jars to some library site on your FileSystem. Then declare the dependency to the Jar file. For example in Gradle
compile files( 'C:/..path../lib/jars/RXTXcomm.jar')
.and setting the dll library path and loading the dll
Finally, the serial API is not in javax.comm package, but in the gnu.io package.