Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting serial COM port using Java

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();
like image 465
user1730748 Avatar asked Oct 06 '22 08:10

user1730748


1 Answers

I advise you to use the JSSC library.

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

like image 200
Moatez Bouhdid Avatar answered Oct 10 '22 04:10

Moatez Bouhdid