数据的可靠性吧,而且算法简单 行列都有奇偶校验,简单分析一下 有1bit跳变,校验不能通过 2bit跳变,校验不能通过 3bit跳变,校验不能通过 4bit跳变,除非这4个bit,位于同样的两行两列(但是这个概率又有多大),否则校验不通过 。。。 可以看出,除了人为的修改数据,绝大部分的偶然跳变都能使校验不通过很多年前用51的汇编语言写过, 看用不用的上. ;********************************************** ; subroutine: ; name: Convert_Hex2EMcode ; function: ; convert hex data to em code ; input: ; gvUserBuf ; output: ; gvBuffer ;********************************************** Convert_Hex2EMcode: mov gvBuffer,#0ffh mov r0,#gvBuffer+1 mov @r0,#01h mov r1,#gvUserBuf mov a,@r1 mov b,a mov bitcount,#07h mov bytecount,#05h mov r4,#04h ConvertEMcode_Loop1: mov a,b rlc a mov b,a mov a,@r0 rlc a mov @r0,a djnz bitcount,ConvertEMcode_Loop11 mov bitcount,#08h inc r0 ConvertEMcode_Loop11: djnz r4,ConvertEMcode_Loop1 mov a,@r1 anl a,#0f0h swap a mov c,PSW.0 mov a,@r0 rlc a mov @r0,a djnz bitcount,ConvertEMcode_Loop12 mov bitcount,#08h inc r0 ConvertEMcode_Loop12: mov r4,#04h ConvertEMcode_Loop2: mov a,b rlc a mov b,a mov a,@r0 rlc a mov @r0,a djnz bitcount,ConvertEMcode_Loop21 mov bitcount,#08h inc r0 ConvertEMcode_Loop21: djnz r4,ConvertEMcode_Loop2 mov a,@r1 anl a,#0fh mov c,PSW.0 mov a,@r0 rlc a mov @r0,a djnz bitcount,ConvertEMcode_Loop22 mov bitcount,#08h inc r0 ConvertEMcode_Loop22: inc r1 mov a,@r1 mov b,a mov r4,#04h djnz bytecount,ConvertEMcode_Loop1 mov a,gvBuffer+7 swap a rlc a mov gvBuffer+7,a ConvertEMcode_Loop3: mov r1,#gvUserBuf mov r3,#00h mov r5,#05h ConvertEMcode_Loop31: mov a,@r1 swap a anl a,#0fh xrl a,r3 mov r3,a mov a,@r1 anl a,#0fh xrl a,r3 mov r3,a inc r1 djnz r5,ConvertEMcode_Loop31 ; get colume parity clr c ; last bit is zero mov a,r3 rlc a mov r3,a ; get last parity mov a,@r0 anl a,#11100000b orl a,r3 mov @r0,a ret#include <stdio.h> typedef unsigned char uint8_t; uint8_t col_check(const uint8_t* buffer) { uint8_t check; uint8_t temp; check = *buffer++; check ^= *buffer++; check ^= *buffer++; check ^= *buffer++; check ^= *buffer; check ^= check >> 4; temp = check ^ (check >> 2); temp ^= temp >> 1; return ((check & 0x0f) << 1) | (temp & 0x01); } void code_create(const uint8_t* buffer, uint8_t* code) { uint8_t check_col; uint8_t check_row; uint8_t temp; check_col = col_check(buffer); *code++ = 0xff; temp = *buffer++; check_row = temp ^ (temp >> 2); check_row ^= (check_row >> 1); *code = 0x80; // fill 1 *code |= temp >> 1; // fill 4 *code &= 0xf8; // all 5 *code |= (check_row & 0x10) >> 2; // fill 1 *code++ |= (temp >> 2) & 0x03; // fill 2 *code = temp << 6; // fill 2 *code |= (check_row & 0x01) << 5; // fill 1 temp = *buffer++; check_row = temp ^ (temp >> 2); check_row ^= (check_row >> 1); *code |= temp >> 3; // fill 4 *code &= 0xfe; // all 7 *code++ |= (check_row & 0x10) >> 4; // fill 8 *code = temp << 4; // fill 4 *code |= (check_row & 0x01) << 3; // fill 1 temp = *buffer++; check_row = temp ^ (temp >> 2); check_row ^= (check_row >> 1); *code++ |= temp >> 5; // fill 3 *code = temp << 3; // fill 1 *code &= 0x80; // all 1 *code |= (check_row & 0x10) << 2; // fill 1 *code |= (temp & 0x0f) << 2; // fill 4 *code &= 0xfc; // all 6 *code |= (check_row & 0x01) << 1; // fill 1 temp = *buffer++; check_row = temp ^ (temp >> 2); check_row ^= (check_row >> 1); *code++ |= temp >> 7; // fill 1 *code = temp << 1; // fill 3 *code &= 0xe0; // all 3 *code |= check_row & 0x10; // fill 1 *code++ |= temp & 0x0f; // fill 4 *code = (check_row & 0x01) << 7; // fill 1 temp = *buffer++; check_row = temp ^ (temp >> 2); check_row ^= (check_row >> 1); *code |= temp >> 1; // fill 4 *code &= 0xf8; // all 5 *code |= (check_row & 0x10) >> 2; // fill 1 *code++ |= (temp >> 2) & 0x03; // fill 2 *code = temp << 6; // fill 2 *code |= (check_row & 0x01) << 5; // fill 1 *code |= check_col; } int main(int argc, char *argv[]) { uint8_t buffer[5] = {0x30, 0x00, 0xf9, 0x49, 0x89}; uint8_t res[8]; code_create((const uint8_t*)buffer, (uint8_t*)res); printf("0x%02x\n", res[0]); printf("0x%02x\n", res[1]); printf("0x%02x\n", res[2]); printf("0x%02x\n", res[3]); printf("0x%02x\n", res[4]); printf("0x%02x\n", res[5]); printf("0x%02x\n", res[6]); printf("0x%02x\n", res[7]); return 0; } 复制代码转载
|