Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:25 01 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Checksum and CRC

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:41pm 05 Apr 2016
Copy link to clipboard 
Print this post

Sorry for posting so much but I need your help again, and I am very grateful for the help I have received so far.

HOW do I create a Checksum figure?

THEN how do I do a CRC to check if that Checksum is correct?

An example of how I want to use it -
A micromite in the Fridge just sending the temperature once every 15 minutes
A micromite in 2 seperate Freezers sending the temperature once every 15 minutes


This is how I envisage the Fridge Micromite program

OPTION BREAK 0
OPTION AUTORUN ON
WATCHDOG 86400 'Reset the Micromite once a day
OPEN "COM1:9600" AS #1 'OPEN port
PRINT #1 "FRIDGE: " TEMPR(pin)"C" checksum 'Send data via HC-12
IF #1="1" then 'Check for "1" to prove the data was recieved correctly
ELSE
PRINT #1 "Fridge" checksum 'Resend the data and check it was recieved correctly
ENDIF
CLOSE #1
CPU SLEEP 900

*** The same program would be on the Freezer Micromites but obviously they would send Freezer 1 or Freezer 2 instead of Fridge***


On the Micromite that monitors the temperatures, it will be doing other things but when it gets an interupt from COM2 it will then process the data coming from the Fridge/freezers and display the temperature

This is how I see this section of program


IF #2= "Fridge" and CRC Is correct then 'Data coming from Fridge Sensor **NO idea how to check Checksum is correct**
PRINT #2 "1" 'COM2 saying the data is correct
ELSE
PRINT #2 "2" 'CRC is incorrect Please resend
IF #2= "FREEZER 1" and CRC Is correct then 'Data coming from Freezer Sensor
PRINT #2 "1" 'COM2 saying the data is correct
ELSE
PRINT #2 "2" 'CRC is incorrect Please resend
IF #2= "FREEZER 2" and CRC Is correct then 'Data coming from Freezer Sensor
PRINT #2 "1" 'COM2 saying the data is correct
ELSE
PRINT #2 "2" 'CRC is incorrect Please resend
ENDIF
PRINT the information from #2 'NOT SURE HOW TO DO THIS



I am totally blank on how to create and check the checksum on both micromites

Can anyone help with correcting what I have above and show me how to create and check a checksum? It's not in the manual I've read both the basic and Micromite+ manuals
Edited by lew247 2016-04-07
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 12:14am 06 Apr 2016
Copy link to clipboard 
Print this post

A quick Google of "checksum" throws this up as the second hit.

Greg
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1114
Posted: 12:36am 06 Apr 2016
Copy link to clipboard 
Print this post

Lew,

CRC (Cyclic Redundancy Check) and checksum are basically the same. Generally, they are the sum of the bytes in the message - try Googleing CRC for a more technical description - in order to determine if there has been an error.

Reading the HC-12 doco by Robert Rozee from the Backshed (search the Backshed for HC-12) it seems that the HC-12 does not generate or check CRC by default. So, if you want to have some form of checksum, you would need to write your own CRC generator and then checker. In order to know what is data and what is CRC in the data stream, you would need a set length for data (ie. a set number of bytes ) so you know when the CRC is.

Regards,
Doug.
Edit: Do a search in the Backshed - there are several code examples of CRC checking.Edited by panky 2016-04-07
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6266
Posted: 12:57pm 06 Apr 2016
Copy link to clipboard 
Print this post

Lew,
To expand on the code in another post,

DO
' sending attaches the checksum to the string
INPUT txt$
sum = chksum(txt$)
txt$ = txt$+CHR$(sum)
PRINT "Sending: ";sum,txt$
'receiving tests the checksum. A zero indicates that all is well
if chksum(txt$) = 0 then
txt$ = left$(txt$,len(txt$)-1)
else
txt$= "error!!!"
endif
print "Received: ";txt$

LOOP


FUNCTION chksum(report$)
LOCAL n, ch$
chksum=0
FOR n = 1 TO LEN(report$)
ch$=MID$(report$,n,1)
chksum=chksum XOR ASC(ch$)
NEXT n
chksum = chksum MOD 256
'print chksum
END FUNCTION

Anything you are going to send, add the checksum character to the end.
Anything you are receiving, check the checksum of the 'string including the checksum'.
If it is zero, the string is intact and simply remove the last character (the checksum)

What you do with a bad checksum is up to you.
If it's not critical, print/log the error and carry on.
Otherwise, send a reply requesting a resend.
One alternative is to always send and acknowledgement and the sender resends if it doesn't receive the ACK

There are lots of methods and it depends on your overall design.

For basic environment logging, I would stick with the log the error and carry on approach.

The checksum method I have shown you finds a single error. It is possible that two errors in the one string will give the same checksum. There are other more complex checksum/CRC methods that find more errors and even correct them but the overheads are higher.

Jim



VK7JH
MMedit
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 10:44pm 06 Apr 2016
Copy link to clipboard 
Print this post

Thanks Jim
 
Greg Fordyce
Senior Member

Joined: 16/09/2011
Location: United Kingdom
Posts: 153
Posted: 01:10am 07 Apr 2016
Copy link to clipboard 
Print this post

Here's a good app note from Maxim explaining CRC checking

Maxim app note 3749
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025