首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >com0com RS232终端Java程序

com0com RS232终端Java程序
EN

Stack Overflow用户
提问于 2010-08-06 08:52:28
回答 1查看 3.6K关注 0票数 0

我制作了一个Java程序,它运行在一台带有两个RS-232端口的计算机上.

效果很好。我连接了两个通过RS-232相互通信的设备.我把电脑放在电缆之间。

你可以在终端窗口看到所有的东西。

但是,经过随机的时间,一个设备停止响应查询。

通常,设备1发送查询1,设备响应。

但是过了一段时间,设备开始发送查询2,设备2不再响应。

下面是一个捕获:

  • 第一列: COM端口id
  • 第二列:字符的十进制表示
  • 第三栏:字符的可视化

为什么这不管用?我计划在将来使终端程序开源。

编辑:我没有发布任何代码,因为代码工作。只有在5分钟-1小时后才停止工作。

以下是连接代码:

代码语言:javascript
复制
    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();
        }
    }

这是接受者:

代码语言:javascript
复制
    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());
            }
        }
    }

这是发送者之一:

代码语言:javascript
复制
    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();
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-23 08:04:22

我不知道问题出在哪里,所以我打算用物理电缆来尝试一下。

或者你方便的话,http://www.lammertbies.nl/comm/cable/RS-232-spy-monitor.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3422263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档