0

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.

  • 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. https://github.com/corytodd/porty – cory.todd Aug 28 '15 at 19:24
  • Thanks! I've taken a look at your app and it looks like it could be some help. – Walter Aldum Sep 03 '15 at 16:46

1 Answers1

0

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();
}
samuel05051980
  • 369
  • 2
  • 2
  • This will only work with USB devices I assume? I have managed to convince most parties involved with this project to move to USB or TCP/IP, but there are still some that are (and will likely for quite some time) insisting on using Serial (direct RS232) – Walter Aldum Mar 10 '16 at 17:23
  • You may use findDriverServingComPort method to check if the given COM port is permanent (direct RS232) or dynamic (USB, TCP/IP etc). For example for cp2102 on windows "slabser" will returned as driver name. This will be different for direct RS232 port. – samuel05051980 Mar 28 '16 at 07:08