Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Comm api 2.0 - CommPortIdentifier.getPortIdentifiers() returns empty enum


Hi,
I'm trying to open connetion with my GSM modem which it connected to my PC by RS-232 cable.
I'm trying to run my program on Windows XP.
But, when i'm running the following piece of code, the enumeration return empty.
How it can be solved?

Enumeration en = CommPortIdentifier.getPortIdentifiers();
System.out.println(en);

// Iterate through the ports.
while (en.hasMoreElements()) {
    portId = (CommPortIdentifier) en.nextElement();
    if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
         System.out.println(portId.getName());
    }
}

On the Widows, Device manager, i can find the modem under Ports section/
Thanks,
Eyal.

Your rating: None Average: 5 (1 vote)

comm api

hi all
i am facing problem to read input from com port
i am using javax.com package on a windows machine

the program is:

import java.io.*;
import javax.comm.*;
import java.util.*;

public class PortReader implements SerialPortEventListener {
static Enumeration ports;

static CommPortIdentifier pID;

InputStream inStream;

SerialPort serPort;

public PortReader() throws Exception{
serPort = (SerialPort) pID.open("PortReader", 2000);
inStream = serPort.getInputStream();

serPort.addEventListener(this);

serPort.notifyOnDataAvailable(true);

serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}

public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
System.out.println("SerialPortEvent.BI occurred");
case SerialPortEvent.OE:
System.out.println("SerialPortEvent.OE occurred");
case SerialPortEvent.FE:
System.out.println("SerialPortEvent.FE occurred");
case SerialPortEvent.PE:
System.out.println("SerialPortEvent.PE occurred");
case SerialPortEvent.CD:
System.out.println("SerialPortEvent.CD occurred");
case SerialPortEvent.CTS:
System.out.println("SerialPortEvent.CTS occurred");
case SerialPortEvent.DSR:
System.out.println("SerialPortEvent.DSR occurred");
case SerialPortEvent.RI:
System.out.println("SerialPortEvent.RI occurred");
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("SerialPortEvent.OUTPUT_BUFFER_EMPTY occurred");
break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("SerialPortEvent.DATA_AVAILABLE occurred");
byte[] readBuffer = new byte[20];

try {
while (inStream.available() > 0) {
int numBytes = inStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException ioe) {
System.out.println("Exception " + ioe);
}
break;
}
}

public static void main(String[] args) throws Exception{
ports = CommPortIdentifier.getPortIdentifiers();

while (ports.hasMoreElements()) {
pID = (CommPortIdentifier) ports.nextElement();
System.out.println("Port " + pID.getName());

if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (pID.getName().equals("COM1")) {
PortReader pReader = new PortReader();
System.out.println("COM1 found");
}
}
}
}

}

when running this alone i get the response

Port com1
COM1 found
port com6
port com7
port com8
portLPT1
port LPT2

but when i use a gps simulator to output on the com1 port
while running the program simulator pops a message "com1 in use"

And when i run the gps simulator first the the program the the program gives
the exception:
Exception in thread "main" javax.comm.portInUseEXception:Port currently
owned by gps_simulator_com1

pls suggest a way how to read output from the simulator through com1 by the program

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.