我一直在测试和处理我的RFID扫描仪代码,我发现了一些奇怪的东西。当我点击卡片时,在某些情况下结果会被截断。
例子:
点击 1:[2]1,000,007,000242985
点击 2:7[3][2]1,000,007,0002429
点击 3:857[3][2]1,000,007,00024
点击 n:等。
预期输出: [2]R,AAA,TTT,NNNNNNNNNN[3]
R = if the ID is registered or not: flags 1 and 0
A = The address where the ID is located inside the scanner's memory
T = the type of card
N = the ID number
如果我理解正确,传输以 ASCII 字符 2 和 3 开始和结束。所以看起来我得到的是一个不完整的结果。我该如何解决这个问题?
这是我的完整代码:
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
public class ComControl{
static SerialPort serialPort;
String[] collector;
public static void main(String[] args) {
serialPort = new SerialPort("COM6");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
serialPort.writeBytes(" 02v0 03".getBytes());//Write data to port
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
try {
serialPort.openPort();//Open port
serialPort.setParams(9600, 8, 1, 0);//Set params
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
@Override
public void serialEvent(SerialPortEvent event) {
try{
}catch(Exception e){
System.out.println(e);
}
if(event.isRXCHAR()){//If data is available
if(event.getEventValue() == 10){//Check bytes count in the input buffer
//Read data, if 10 bytes available
String sizer="";
try{
sizer="";
sizer += serialPort.readString();
}catch (SerialPortException ex) {
System.out.println(ex);
}
System.out.println(sizer);
sizer = "";
}
}
// else if(event.isCTS()){//If CTS line has changed state
// if(event.getEventValue() == 1){//If line is ON
// System.out.println("CTS - ON");
// }
// else {
// System.out.println("CTS - OFF");
// }
// }
// else if(event.isDSR()){///If DSR line has changed state
// if(event.getEventValue() == 1){//If line is ON
// System.out.println("DSR - ON");
// }
// else {
// System.out.println("DSR - OFF");
// }
// }
}
}
}
以下是有关我正在使用的设备的信息:低成本RFID阅读器 - 电子小玩意
关系,我得到了答案。我愚蠢到把 10 个字节放在它实际上是 22 个字节的时候。