嵌入式设计应用
// Generate a long delay for card reset and read intervals. void longDelay() { int i, j; for (i = 1; i < 5; i++) { for (j = 1; j < 5000; j++) { ; } } } // Generate a shorter delay (used between STROBE/DATA transitions). void delay() { int i; for (i = 1; i < 1000; i++) { ; } } // Release the DATA line (P0.0) and allow it to float high. void dataHigh() { P0 |= 0x01; delay(); } // Drive the DATA line (P0.0) low. void dataLow() { P0 &= 0xFE; delay(); } // Release the STROBE line (P0.1) and allow it to float high. void strobeHigh() { P0 |= 0x02; delay(); } // Drive the STROBE line (P0.1) low. void strobeLow() { P0 &= 0xFD; delay(); } void resetCardReader() { dataHigh(); strobeHigh(); longDelay(); dataLow(); // Force DATA low. longDelay(); strobeLow(); // Drive STROBE low, then high again. strobeHigh(); strobeLow(); // Drive STROBE low, then release DATA. dataHigh(); longDelay(); strobeHigh(); // Drive STROBE low and high again two more times strobeLow(); // to complete the reset and leave the card reader strobeHigh(); // in the ready state, prepared to scan a card. strobeLow(); }
// Wait for the DATA line to be driven low by the card reader. void waitForDataLow() { int i = 0xFF; dataHigh(); // Make sure that DATA is floating high. while ((i & 1) == 1) { i = P0; } } .... resetCardReader(); printf("\r\n"); printf("Waiting for card swipe...\r\n"); printf("\r\n"); waitForDataLow(); // DATA low indicates that card swipe has begun. strobeHigh(); longDelay(); strobeLow(); longDelay(); waitForDataLow(); // DATA low indicates that card swipe is complete.
// Clock a single bit value out of the card reader by driving STROBE high, // then low, and reading the DATA line. int readBit() { int i; strobeHigh(); // Drive STROBE high. strobeLow(); // Drive STROBE low (DATA line now contains bit). i = P0; if ((i & 1) == 0) { return 1; // Low on DATA line indicates a 1 bit. } else { return 0; // High on DATA line indicates a 0 bit. } }读卡器同步输出的前16位是“前导”位,指出读卡器ASIC的版本。这些为并非卡的数据,应用程序可以忽略它们。
// 0123456789012345678901234567890123456789012345678901234567890 123 char char7bit[64] = " !'#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"; // Clock out and decode a 7-bit character from the track memory, returning the // character value. 7-bit (alphanumeric) characters are found on Track A only. char read7BitChar() { int i, c; // Each character is composed of 7 bits, which we clock out of the track memory // beginning with the least significant bit. Bit 7 is parity, which is ignored. c = 0; for (i = 1; i < 128; i *= 2) { c |= (readBit() * i); } c &= 0x3F; return char7bit[c]; // Decode/return the character using the 7-bit table. } .... // Track A - 76 characters, 7 bits per alphanumberic character including parity. printf("Track A > "); for (i = 0; i < 76; i++) { putchar(read7BitChar()); } printf("\r\n\r\n"); // At this point, we have read 532 bits of the 704-bit Track A memory on the // card reader IC. Flush the remaining 172 bits. for (i = 0; i < 172; i++) { readBit(); }不同类型的卡在磁道A上有不同的数据。磁道A还可以含有字母字符。因此,磁道A常用于存储持卡人的姓名、地址和账号等数字信息。正如上面代码所示,在同步输出磁道B的数据之前,必须读出磁道A的所有704位数据(即使并非所有位都含有编码数据)。
// 0123456789012345 char char5bit[16] = "0123456789:;<=>?"; // Clock out and decode a 5-bit character from the track memory, returning the // character value. 5-bit (numeric+symbol) characters are found on Tracks B and C. char read5BitChar() { int i, c; // Each character is composed of 5 bits, which we clock out of the track memory // beginning with the least significant bit. Bit 5 is parity, which is ignored. c = 0; for (i = 1; i < 32; i *= 2) { c |= (readBit() * i); } c &= 0x0F; return char5bit[c]; // Decode/return the character using the 5-bit table. } .... // Track B - 40 characters, 5 bits per numeric/symbol character including parity. printf("Track B > "); for (i = 0; i < 40; i++) { putchar(read5BitChar()); } printf("\r\n\r\n");在磁道A的最后,读取磁道C之前,必须读取磁道B的所有剩余位(如果需要用到磁道C)。由于已经从磁道B (40个字符 x 5位)读取了200位,在访问磁道C之前,必须同步输出其余504位。
全部0条评论
快来发表一下你的评论吧 !