我制作了一个在具有两个 RS-232 端口的计算机上运行的 Java 程序。
它工作得很好。我连接了两个通过 RS-232 相互通信的设备。我把电脑放在电缆之间。
您可以在终端窗口上看到发送的所有内容。
但是在随机时间后,一个设备停止响应查询。
通常设备 1 发送查询 1 并且设备响应。
但一段时间后,设备开始发送查询 2,设备 2 不再响应。
这是一个捕获:
- 第一列:COM 端口号
- 第二栏:字符的十进制表示
- 第三栏:人物形象化
为什么这不起作用?我计划将来使终端程序开源。
编辑:我没有发布任何代码,因为代码有效。它仅在 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();
}
}
}