Question

I made a Java program that runs on a computer with two RS-232 ports.

It works pretty good. I connected two devices that communicate with each other through RS-232. I put the computer between the cable.

You can see everything getting send on a terminal window.

But after a random amount of time one device stops responding to queries.

Normally device 1 sends query 1 and the device responds.

But after some time the 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

https://i77.photobucket.com/albums/j74/bertyhell/errors/capture.png?t=1281084570

Why is this 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. It only stops working after 5 min - 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 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();
        }
    }

This is the receiver:

    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());
            }
        }
    }

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();
            }
        }
    }
Was it helpful?

Solution

I can't figure out what the problem is, so I'm going to try it with a physical cable.

Or if you're handy, a RS-232 serial spy monitor cable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top