我正在将此RFID模块用于Arduino以太网R3,我需要从软件序列中检索写在标签外部的卡(TAG)ID。 模块的datasheet表示将14个字节发送到Arduino。第一个是标题,最后一个页脚,页脚前面的2个字节是校验和,另外10个字节是包含标记ID的ASCII数据。
如何重新创建卡的ID,并控制校验和?例如,如果标签的ID为:0013530444,则Arduino响应为:
I received: 2
I received: 51
I received: 67
I received: 48
I received: 48
I received: 67
I received: 69
I received: 55
I received: 53
I received: 52
I received: 67
I received: 67
I received: 66
I received: 3
但我不知道如何在屏幕上打印Arduino读取的ID。如何计算checksum?
http://www.seeedstudio.com/wiki/index.php?title=125Khz_RFID_module_-_UART
任何人都可以帮助我吗?
答案 0 :(得分:3)
以下是如何计算校验和的演练。
取你的卡号(这只是直接引用你的文字)
I received: 2
I received: 51
I received: 67
I received: 48
I received: 48
I received: 67
I received: 69
I received: 55
I received: 53
I received: 52
I received: 67
I received: 67
I received: 66
I received: 3
这将为您提供一个与以下内容相同的数字:
2 51 67 48 48 67 69 55 53 52 67 67 66 3
第一个数字(2)表示这是请求的开始。
最后一个数字(3)表示这是请求的结束。
2 51 67 48 48 67 69 55 53 52 67 67 66 3
为了计算校验和,我们将删除这两个数字。所以你的新号码现在是:
51 67 48 48 67 69 55 53 52 67 67 66
你拥有的最后两个数字是你的校验和。剩下的数字是您的卡号。所以:
您的卡号是:
51 67 48 48 67 69 55 53 52 67
你的校验和是:
67 66
接下来,您需要将您的卡号和校验和转换为ASCII值:
您的卡号是:
3 C 0 0 C E 7 5 4 C
你的校验和是:
C B
接下来,将每个数字成对:
您的卡号是:
3C 00 CE 75 4C
你的校验和是:
CB
然后,您需要将每个对视为HEXIDECIMAL值并对它们执行XOR。所以基本上你需要证明以下内容:
3C ^ 00 ^ CE ^ 75 ^ 4C == CB
(3C ^ 00)= 3C
3C ^ CE ^ 75 ^ 4C == CB
(3C ^ CE)= F2
F2 ^ 75 ^ 4C == CB
(3C ^ CE)= 87
87 ^ 4C == CB
(87 ^ 4C)= CB
CB == CB
因为CB == CB,这是一个有效的交易。
毫无疑问,其他人可以提出比这更好的方法,但是这里应该有足够的伪代码供你自己编写。
答案 1 :(得分:0)
我发现this blog在Arduino中有一个实现,我已经调整它在Java
中工作,结果很好。由于存在大量按位操作 - 我使用http://www.miniwebtool.com/bitwise-calculator/来尝试了解发生了什么。我理解除(val | (tempbyte << 4))
之外的所有内容,我的意思是我理解这句话的作用,我很难看出它是如何产生我想要的结果的。
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) {
// check for header
bytesread = 0;
while (bytesread < 12) {
// read 10 digit code + 2 digit checksum
if( Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) {
// if header or stop bytes before the 10 digit reading
break;
// stop reading
}
// Do Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
} else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) {
// If we're at the checksum byte,
checksum ^= code[bytesread >> 1];
// Calculate the checksum... (XOR)
};
} else {
tempbyte = val;
// Store the first hex digit first...
};
bytesread++;
// ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) {
// removed code for clarity
LCD.print("Check:");
LCD.print(code[5], HEX);
LCD.print(code[5] == checksum ? "-passed" : "-error");
}
bytesread = 0;
}
}
}
我的Java / Android端口正在监听BluetoothSocket
。我使用BlueTerm中的代码作为基础,此代码位于ConnectedThread
。为所有愚蠢的评论道歉,但我还在学习Java)。
//assume you have checksum as int and code as int array. it will overflow if bytes are used like above example
public void run() {
Log.d(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
Log.d(TAG, "Running");
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
for (int i = 0; i < bytes; i++) {
Log.d(TAG, "Reading: " + i + " of " + bytes + " from input stream");
byte b = buffer[i];
try {
if(bytesread >= 0) {
//just printing ASCII
char printableB = (char) b;
if (b < 32 || b > 126) printableB = ' ';
Log.d(TAG, "'" + Character.toString(printableB) + "' (" + Integer.toString(b) + ")");
if((b == 0x0D)||(b == 0x0A)||(b == 0x03)||(b == 0x02)) {
// if header or stop bytes before the 10 digit reading
Log.e(TAG, i + " Unexpected header while processing character " + Integer.toString(b));
} else {
// Do ASCII/Hex conversion
if ((b >= '0') && (b <= '9')) {
b = (byte) (b - '0');
} else if ((b >= 'A') && (b <= 'F')) {
b = (byte) (10 + b - 'A');
}
if ((bytesread & 1) == 1) {
//if isOdd(bytesread)
// make some space for this hex-digit by shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (b | (tempbyte << 4));
if (bytesread >> 1 != 5) {
// If we're not at the checksum byte,
checksum ^= code[bytesread >> 1];
// Calculate the checksum... (XOR)
}
} else {
// Store the first hex digit first
tempbyte = b;
}
}
bytesread++;
} else if(b == 2) {
bytesread = 0;
Log.d(TAG, "Header found!");
}
if(bytesread == 12) {
String check = (code[5] == checksum ? "-passed" : "-error");
String r = "";
for(int j = 0; j < 5; j++){
r += Integer.toString(code[i]);
}
Log.d(TAG, "Check:" + Integer.toString(code[5]) + check);
init();
} else if(bytesread > 12){
Log.e(TAG, "Too many bytes!");
}
} catch (Exception e) {
Log.e(TAG, i + " Exception while processing character " + Integer.toString(b), e);
}
}
String a = buffer.toString();
a = "";
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
Log.i(TAG, "END mConnectedThread");
}