我在我的Seeeduino Mega 1.22上有一個簡單的草圖,它只是在液晶顯示器上顯示串行輸入。使用lynx術語和arduino串行監視器工作正常:正在顯示發送的輸入。當我想在我的Java程序(在Win7 x64機器上的Eclipse中運行)和Seeeduino之間啓動串行通信時,麻煩就開始了。我正在使用RXTX x64版本。該程序旨在通過開放端口發送和接收一些string.getBytes()。在Java端接收工作,但在Arduino端接收失敗。Java RXTX和Arduino之間的串行通信的流量控制設置
似乎問題在於正確的流量控制設置。我看到有些人有類似這裏的問題Issues receving in RXTX
但是這個解決方案對我不起作用。如果我將FlowControl設置爲None,那麼只會在顯示屏上顯示一個塊圖標,表明串行連接已建立,但沒有其他任何內容。如果我將FlowControl設置爲RCTS_IN | RCTS_OUT,然後當我關閉建立的連接時,我只在顯示屏上顯示字符串字節。
爲什麼只有當我關閉連接時才發送數據(刷新出流也沒有幫助)?我在做Flow Controll設置有什麼問題?
這是我正在使用的修改後的connect()方法。
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier
.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(),
2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
try {
serialPort.setFlowControlMode(
// SerialPort.FLOWCONTROL_NONE);
// OR
// If CTS/RTS is needed
//serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
} catch (UnsupportedCommOperationException ex) {
System.err.println(ex.getMessage());
}
serialPort.setRTS(true);
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
(new Thread(new SerialWriter(out))).start();
serialPort.addEventListener(new SerialReader(in, this));
serialPort.notifyOnDataAvailable(true);
} else {
System.out.println("Error: Only serial ports are to use!");
}
}
}
預先感謝您的時間