Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 09:04 17 May 2024 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 : Xmodem CRC

Author Message
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1595
Posted: 03:04am 01 Dec 2018
Copy link to clipboard 
Print this post

In another post I suggested that it may be possible to include an Xmodem CRC function into MMBasic. I don't expect that to happen so I would like to present one here:

' Xmoden CRC as String
'
' To Use:
' Assemble a message as a string (eg: TxMsg$)
' Add the CRC to the message by:
'
' TxMsg$ = TxMsg$ + XCRC$(TxMsg)
'
' and transmit it.
'
' To test a recieved message (eg: RxMsg$) for a correct CRC
' and that it is not otherwise corrupted use:
'
' IF XCRC$(RxMsg$) = CHR$(0) + CHR$(0) THEN ' CRC is OK

FUNCTION XCRC$(a$)
LOCAL CRC% = &H0000, n%, j%
FOR n% = 1 TO LEN(A$)
CRC% = (CRC% AND &HFFFF) XOR (ASC(MID$(a$, n%, 1)) << 8)
FOR j% = 1 TO 8
IF (CRC% AND &H8000) <> 0 THEN
CRC% = (CRC% << 1) XOR &H1021
ELSE
CRC% = CRC% << 1
END IF
NEXT j%
NEXT n%
XCRC$ = CHR$(CRC% >> 8 AND &HFF) + CHR$(CRC% AND &HFF)
END FUNCTION

The use is explained in the comments and is it used exactly the same way as the Modbus CRC functions presented elsewhere. It could be used with the implementation of an Xmodem transfer program.

Xmodem is usually used with blocks of data with consisting of a header, data and CRC and expects an ACK (acknowledge) after each block. This function doesn't care about that - it will provide the Xmodem CRC for whatever message you feed to it and (like the Modbus CRC) can be used for any application where messages are transmitted between devices.

For Xmodem and Modbus applications the correct CRC must be used but either could be used on a home-made message structure to improve security.

Bill
Keep safe. Live long and prosper.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 12:57pm 02 Dec 2018
Copy link to clipboard 
Print this post

Turbo46, this is very cool. I am working on a micromite project that uses RS485 to transmit to up to 16 other micromite devices with addressing (one way comms). I think this would be a good way to do a quick check to see if data is always good. I am using simple stereo audio cables with 3.5mm stereo audio plugs (A,B and GND) and seems to work quite well at 9600bps. Tested with daisy chained connections with over 1000 combined feet of cable. I know this is not the best cable to use for RS485, but with the last unit terminated with a 100 ohm resistor, it seems to work nicely, and the cables are cheap, very flexible and readily available.

Using your program, I can transmit a bunch of data and check if I am getting errors. This will also allow me to crank up the speed to see what the limitations of my setup are. Thanks for the effort!
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1595
Posted: 01:24am 03 Dec 2018
Copy link to clipboard 
Print this post

Thanks viscomjim,

I'm very pleased that you are going to use a CRC. If you really don't want two way comms with the remote sending an 'Ack' response then it will give you an extra level of security.

You could think about it though, the 'Ack' (say slave address + CRC) along with a timeout in the master would confirm that the slave DID receive the message correctly. Please have a look here for a similar discussion.

If you can find out the characteristic impedance of your cable you could use a resistor of that value to terminate the cable - you may be able to use a higher speed if that's what you want.

Bill
Keep safe. Live long and prosper.
 
Print this page


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

© JAQ Software 2024