串行事件值和串行端口值之间的差异

时间:2017-07-10 07:04:28

标签: java serial-port sensor jssc input-devices

我正在使用jssc从串口读取值。我写入串口后调用以下回调。在该回调中,我可以从事件中获取值,并且从串行端口本身包含不同的值。我想知道串口值和串行事件值之间的区别事件是否包含来自串口的值?。

public class PortReader implements SerialPortEventListener {

SerialPort serialPort;
public PortReader(SerialPort serialPort) {
    this.serialPort = serialPort;
}

@Override
public void serialEvent(SerialPortEvent event) {
    System.out.println("started");
    if (event.isRXCHAR() && event.getEventValue() > 0) {
        try {
            String receivedData =  serialPort.readString(event.getEventValue());
            System.out.println("Received response: " + receivedData);
            receivedData = serialPort.readString();
            System.out.println("Received response: " + receivedData);
        } catch (SerialPortException ex) {
            System.out.println("Error in receiving string from COM-port: " + ex);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

根据JSSC的文档,事件值是jssc特定的,而事件携带的数据来自串口。