Discussion:
Help about writing to a local COM port
(too old to reply)
Ale
2013-01-29 09:37:57 UTC
Permalink
Hi,
I'm using JDK 1.6.0_05 and I have to write to a local COM port (OS is Windows XP SP2 32 bit).

I've installed and set the Comm libraries :
1) downloaded the "comm.jar" and put under C:\Program Files\Java\jdk1.6.0_06\jre\lib\ext
2)copied "win32com.dll" to C:\Program Files\Java\jdk1.6.0_06\jre\bin
3)copied "javax.comm.properties" to C:\Program Files\Java\jdk1.6.0_06\jre\lib\ext

This is my code snippet, it should work but even if I don't get any error, the problem is already at the beginning because the command "CommPortIdentifier.getPortIdentifiers();" gives a NULL object, so no serial port read !!

Please could you help ?

Thanks a lot and best regards,
Alessandro


///////////////////////////
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static String messageString="TEST";

public static void main(String[] args) {

try
{
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
///////////////////////////
Roedy Green
2013-01-29 14:10:10 UTC
Permalink
Post by Ale
2)copied "win32com.dll" to C:\Program Files\Java\jdk1.6.0_06\jre\bin
is that on the path or library path? Use Wassup run as an
application to check


http://mindprod.com/applet/wassup.html
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
Roedy Green
2013-01-29 14:17:28 UTC
Permalink
Post by Ale
3)copied "javax.comm.properties" to C:\Program Files\Java\jdk1.6.0_06\jre\lib\ext
was that in the instructions? I would have expected them to be in the
current dir. Double check.

You realise you are using an old java. Make sure COMM is supposed to
work with it. see http://mindprod.com/jgloss/jdk.html to get a fresh
one.

Also see http://mindprod.com/jgloss/serialport.html

There are many ext dirs. Inoculate them all. You never know which
one you are using.

see http://mindprod.com/jgloss/ext.html
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
Loading...