问题
I used rxtxComm to communicate(send and receive data) with Arduino (USB-Serial port). How do i handle the handshake lines like DTR using the rxtxComm library? Can you give me some tutorial on this or some sample code???
- Note: I use win7 OS. I bought a USB-Serial(DB9) adapter today(21-4-2012); planning to connect a led array directly to rs232...
回答1:
I guess you have all the libraries installed.
change to your port defaultPort = "COM24";
Try the standard code
package serialtest;
import java.util.*;
import gnu.io.*;
import java.io.IOException;
import java.io.OutputStream;
public class DXSimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String msgStr = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "COM24";
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 "+msgStr+"\" to "+serialPort.getName());
try {
outputStream.write(msgStr.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.");
}
}
}
good luck!
来源:https://stackoverflow.com/questions/10256776/handling-handshake-lines-in-rs232-db9-using-rxtxcomm