0

I am writing a program for Datalogic Scanner to scan QR code and using JSSC library to send a command. When I first scan the QRcode it prints out the actual number when I scan the second QRCode it prints out the QRcode of the first and same goes on to different QRcode. Seems like everything is right but I don't know what I am doing wrong. Help, please.

I have already tried looking for solutions to my problem but seems like none of the questions address the issue I am facing. And also I am getting this error when I debug the code in Intellij "Inconvertible types; cannot cast 'com.company.BarcodeScanningController' to 'jssc.SerialPort.EventThread'". It occurs from the time when I instantiate a class.

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;

public class BarcodeScanningController {
    static SerialPort serialPort;

    public static void main(String[] args) {
        serialPort = new SerialPort("COM4");
        try {
            serialPort.openPort();//Open serial port

            //Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
            serialPort.setParams(9600, 8, 1, 0);
            serialPort.writeString(new String(new byte[]{0x02})); //Activates the bar code scanner
            serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
        } catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }

    public static class SerialPortReader implements SerialPortEventListener {
        String str = "";
        public void serialEvent(SerialPortEvent event) {
            if (event.isRXCHAR() && event.getEventValue() > 0) {//If data is available
                //Check bytes count in the input buffer
                int bytesCont = event.getEventValue();
                try {
                    str = serialPort.readString(bytesCont);
                    System.out.println(str);
                } catch (SerialPortException e) {
                    e.printStackTrace();
                }
            }
            try {
                serialPort.writeString(new String(new byte[]{0x03})); //deactivates bar code scanner
                System.out.println("Close message sent");
                serialPort.removeEventListener();
                System.out.println("Event Listener removed");
                serialPort.closePort();
                System.out.println("Port Closed");
            } catch (SerialPortException e) {
                e.printStackTrace();
            }
        }
    }
}
sanki
  • 59
  • 2
  • 9
  • is the main class called more than once? – B.Letz Aug 09 '19 at 07:15
  • I think you should not close the `serialPort` in your `SerialPortReader` – B.Letz Aug 09 '19 at 07:17
  • At the start of your `serialEvent(SerialPortEvent event)` set `str = ""` – B.Letz Aug 09 '19 at 07:19
  • The main class is called multiple times only to test the QRCode. And I kept the port open but that didn't help and also I set the str = "", no difference. – sanki Aug 09 '19 at 15:06
  • It seems like a bug in the scanner. I unplugged it and plugged it back in and it working as expected. – sanki Aug 09 '19 at 19:02
  • I have another problem regarding the above class. How do I retrieve the value of "str" from another class? When I instantiate the above class and call scannerHandler(Changed main to scannerHandler()) on another class the EventListener waits for QRCode to be scanned. I know I will not retrieve any value until I scan the code. I am having hard time implementing it. – sanki Aug 11 '19 at 17:14
  • If you have a new question, please ask a new question. But please research before you ask. – B.Letz Aug 13 '19 at 08:23

0 Answers0