5
votes

My Java program is failing to detect the serial port device connected to the computer.

I am using a Windows 7 (64-bit) computer ().

The serial port device is a GPS module which outputs text GPS strings at 37860 baud, connected to a Serial-USB converter plugged into the laptop USB port.

The Windows control panel/device manager shows this device as COM7.

The GPS application "MiniGPS" running on the computer is able to communicate in both directions with the GPS chip as COM7.

An application in Processing is able to detect that the computer has 2 serial ports (com3 and com7, com3 is the mouse).

I want to write an application in Java (or C++) to collect the data from the serial port, from the GPS.

I copied the following code (below) from somewhere, not sure where now, the same or similar code appears in a bunch of other questions here already.

When I run this, the portList enumeration comes back empty, it can't find any serial port at all.

I know only one program can see the serial port at a time, I shut down the device manager app, the GPS app and the Processing app before I tried this, still no serial port gets identified by the CommPortIdentifier object.

Any suggestions ?

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) 
{
    System.out.println("Hello World");
    portList = CommPortIdentifier.getPortIdentifiers();

    JOptionPane.showMessageDialog(null,"portlist :"+portList.hasMoreElements());
    int numelt=0 ;
    while (portList.hasMoreElements()) 
    {
        numelt++;
        portId = (CommPortIdentifier) portList.nextElement();
3
You must load the win32com.dll when you start your application. Did you do that??Alex Calugarescu

3 Answers

4
votes

I advise you to use the JSSC library.

It's efficient by experience and your code will be much briefer

0
votes

It's hard to say for sure, but your problem sounds like one of the following:

  • You have plugged the device in after the program is initialized.

    From my experience the COM ports are recognized when the program is initialized. My company has worked for months trying to find a way to listen for a device being plugged in, but it doesn't look possible with purely Java (which baffles me). Try plugging it in, then starting the program.

  • You are missing the driver files.

    You're using javax.comm, which requires the files javax.comm.properties and win32comm.dll (You may have to find a different driver on a 64-bit system). The properties file should be placed in the %JAVA_HOME%\lib\ directory, and the dll goes in %JAVA_HOME%\bin.

0
votes

using javax.comm with windows 7 has compatibility issues try to use RxTx API which suports on windows 7.