The communication format between a non-contact card reader and a PC is as follows:
STX (02 h) + 6-byte card number + Verh + verl + EOT (04 H) STX (02 h) Start byte EOT (04 H) the end is a 6-byte card with six hexadecimal ASCII characters. It is transmitted in 6 bytes. The high byte is in front and the low byte is in the back. For example: Card number: 8 d e f 9 E transmission data format: 38 44 45 46 39 45 (hexadecimal) adopts the most common BCC verification method at present: the specific method is to verify the valid card number in bytes for an exception or (XOR): 38 H (XOR) 44 h (XOR) 45 h (XOR) 46 h (XOR) 39 h (XOR) 45 h = 03 h and then combine the received data Verh + verl into a byte data, 30 h (HEX) = H (HEX) = 3 the merged data is 03 H. If the received data is consistent with the verification data of the card number we received, the received card number is the correct card number. For example, if the current card number is: Card number: 0 5 8 E 4 2 Data Transmission Format: 30 35 38 45 34 32 (hexadecimal) currently, the most common BCC verification method is used for verification. The specific method is to perform an exclusive or (XOR) Verification for valid card numbers connected to Bytes: 30 h (XOR) 35 h (XOR) 38 H (XOR) 45 h (XOR) 34 h (XOR) 32 h = 7eh, then the received data Verh + verl is merged into a byte data, 37 h (HEX) = 7, 45 h (HEX) = E. The synthesized data is 7eh. If the received data is consistent with the verification data of the card number we received, the received card number is the correct card number. CompileProgramYou can first receive all the data to the computer's memory, then calculate the BCC check value value1, and then combine the received BCC value into a hexadecimal value value2, then compare the two values. If the two values are equal, the received card number is a valid card number and then processed according to your system.
Public static string getbcc (byte [] data ){
String ret = "";
Byte BCC [] = new byte [1];
For (INT I = 0; I <data. length; I ++)
{
BCC [0] = (byte) (bcc [0] ^ data [I]);
}
String hex = integer. tohexstring (bcc [0] & 0xff );
If (Hex. Length () = 1 ){
Hex = '0' + hex;
}
RET + = Hex. touppercase ();
Return ret;
}