Email
 Microcontroller and PC projects Forum Index : Microcontroller and PC projects         Section -- Select Forum -- Off grid living and renewables   - Windmills   - Solar   - EV's Electronics and Microcontrollers   - Electronics   - Microcontroller and PC projects   - PCB Manufacturing Miscellaneous   - Other Stuff   - Site News   - Off topic archive. Classifieds   - Price Watch   - Wanted   - For Sale Guest Book   - Guest Book
 Topic: Modbus CRC.
 << Prev Page 1 2 of 2
Author
 Message << Prev Topic | Next Topic >>
Turbo46
Newbie

Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 33
 Posted: 10 April 2018 at 6:05pm | IP Logged
Previously I wrote:

Quote:
 I believe that if you receive a message, then process the whole message (CRC included) with the CRC generation function, the resultant 'CRC' will be zero. That would be a simpler way of checking the validity of the message. I'm yet to try that though.

Well I have tried it with the examples in Jim's CRC calculation function and with some other real world examples and it does work eg:

test1\$ = CHR\$(01)+CHR\$(03)+CHR\$(04)+CHR\$(&H3F)+CHR\$(&H80)+CHR\$(0)+CHR\$(0)
check1\$= CHR\$(&HF7)+CHR\$(&HCF)
test1\$ = test1\$+check1\$

PRINT HEX\$(CRC(test1\$),4)

will return zero (0000)

Bill

Edited by Turbo46 on 10 April 2018 at 6:05pm

Phil23
Guru

Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1509
 Posted: 15 April 2018 at 9:08am | IP Logged
Wondering if anyone can see fault in this variation.

It returns a 2 character string that I can add to the string I'm building to sent.

Appears to test Ok, and I assume from above if I pass a response string to it, it should return "String"=Chr\$(0)+Chr\$(0).

Thanks

Phil.

PS, I am now successfully reading the first Six registers with a single request & converting them successfully.

They all reside in the address range from 30001 to 30032.

Really need a second request for the others as they are up at 30071 to 30080.
If I tried to get them all with one request the response string would be too long.

Quote:
 FUNCTION CrcStr(a\$) As String LOCAL ErrorWord% = &HFFFF, n, j, ByteVal, LSB LOCAL CrcHex As Integer FOR n = 1 TO LEN(A\$) ByteVal = ASC(MID\$(a\$, n, 1)) ErrorWord% = (ErrorWord% AND &HFFFF) XOR ASC(MID\$(a\$, n, 1)) FOR j = 1 TO 8 LSB = ErrorWord% AND &H0001 IF LSB = 1 THEN ErrorWord% = ErrorWord% - 1 ErrorWord% = ErrorWord% / 2 IF LSB = 1 THEN ErrorWord% = ErrorWord% XOR &HA001 NEXT j NEXT n CrcHex = ErrorWord% AND &HFFFF CrcStr=Chr\$(CrcHex AND &hFF)+Chr\$(CrcHex>>8)END FUNCTION

goc30
Regular Member

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 94
 Posted: 15 April 2018 at 1:44pm | IP Logged
Hi Phil

In the MODBUS protocol, the frames are limited to 128 bytes, including crc, which means that if a register is 4 bytes long, the slave can only send 30 registers values

Turbo46
Newbie

Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 33
 Posted: 15 April 2018 at 3:05pm | IP Logged
Hi Phil

I think it does work but you are ending up with a null string.
CHR\$(0) + CHR\$(0) = CHR\$(0) = nothing.

Bill

Edit: From what I have found the maximum Modbus frame size is 256 bytes.

Edited by Turbo46 on 15 April 2018 at 3:23pm

goc30
Regular Member

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 94
 Posted: 16 April 2018 at 12:32am | IP Logged
hi Turbo46

Edit: From what I have found the maximum Modbus frame size is 256 bytes.

in wikipedia for modbus they say:

Because register values are 2-bytes wide and only 127 bytes worth of values can be sent, only 63 holding registers can be preset/written at once

Wikipedia

Edited by goc30 on 16 April 2018 at 12:34am

Turbo46
Newbie

Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 33
 Posted: 16 April 2018 at 8:23am | IP Logged
Hi goc30

You may well be right! I have only had experience with small RTUs. My reference though is the 'Modicon Modbus Protocol Reference Guide' Here. Check out page 58 where it states:

'The quantity of registers to be read, combined with all other fields in the expected response, must not exceed the allowable length of Modbus messages: 256 bytes.'

Cheers
Bill

goc30
Regular Member

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 94
 Posted: 16 April 2018 at 4:22pm | IP Logged
Hi Turbo46

The MODBUS protocol is open, you can create your own function codes (I created some for clients). The 128-byte limit must have a historical reason (maybe for slow speed reasons)

Modicon use a partial modbus protocol (not 100% compatible, and many functions no-standards) but it is not important. They use also an ascii communication, for this they need more bytes maybe

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register