0
我做了一個在兩臺RS-232端口的計算機上運行的Java程序。com0com RS232終端Java程序
它工作的很好。我連接了兩臺通過RS-232相互通訊的設備。我把電腦放在電纜之間。
您可以在終端窗口中看到發送的所有內容。
但是經過一段隨機時間後,一臺設備停止響應查詢。
通常,設備1發送查詢1並且設備響應。
但經過一段時間後,設備開始發送查詢2,設備2不再響應。
這裏是一個捕捉:
- 第一欄:COM端口ID
- 第二欄:字符
- 三列的小數表現:字符的可視化
爲什麼這不起作用?我計劃在未來將終端程序開源。
編輯: 我沒有發佈任何代碼,因爲代碼的作品。它只在5分鐘 - 1小時後停止工作。
這裏是連接代碼:
CommPortIdentifier portIdentifier;
portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
InputStream inCom1;
InputStream inCom2;
if (portIdentifier.isCurrentlyOwned()) {
addError("COM1 in use!, please restart");
}
else {
SerialPort serialPort = (SerialPort) portIdentifier.open("Main", 2000);
//19200 8n1
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// CTS/RTS handshaking
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
//Set sender
Com1Sender.setWriterStream(serialPort.getOutputStream());
//Set receiver
// new com1_receive(serialPort.getInputStream()).start();
inCom1 = serialPort.getInputStream();
portIdentifier = CommPortIdentifier.getPortIdentifier("COM2");
if (portIdentifier.isCurrentlyOwned()) {
addError("COM2 in use!, please restart");
}
else {
serialPort = (SerialPort) portIdentifier.open("Main2", 2001);
//19200 8n1
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// CTS/RTS handshaking
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
//Set sender
Com2Sender.setWriterStream(serialPort.getOutputStream());
//set receiver
// new com2_receive(serialPort.getInputStream()).start();
inCom2 = serialPort.getInputStream();
new Receiver(inCom1, inCom2).start();
}
}
這是接收器:
public class Receiver extends Thread {
InputStream inCom1;
InputStream inCom2;
public Receiver(InputStream inCom1, InputStream inCom2) {
this.inCom1 = inCom1;
this.inCom2 = inCom2;
}
@Override
public void run() {
try {
int b1;
int b2;
while (true) {
// if stream is not bound in.read() method returns -1
//dect
while ((b1 = inCom1.read()) != -1) {
//Send trough to COM2
Com2Sender.send(new byte[]{(byte) b1});
Main.addText(Integer.toString(b1), true);
}
//televic
while ((b2 = inCom2.read()) != -1) {
//Send trough to COM2
Com1Sender.send(new byte[]{(byte) b2});
Main.addText(Integer.toString(b2), false);
MessageExtractor.add(b2);
}
// Wait 10 ms when stream is broken and check again.
sleep(10);
}
} catch (Exception e) {
Main.addError(e.getMessage());
}
}
}
這是發件人的一個:
public class Com1Sender {
static OutputStream out;
public static void setWriterStream(OutputStream out) {
Com1Sender.out = out;
}
public static void send(byte[] bytes) {
try {
// Sending through serial port is simply writing into OutputStream.
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void send(int letter) {
try {
Main.addText(Character.toString((char)letter), false);
// Sending through serial port is simply writing into OutputStream.
out.write(new byte[]{(byte)letter});
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您會發現,如果您發佈了一些關鍵代碼,編碼人員將獲得更多信息來幫助您解答問題。例如,您究竟如何閱讀和寫入端口? – 2010-08-06 09:02:06
好的,添加了代碼 希望它不會嚇跑ppl ^^ – Berty 2010-08-06 14:40:20