【问题标题】:How to send command to serial port (JAVA + RXTX)如何向串口发送命令(JAVA + RXTX)
【发布时间】:2013-10-30 12:50:37
【问题描述】:

我在将数据输入发送到称重天平时遇到问题。我需要发送ESC P CR LF 命令。我使用 RXTX 库。我不知道为什么它不起作用。

代码如下:

public static class SerialWriter implements Runnable {
    OutputStream out;

    public SerialWriter ( OutputStream out ) {
        this.out = out;
    }

    public void run () {
        try {                
            while (true) {
               this.out.write(new byte[]{0x1B, 0x50, 0x0D, 0x0A});
                Thread.sleep(1000);
            }                
        } catch ( IOException | InterruptedException e ) {
            e.printStackTrace();
        }            
    }
}

我尝试使用flush,但没有任何反应。

我在下面附上了完整的(修改后的)代码。

package model;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FirstSteps {
    public FirstSteps() {
        super();
    }

    void connect (String portName) throws Exception {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if (portIdentifier.isCurrentlyOwned()) {
            System.out.println("Error: Port is currently in use");
        } else {
            System.out.println("Connect 1/2");
            CommPort commPort = portIdentifier.open(this.getClass().getName(),6000);
            if (commPort instanceof SerialPort) {
                System.out.println("Connect 2/2");
                SerialPort serialPort = (SerialPort) commPort;
                System.out.println("BaudRate: " + serialPort.getBaudRate());
                System.out.println("DataBIts: " + serialPort.getDataBits());
                System.out.println("StopBits: " + serialPort.getStopBits());
                System.out.println("Parity: " + serialPort.getParity());
                System.out.println("FlowControl: " + serialPort.getFlowControlMode());
                serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
                System.out.println("BaudRate: " + serialPort.getBaudRate());
                System.out.println("DataBIts: " + serialPort.getDataBits());
                System.out.println("StopBits: " + serialPort.getStopBits());
                System.out.println("Parity: " + serialPort.getParity());
                System.out.println("FlowControl: " + serialPort.getFlowControlMode());
                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();

                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();
            } else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }

    public static class SerialReader implements Runnable {
        InputStream in;

        public SerialReader(InputStream in) {
            this.in = in;
        }

        public void run() {
            byte[] buffer = new byte[1024];
            int len = -1;
            try {
                while ((len = this.in.read(buffer)) > -1) {
                    //System.out.println("Received a signal.");
                    System.out.print(new String(buffer,0,len));
                }
            } catch ( IOException e ) {
                e.printStackTrace();
            }            
        }
    }

    public static class SerialWriter implements Runnable {
        OutputStream out;

        public SerialWriter(OutputStream out) {
            this.out = out;
        }

        public void run() {
            try {
                byte[] array = {0x1B, 0x50, 0x0D, 0x0A};
                while (true) {
                    this.out.write(new byte[]{0x1B, 0x50, 0x0D, 0x0A});
                    this.out.flush();
                    Thread.sleep(1000);  
                }                
            } catch ( IOException | InterruptedException e ) {
                e.printStackTrace();
            }            
        }
    }

    public static void main(String[] args) {
        try {
            (new FirstSteps()).connect("COM7");
        } catch ( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java serial-port communication rxtx


    【解决方案1】:

    根据提供的代码,我可以给出的唯一建议是在将数据放入其中后尝试刷新流 (out.flush())。

    除此之外 - 我需要查看获取输出流的代码。

    【讨论】:

    • 如果您看到来自连接方法的所有控制台消息 - 确保串行端口参数正确,请尝试重新插入设备并使用其他端口...我想不到的其他.无论哪种方式,您都应该添加代码来关闭流、关闭端口并在完成与端口的通信后停止读/写线程。 RXTX 有很多 bug,如果你没有正确管理你的端口和线程,它很容易死锁你的应用程序。
    • 我在串口监视器中检查串口连接,在这个程序中,当我发送 0x1B 和 0x50 时,天平向计算机发送重量都是正确的,但在 java 中什么也没发生:S
    • serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);我删除了这一行,它可以工作。谢谢。
    【解决方案2】:

    添加这一行:

    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
    

    【讨论】:

      【解决方案3】:
      TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
                                  twoWaySerCom.serialPort.getOutputStream());
      sw.out.write(b);
      sw.out.write((byte)13);//13 code of CR etc.
      

      【讨论】:

      • 它对我有用。
      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多