【问题标题】:Send 'A' in the form of byte array to comm port using javaSerial port library使用java串口库以字节数组的形式将'A'发送到com端口
【发布时间】:2018-06-13 14:32:33
【问题描述】:

我正在尝试向连接到“COM1”的动臂屏障发送命令,它的波特率 -> 9600,奇偶校验 -> 无,数据位 ->8,停止位 -> 1。我正在尝试以下操作代码但无法发送命令。请帮帮我。

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

import com.fazecast.jSerialComm.SerialPort;

public class BoomBarrierUsingJavaSCLib {

    public static void main(String[] args) {
        SerialPort comPort = SerialPort.getCommPorts()[0];
        //System.out.println(SerialPort.getCommPorts().length);
        comPort.openPort();
        comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);

        comPort.setComPortTimeouts(1000, 1000, 1000);
        System.out.println("Written to Comport: "+comPort.writeBytes("A".getBytes(),"A".getBytes().length));
        comPort.closePort();
    }

}

但我得到以下输出:写入 Comport:-1 理想情况下,它应该返回写入通信端口的字节数。 -1 表示没有写入任何内容。

提前致谢。

【问题讨论】:

    标签: java string serial-port byte comm


    【解决方案1】:

    我错过的是,我没有关闭不允许打开通信端口的 bray++ 终端。在写入端口时,需要检查端口是否可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多