Problem Getting the Serial Port.

M

Malik Asif Joyia

Hello
I m writing the following code but it is not working can any bod tell
that what is the problem with it?
<code>
public void OpenSerialPort() throws java.io.IOException {
try {
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CPI1 = CommPortIdentifier.getPortIdentifier("COM1");
try
{
sm = (SerialPort) CPI1.open("UARTTesting", 3000);
}catch(PortInUseException ee)
{
System.out.println(ee.getMessage());
}

} catch (NoSuchPortException e) {
System.out.println(e.getMessage());
}


}
</code>
but when i run the code it throws exception no such port exists, My OS
is Windows 2003 Server Enterpirze Edition.

Thanks
 
T

Tor Iver Wilhelmsen

Malik Asif Joyia said:
I m writing the following code but it is not working can any bod tell
that what is the problem with it?

The comm API will silently "fail" to detect any ports if it cannot
load the native component. Are you sure javacomm.dll is on the library
path?
 
D

Dale King

Tor said:
The comm API will silently "fail" to detect any ports if it cannot
load the native component. Are you sure javacomm.dll is on the library
path?

Actually, I believe it usually occurs because of not having that stupid
properties file as well.

I recommend against using the Java Comm Api and going with RXTX
(www.rxtx.org). It at least has had active development in the past 7 years.
 
K

Knute Johnson

Malik said:
Hello
I m writing the following code but it is not working can any bod tell
that what is the problem with it?
<code>
public void OpenSerialPort() throws java.io.IOException {
try {
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CPI1 = CommPortIdentifier.getPortIdentifier("COM1");
try
{
sm = (SerialPort) CPI1.open("UARTTesting", 3000);
}catch(PortInUseException ee)
{
System.out.println(ee.getMessage());
}

} catch (NoSuchPortException e) {
System.out.println(e.getMessage());
}


}
</code>
but when i run the code it throws exception no such port exists, My OS
is Windows 2003 Server Enterpirze Edition.

Thanks

The installation is tricky and all of the parts have to be in the right
places. Here is the little class I wrote to identify the serial ports
and also to tell me that the JavaComm API was installed correctly. My
Windows XP installation requires the:

comm.jar file to be in the Program Files/Java/jdk1.5.0_03/jre/lib/ext
and the Program Files/Java/jre1.5.0_03/lib/ext directories.

javax.comm.properties file to be in the
Program Files/Java/jre1.5.0_03/jre/lib directory.

win32com.dll file to be in the Program Files/Java/jre1.5.0_03/bin directory.

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

public class Ports {
public Ports() {
CommPortIdentifier cpi = null;

Enumeration e = CommPortIdentifier.getPortIdentifiers();

while (e.hasMoreElements()) {
try {
cpi = (CommPortIdentifier) e.nextElement();
} catch (NoSuchElementException n) {}
System.out.println(cpi.getName());
}
}
public static void main(String[] args) { new Ports(); }
}
 
D

Dale King

Knute said:
The installation is tricky and all of the parts have to be in the right
places. Here is the little class I wrote to identify the serial ports
and also to tell me that the JavaComm API was installed correctly. My
Windows XP installation requires the:

comm.jar file to be in the Program Files/Java/jdk1.5.0_03/jre/lib/ext
and the Program Files/Java/jre1.5.0_03/lib/ext directories.

javax.comm.properties file to be in the
Program Files/Java/jre1.5.0_03/jre/lib directory.

win32com.dll file to be in the Program Files/Java/jre1.5.0_03/bin
directory.

I recommend putting all 3 files in both the JDK and the JRE to make sure
it works. Depending on your setup you may end up invoking the java
runtime that is in the JDK instead of the JRE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top