我写了一个简单的led控制程序并上传到Arduino,当我使用"串行监视器"将控制命令发送给Arduino。虽然,我尝试在ubuntu上编写一个java程序来发送命令。当我运行我的java程序时,我可以看到RX指示灯闪烁,但似乎Arduino无法识别命令。
我在这里发布我的代码。希望任何人都能帮助我。
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import gnu.io.* ; // rxtx serial port driver
public class SerialTest {
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public static void main( String[] args) {
boolean portFound = false;
String defaultPort = "/dev/ttyACM0";
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("SerialTest", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream = serialPort.getOutputStream();
outputStream.write("cccc".getBytes());
} catch (IOException e) {}
serialPort.close();
System.exit(1);
}
}
}
}
}