简体   繁体   English

com0com RS232终端Java程序

[英]com0com RS232 terminal Java program

I made a Java program that runs on a computer with two RS-232 ports. 我制作了一个Java程序,该程序在具有两个RS-232端口的计算机上运行。

It works pretty good. 效果很好。 I connected two devices that communicate with each other through RS-232. 我连接了两个通过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. 通常,设备1发送查询1,然后设备做出响应。

But after some time the device starts sending query 2 and device 2 doesn't respond anymore. 但是一段时间后,设备开始发送查询2,而设备2不再响应。

Here is a capture: 这是一个捕获:

  • First column: COM port id 第一列:COM端口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. 它只会在5分钟至1小时后停止工作。

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

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 . 或者,如果方便的话,请使用RS-232串行间谍监视电缆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM