java - 获取 Ring Indicator isRING() 并使用 jssc 库将其添加到侦听器?
问题描述
我正在尝试使用 JSSC 库为收银机编写一个小程序。我已经使用该库在条形码扫描仪上工作,它工作正常,我从扫描仪获得返回值。但对于收银机案例,我没有得到任何价值。每当收银机打开时,ringindicator = true 和关闭时为 false。在这种情况下,如何在 SerialPort 类中创建一个侦听器以获取 ringindicator 的状态 isRING() 。谢谢!
public class CashRegister implements SerialPortEventListener {
private SerialPort serialPort;
private CashRegisterCallBack callback;
public CashRegister(CashRegisterCallBack callback) {
this.callback = callback;
}
private boolean result;
public void startScan() {
try {
serialPort = new SerialPort("COM1");
if (serialPort.isOpened()) {
serialPort.closePort();
}
serialPort.openPort();//Open serial port
serialPort.addEventListener(this);
//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.setParams(9600, 8, 1, 0);
//Triggers scanner in scanning mode
serialPort.writeByte((byte)7); // opens the register
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
//doesnot get to this method when closed
@Override
public void serialEvent(SerialPortEvent event) {
boolean isDataAvailable = event.isRING();
//Check bytes count in the input buffer
System.out.println(isDataAvailable);
try {
this.callback.callback(isDataAvailable);
serialPort.writeString(new String(new byte[]{0x03}));
System.out.println("Close message sent");
serialPort.removeEventListener();
System.out.println("Event Listener removed");
serialPort.closePort();
System.out.println("Port Closed");
System.exit(0);
} catch (IOException | InterruptedException | SerialPortException e) {
e.printStackTrace();
}
}
}
public interface CashRegisterCallBack {
void callback(boolean result) throws IOException, InterruptedException;
}
解决方案
推荐阅读
- javascript - 如何在javascript中获取子节点
- json - How to parse Json data returned from a HTTP request in scala
- android - 如何确保在应用程序关闭时运行的服务不会被杀死?
- grafana - 如何在 grafana 的条形规面板中更改标题
- windows - 在 PowerShell 中更改错误消息语言
- mongodb - 打字稿中的全部承诺并不能解决所有问题
- azure - 使用命令行将文件从 HDFS 传输到 ADLS Gen 2 的过程与传输到 BLOB 的过程是否相同?
- algorithm - 关节点忽略导致直接父级的边缘的反向
- dialogflow-es - 如何从 Dialogflow 调用 Rest API
- php - 使用 Python 从带有真实性令牌的表单中获取链接