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();
}
}
}
}