i made a JAVA program that runs on a computer with 2 RS232 ports
it works pretty good i connected 2 devices that comunicate with eachother trough RS232 i put the computer between the cable
you can see everything getting send on a terminal window
but after a random amount of time 1 device stops responding on queries
normally device 1 sends query 1 and device responds
but after some time device starts sending query 2 and device 2 doesn't respond anymore
here is a capture: first column: com port id second column: decimal presentation of the character third column: visualization of characters
any idea's why this is not working? I'm planning on making the terminal program open source in the future
EDIT: i didn't post any code because the code works only stops working after 5min - 1 hour
here is the connection code: 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 reciever // new com1_recieve(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 reciever
// new com2_recieve(serialPort.getInputStream()).start();
inCom2 = serialPort.getInputStream();
new Reciever(inCom1, inCom2).start();
}
}
this is the reciever: public class Reciever extends Thread {
InputStream inCom1;
InputStream inCom2;
public Reciever(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 10ms when stream is broken and check again
sleep(10);
}
} catch (Exception e) {
Main.addError(e.getMessage());
}
}
}
this is one of the senders: 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();
}
}
}