Jump to content

Recommended Posts

Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...