Today, when I tinker with a handheld operator, I have a little problem and record today's experience pack
Because before the company's products in the calibration is basically and check, today in preparing to use C # to simulate an antique operator, but encountered a problem, the simulator issued data, the motherboard will not reply, compared to the communication protocol has not found any problems. Because the document is not complete, just know the communication format, compared to think it should be a check out the problem. Because CRC check is the most commonly used in the field of data communication verification method, asked a few old guys to know that this four-byte ASCII checksum should be Crc16-ccitt generated, and then go to the University of Idiot for a long time did not understand the details of CRC check.
How the specific CRC is generated I don't elaborate, the key point is "generate polynomial" and initial value.
The generative polynomial of Crc16-ccitt is 0x1021;
Not much to say, provide code Crc16-ccitt class
Public classCrc16ccitt { Public enumInitialcrcvalue {zeros, NonZero1 =0xFFFF, NonZero2 =0x1d0f } Const ushortPoly =4129; ushort[] table =New ushort[ the]; ushortInitialValue =0; PublicCrc16ccitt (Initialcrcvalue initialvalue) { This. InitialValue = (ushort) InitialValue; ushorttemp, A; for(inti =0; I < table. Length; ++i) {temp=0; A= (ushort) (I <<8); for(intj =0; J <8; ++j) {if((temp ^ a) &0x8000) !=0) {Temp= (ushort) (Temp <<1) ^Poly); } Else{Temp<<=1; } A<<=1; } Table[i]=temp; } } Public ushortComputechecksum (byte[] bytes) { ushortCRC = This. InitialValue; for(inti =0; I < bytes. Length; ++i) {CRC= (ushort) (CRC <<8) ^ table[(CRC >>8) ^ (0xFF&bytes[i])); } returnCRC; } Public ushortComputechecksum (list<byte>listtemp) { byte[] bytes =listtobytes (listtemp); ushortCRC = This. InitialValue; for(inti =0; I < bytes. Length; ++i) {CRC= (ushort) (CRC <<8) ^ table[(CRC >>8) ^ (0xFF&bytes[i])); } returnCRC; } Public byte[] Computechecksumbytes (byte[] bytes) { ushortCRC =computechecksum (bytes); returnBitconverter.getbytes (CRC); } Public byte[] Listtobytes (list<byte>listtemp) { intLength =Listtemp.count (); byte[] bytes =New byte[length]; for(inti =0; i < length; i++) {Bytes[i]=Listtemp[i]; } returnbytes; } }
Finally, please call me red scarf
A little comprehension of C # CRC check