cancel
Showing results for 
Search instead for 
Did you mean: 

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

Lchal.1
Associate II
 
4 REPLIES 4

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 Venmo Up vote any posts that you find helpful, it shows what's working..

Which STM32?

What did you do?

JW

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 Venmo Up vote any posts that you find helpful, it shows what's working..

Thank you