Hi! I have RxTx installed on my iMac with a usb-serial adapter.
I have managed to list all the available serial devices on the computer with this code:
import java.io.*;
import java.util.*;
import gnu.io.*;
public class FindPorts {
public static void main(String[] args) {
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName() + " " +portId.getCurrentOwner());
}
}
}
}
The result I get in "Terminal" when I compile and run is:
Experimental: JNI_OnLoad called.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
/dev/tty.UC-232AC null
/dev/cu.UC-232AC null
So the computer found my usb-serial adapter /dev/tty.UC-232AC null & /dev/cu.UC-232AC null
The problem is when I try to write something to the port with this code:
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "/dev/tty.UC-232AC";
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
System.out.println(
"Writing \""+messageString+"\" to "
+serialPort.getName());
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
serialPort.close();
System.exit(1);
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
}
Terminal answers with:
Experimental: JNI_OnLoad called.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Found port /dev/tty.UC-232AC
Port in use.
My usb-serial adapter is not connected to anything at the time when I run the program.. What does "Port in use" mean in this case?
And which one of /dev/tty.UC-232AC & /dev/cu.UC-232AC should I use in my program?
regards / Christian