No COM ports identified with usb4java

448 views Asked by At

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();
      }
    }
}
1

There are 1 answers

0
J.E.Tkaczyk On

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

System.setProperty("java.library.path", "C:/..path../lib/dlls_x64")
System.loadLibrary("rxtxSerial")

Finally, the serial API is not in javax.comm package, but in the gnu.io package.

import gnu.io.CommPortIdentifier
import io.FileMgr

//this one is based on RXTX and dlls are in library (gnu.io)
fun main(args: Array<String>) {
  FileMgr.setDllLibraryPath("C:/..path../lib/dlls_x64")
  System.loadLibrary("rxtxSerial")

  val portList = CommPortIdentifier.getPortIdentifiers()

  while (portList.hasMoreElements()) {
    val x = portList.nextElement() as CommPortIdentifier
    println("Port Name = " + x.name + ", type= " + x.portType)

  }
  if (portList.nextElement() == null) println("Is Null")
}

fun setDllLibraryPath(resourceStr: String) {
  try {
    System.setProperty("java.library.path", resourceStr)
    //System.setProperty("java.library.path", "/lib/x64");//for example

    val fieldSysPath = 
ClassLoader::class.java.getDeclaredField("sys_paths")
    fieldSysPath.isAccessible = true
    fieldSysPath.set(null, null)//next time path is accessed, the new path 
will be imported
  } catch (ex: Exception) {
    ex.printStackTrace()
    throw RuntimeException(ex)
  }
}