views:

989

answers:

3
+1  Q: 

rxtx com port

Hi, I am using rxtx api to read data from a GPS device over a com port. Right now I'm finding the correct com port by reading a config file and looking for the port listed. The problem that I'm having is that if the device is unplugged the com port could change then the user has to know to change the config file. I wrote an app similar to this in c# and was able to list the windows device name instead of the com port and I cycled through the com ports until the device name matched the name in the config file. Using that method nothing in the config file has to change even if the com port being used changes. Is there a way to do that with the rxtx api?

Thanks in advance!

A: 

CommPortIdentifier.getPortIdentifiers lists all ports in the system that are usable by the Javacomm API. Iterate through them to find the port your device is connected to.

Bombe
I've done that but it doesn't give me the device id of the device using the port, it just returns the ComPortIdentifier for each port. Using that you can get the port name (ie COM3) but not a device id so I would still need to know the port that I am looking for.
What exactly do you need that for? From your question it seems that you want to enumerate all available ports to find the one you’re looking for so that you do not need to change the configuration file. This should help you—unless you’ve asked the wrong question. :)
Bombe
With what you suggested I would still have to change the config file if the device gets a new com port after being unplugged and plugged back in (which happens frequently with my system). My code in C# looked for the device name on a com port (which doesn't change) so even if the com port changes the app can still find the right port. Hopefully this will make more sense.
I still don’t really understand your problem. Why does the name matter? You can enumerate all ports there are. The port you’re looking for is one of them! It will always be, its name is completely irrelevant. Also, you don’t need to store the name of the port in a config file when you’re iterating over all available ports anyhow.
Bombe
Serial devices do not have a standard way of returning a device ID. If you’re using a serial-to-USB converter you need to use a USB library for java to get the converter’s device ID. Otherwise I’m all out of ideas what it is you really want.
Bombe
A: 

If you want to get the name associated with the device on the COM port (particularly if a driver is installed to provide it), you'll have to do so with a smidge of the dreaded Java->Native Interface to talk to the Windows APIs that gather this information. C# is nice, in that this information is gathered and provided to you, but in Java you have to go this extra step.

Windows Function Discovery may prove useful. I'm not certain exactly what API provides this functionality.

Chris Kaminski
Thanks, I'll look into that.
A: 

If anyone is interested...

I created a windows service in C# that monitors a socket. If a client connects to that socket the service gathers port name, and device id that is on that port and sends the data in a string over the com port the client can then parse apart the string to get the data it needs.

In my case the string being passed is: "ACPI\PNP0501 *PNP0501 ,COM1 ,PCI\VEN_8086&DEV_29B7&SUBSYS_02111028&REV_02 PCI\VEN_8086&DEV_29B7&SUBSYS_02111028 PCI\VEN_8086&DEV_29B7&CC_070002 PCI\VEN_8086&DEV_29B7&CC_0700 ,COM3 ,USB\Vid_067b&Pid_2303&Rev_0400 USB\Vid_067b&Pid_2303 ,COM5"

When I parse it I can see that ACPI\PNP0501 *PNP0501 is the device id for COM 1, there are three device id's for COM3, and two device ids on COM5.

This may not be the best way to handle this but it is good enough for my needs and it saved me from JNI. :)