3/27/2023 0 Comments Modbus rtu checksum calculatorI note that your screenshot for the "CRC-16 Pre-defined polynomial" shows "Data Shift Direction" shift lef t. If your code gets these values, it ain't modbus. Bottom line: I disagree with your posted "Expected Result" modbus values. Expected Result : 0xF4F0 The on-line calculators and the code from my exmple all give 0xCEFFįor this one we get 0x7204. See Sunshine1.png I used this for my reference: So I gotta say: Huh? Modbus CRC value from your link and code generated from the MCC setup I showed in my previous post is 0xDD6C. Expected Result : 0圎B6F The subject of this thread is "Calculating modbus CRC," and I would think that people coming across this post expect modbus results. Meanilkp //Example1 with 2 data inputs 0圆C,0x93 While (CRC_8BitDataWrite(0xC0) = 0) // Write third byte While (CRC_8BitDataWrite(0xB0) = 0) // Write second byte While (CRC_8BitDataWrite(0xA0) = 0) // Write first byte Example3 with 3 data inputs 0xA0, 0xB0, 0xC0 While (CRC_8BitDataWrite(0x29) = 0) // Write second byte While (CRC_8BitDataWrite(0x56) = 0) // Write first byte While (CRC_8BitDataWrite(0x93) = 0) // Write second byteĬrcResult = CRC_CalculatedResultGet(false, 0x0) While (CRC_8BitDataWrite(0圆C) = 0) // Write first byte Please find the attachment for MCC CRC configuration snapshot and example code files Link for CRC calculation: uint16_t crcResult Note that in each case I retrieved the "raw" CRC accumulator contents just for comparison, but the real Modbus CRC value was obtained from CRC_CalculatedResultGet(). Test 2 with CRC bytes appended to the original message: Test 1 with CRC bytes appended to the original message: Compiled on at 06:59:42 UTC by XC8 version 2000 Outputs from my PIC18F47K40 evaluation circuit. Nowadays, it's easy, once you get the hang of it. Older MCUs from Microchip did not handle odd numbers of bytes working into 16-bit CRC calculations. Note that it is very important to have sequences with odd numbers of bytes and sequences with even number of bytes. (Of course you call CRC_Initialize() and CRC_Start() before each message-processing sequence.) Here's a couple of tests from my evaluation program. Then you use CRC_CalculatedResultGet() with the first argument set to the #defined constant REVERSE to get your 16-bit CRC. All you have to do is put that into a loop that steps through a buffer in your test program that contains the message. (MPLABX version 5.05, MCC plugin version 3.65.1) Now, the mcc.h header file gives a suggestion on the proper way to use the mcc-generated CRC_8bitDataWrite() function to write a single byte to the CRC module. If there are no errors, the result will be zero.The attachment shows my MCC setup for the CRC module. If you are validating a received message, just process all of the bytes, including the two CRC bytes through the CRC module.(Less Significant Byte first, then More Significant Byte.) If you are generating a CRC for a transmitted message, you append the two bytes of the CRC to the message sequence in little-endian fashion.Fortunately, the MCC guys gave us the ability to retrieve the bit-reversed value of the Accumulator contents without making us deal explicitly with a separate function in our flow. The final contents of the 16-bit CRC Accumulator must be bit-reversed to get the actual Modbus CRC. The Modbus Polynomial is 0xA001, but to calculate the Modbus CRC in the 'K40 you have to use the bit-reversed value 0x8005.Everything inside the CRC module is in reverse order, but the bits aren't switched from one end to the other.The CRC module on the 'K40 has the ability to reverse the input bytes so that you don't have to massage your input data.Modbus CRC is calculated a byte at a time from bytes that are shifted LSB first.It may or may not be useful as a starting point for other devices. These are the two that I have tested most recently, and everything I will say here can be backed up with code and test results. Secondly, you have to realize that the CRC modules for recent MCUs (like the 'K40 and 'K42) are much improved over older ones, but they still take a "little getting used to." For people using devices other than the 'K40 and 'K42, there may be some differences. I am using Mplab x ide v5.05 and compiler xc8 v2.0įirst of all, I'm assuming you can find some examples that use actual Modbus data sequences so that you can test.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |