首页 > 解决方案 > 获取 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;
}

标签: javaserial-portlistenerjava-11jssc

解决方案


推荐阅读