Skip to main content
Lchal.1
Associate III
October 16, 2020
Question

hi, i am fresher to this stm32 , i am doing project on modbus but i am not able to calculate crc value and i am not getting what i need to add to my code to calculate crc in modbus . can anyone help me its urgent. Thank you

  • October 16, 2020
  • 2 replies
  • 823 views

..

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
October 16, 2020

I posted some examples in the past, the search is pretty hopeless here

This is an old one, gives a couple of working examples of MODBUS CRC computation

https://community.st.com/s/contentdocument/0690X0000060JWXQA2

https://community.st.com/s/question/0D50X00009XkgXtSAJ/programmable-crc-peripheral-in-newer-stm32-devices

https://community.st.com/s/question/0D50X00009XkbNMSAZ/crc-computation

// sourcer32@gmail.com
// 16-Bit CRC of Right Shifting MODBUS Polynomial 0xA001
 
#include <windows.h>
#include <stdio.h>
 
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
 
uint16_t Crc16Modbus(uint16_t Crc, uint8_t Data)
{
 int i;
 Crc = Crc ^ Data;
 for(i=0; i<8; i++)
 if (Crc & 0x0001)
 Crc = (Crc >> 1) ^ 0xA001; // Polynomial used by MODBUS
 else
 Crc = (Crc >> 1); // Right shifting algorithm
 return(Crc);
}
 
int main(int argc, char **argv)
{
 uint8_t TestData[] = { 0x01,0x04,0x75,0x54,0x00,0x02 };
 uint16_t crc;
 int i;
 crc = 0xFFFF; // Initial state
 for(i=0; i<sizeof(TestData); i++)
 crc = Crc16Modbus(crc, TestData[i]);
 printf("CRC = %04X\n", crc);
 return(1);
}

http://www.catb.org/~esr/faqs/smart-questions.html#urgent

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
October 16, 2020

Which STM32?

What did you do?

JW

Tesla DeLorean
Guru
October 16, 2020

Based on recent postings stm32f103, so needs to be done in software.

Faster methods described in one of attached posts.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Lchal.1
Lchal.1Author
Associate III
October 17, 2020

Thank you