Home  |  Contents 

Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: Modbus CRC. Post ReplyPost New Topic
<< Prev Page of 2
Author
Message << Prev Topic | Next Topic >>
Turbo46
Newbie
Newbie


Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 25
Posted: 10 April 2018 at 6:05pm | IP Logged Quote Turbo46

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



Back to Top View Turbo46's Profile Search for other posts by Turbo46
 
Phil23
Guru
Guru


Joined: 27 March 2016
Location: Australia
Online Status: Offline
Posts: 1379
Posted: 15 April 2018 at 9:08am | IP Logged Quote Phil23

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


Back to Top View Phil23's Profile Search for other posts by Phil23
 
goc30
Newbie
Newbie
Avatar

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 34
Posted: 15 April 2018 at 1:44pm | IP Logged Quote goc30

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
Back to Top View goc30's Profile Search for other posts by goc30
 
Turbo46
Newbie
Newbie


Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 25
Posted: 15 April 2018 at 3:05pm | IP Logged Quote Turbo46

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
Back to Top View Turbo46's Profile Search for other posts by Turbo46
 
goc30
Newbie
Newbie
Avatar

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 34
Posted: 16 April 2018 at 12:32am | IP Logged Quote goc30

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
Back to Top View goc30's Profile Search for other posts by goc30
 
Turbo46
Newbie
Newbie


Joined: 24 December 2017
Location: Australia
Online Status: Offline
Posts: 25
Posted: 16 April 2018 at 8:23am | IP Logged Quote Turbo46

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

Back to Top View Turbo46's Profile Search for other posts by Turbo46
 
goc30
Newbie
Newbie
Avatar

Joined: 12 April 2017
Location: France
Online Status: Offline
Posts: 34
Posted: 16 April 2018 at 4:22pm | IP Logged Quote goc30

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
Back to Top View goc30's Profile Search for other posts by goc30
 


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

  Search the forums using Google.
<< Prev Page of 2
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.1523 seconds.
Privacy Policy     Process times : 0, 0, 0, 0.15