0
\$\begingroup\$

I am sending/receiving data using UART between STM32G4 board and other board. While I am sending the data I am adding the CRC and transmitting it through UART to other board. While receiving how do I check the CRC correct or not. For Eg: From other board to my STM32G4 board data frame is as below

SOF 0x32

Length 0x05

Command 0x14

Data 0x00/0x01/0x02

CRC CRC EOF 0x05

how do I check the CRC is correct or not in the coding? Do I need to calculate crc for all 3 possible data and compare the possible crc with received crc?is this correct way. Can anyone suggest how to check the CRC.

Thanks

\$\endgroup\$
5
  • \$\begingroup\$ There have been a number of questions & discussions about how to use CRCs on this site recently. Have you used the tag feature to look for CRC questions? \$\endgroup\$
    – SteveSh
    Commented Nov 2, 2023 at 12:08
  • \$\begingroup\$ What is "three possible data"? you get sent exactly one data. \$\endgroup\$ Commented Nov 2, 2023 at 12:30
  • \$\begingroup\$ I think OP was talking about the 3 data bytes. \$\endgroup\$
    – SteveSh
    Commented Nov 2, 2023 at 12:32
  • \$\begingroup\$ Either way, what is incorporated in the CRC must be specified by the protocol, we can't guess that. \$\endgroup\$ Commented Nov 2, 2023 at 12:46
  • \$\begingroup\$ Sorry I forget to mention CRC calculation will done for length,command& data bytes. Three possible data means for data byte I may receive 3 possible values as mentioned (0x00/0x01/0x02). \$\endgroup\$
    – Sireesha
    Commented Nov 3, 2023 at 4:30

2 Answers 2

1
\$\begingroup\$

Well you don't say if you use the UART hardware CRC or some software calculated CRC and which exact CRC algorithm, but it does not matter.

When you send a packet you calculate CRC and send it.

When you receive a packet, you have two options which one may make more sense than the other.

You receive the data packet and calculate the CRC for received data, then you compare the calculated CRC matches the received CRC. This may be the simplest option.

Another way is to calculate received CRC over received data and CRC. The result of calculation usually causes the CRC to cancel itself out so you get a value of zero. Or depending on how the calculations are done, it may be arranged to be something else which is known but not zero.

\$\endgroup\$
0
0
\$\begingroup\$

We can't tell you: Which parts of a packet are subject to a checksum is specified in the protocol, and there's no way for us to know that.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.