我一直在测试和处理我的RFID扫描仪代码,我发现了一些奇怪的东西。当我点击该卡时,有些情况会导致结果被截断。
示例:
点按1: [2] 1000007 ,000242985
点按2: 7 [3] [2] 1,000,0 07,0002429
点按3: 857 [3] [2] 1000 ,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("\002v0\003".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");
// }
// }
}
}
}
以下是有关我正在使用的设备的信息: Low Cost RFID Reader - e-Gizmo
答案 0 :(得分:0)
没关系,我得到了答案。当它实际上是22个字节时,我愚蠢到可以放10个字节。