Sign in to follow this  
Followers 0
AliBahar

CRC generation problem

3 posts in this topic

Hi friends for DNP3 protocol , crc code is generated by the following algorithm: • Start with user data block M of k bits • Multiply by 65536 (ie append 16 zeros to end to form k + 16-bit number) • Divide by generator polynomial P to obtain quotient Q and remainder R modulo-2 division is used) • Discard Q, keep R • Invert R to obtain R? • Append R? to M forming message T? to be transmitted Where generator polynomial: P = x16 + x13 + x12 + x10 + x8 + x5 + x2 + 1 This forms a 17-bit number which is 13D65 (in hex) I have created the following c code for crc calculation using modulo-2 division method: #include "stdio.h"typedef unsigned int uint;typedef unsigned char uchar;typedef unsigned long int UL;#define WIDTH (8 * sizeof(UL))#define TOPBIT (1 << (WIDTH - 1))UL POLYNOMIAL=0x3D650000;uint remainder = 0;uint crcGener(uint *message, uchar nSize){ uchar ff, bit; UL Rem; for (ff = 0; ff < nSize; ff++){ Rem ^=(message[ff]<<16); //printf("%x\n",Rem); for (bit = 0; bit <16; ++bit) { if (Rem & TOPBIT) { Rem = (Rem << 1) ^ POLYNOMIAL; } else { Rem = (Rem << 1); } } } /* * The final remainder is the CRC result. */ return (Rem);} void main(void){ uint Msg[5]={0x0564,0x0500,0x0C00,0x0100,0x0000}; remainder=crcGener(Msg, 5); printf("%x\n",remainder); getchar();}but the result is different from expected value

Share this post


Link to post
Share on other sites
are you sure it is 13D65 ?

Share this post


Link to post
Share on other sites
yes. reference: G. Clarke and D. Reynders, "Practical Modern SCADA Protocols: DNP3, 60870.5 and Related Systems", page:97

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0