5

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();
1
  • You must load the win32com.dll when you start your application. Did you do that?? Commented Oct 13, 2012 at 4:39

3 Answers 3

4

I advise you to use the JSSC library.

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

The first suggestion doesn't help. The GPS device is plugged in and running all the time. After the Java program has failed to detect any serial ports, I can go back and run the GPS application and confirm that the serial port and input data are still working.
The second suggestion is much more likely. I have the DLL but I need to figure out which of the dozen JRE's on the computer is actually being used.
Second suggestion seems to have tackled the problem. The dll file needs to go in the JRE under the JDK directory. I can see the datastream now, although it is scrambled. I can work on that more.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.