0
votes

I'm developing an application that needs to be able to communicate with devices over Serial Comms on Windows OS. The application is written in Java. I am using the jSSC library. (Javax.comm has issues and RxTx has no Java 8 support apart from an unofficial update) The devices may connect directly to the PC using a Serial Port, or alternatively using a USB-to-Serial converter.

My Question: COM ports on windows are dynamic. This raises an issue for defining which comm port to connect to. Is there a way to automatically figure out which COM port to connect to?

So far the only solutions I have been able to come up with involves scanning all the com ports for active ports, saving this list and then asking the user to insert the device and performing another scan - the additional COM port will then be the one to connect to.

Alternatively, the device will respond to a specific 'identification' packet: I can open each COM port and send this packet and evaluate the response. This has the risk of the other devices being hijacked for a short period of time which is not ideal.

1
I've yet to see a good autodetection scheme. You never know how other devices are going to react as you have alluded to. If you are running solely on Windows I would suggest a helper library in .net that can capture the USB inserted event. If this is a classic DB9 then I have nothing for you. Check out my app called Porty that does the port name detection. You could probably adapt it to your needs. github.com/corytodd/portycory.todd
Thanks! I've taken a look at your app and it looks like it could be some help.Walter Aldum

1 Answers

0
votes

For detecting ports dynamically operating system specific functions needs to be called. SCM library provides API for detecting ports dynamically.

try {
    scm.registerUSBHotPlugEventListener(this, PRODUCT_VID, PRODUCT_PID, null);
    if(scm.isUSBDevConnected(PRODUCT_VID, PRODUCT_PID, null)) {
        USBhotplugEventQueue.offer(SerialComUSB.DEV_ADDED);
    }
} catch (Exception e) {
    e.printStackTrace();
}