Modbus spec 32-bits register access

  • Thread starter Frédéric Parent
  • Start date
F

Thread Starter

Frédéric Parent

We are developing a device that support Modbus protocol.

We have 32-bits register can be accessed in read/write by Read holding register and Write multiple register commands with a 2 registers length argument.

My question is what happen if read/write only a 16bits portion of this 32-bits register? I should process the ModBus request by send and "access violation" exception or the request is legal and I must process the 16-bits operation?

Thanks!
Fred
 
B

Bob Peterson

Modbus thinks that all registers are 16 bits. so if someone sends a request for just one of the two registers that makes up the 32 bits in your device,you probably should respond with just the 16 bits it is asking for. that way someone that wanted to could read the 32 bits in two separate reads if they wanted to and it would come out correct.

--
Bob
http://ilbob.blogspot.com/
 
L

Lynn August Linse

One sees both behaviors - for most PLC one can read 1/2 a float and never be any wiser. Other devices force reading on proper boundry, so a read offset of 50 and 52 might work, but 51 or 53 causes a simple 0x02 data exception.

I'd also suggest the following:

1) many masters cannot 'mix' 32-bit boundaries, so having 4 values like:

A) 32-bit, 16-bit, 32-bit, 16-bit is huge problem and they would need to read as 2 polls

B) 32-bit, 16-bit, 16-bit, 32-bit is fine, so padding might be needed to keep all 32-bits aligned. Grouping 32-bits togather also helps.

2) Most 3rd party devices assume first register is high word, but most older Modicon (plus the old I/O Scanner) assume first word is low. You might want a flag or setting to swap between these 2 modes (many call the later mode 'Modicon compatibility mode')
 
D

David Wilson

Copying 16 bits at a time of a 32 bit value that may be changing can have unintended results. If you cannot read and/or write 32 bits at a time with a communication device or protocol, separate the communicated registers from the registers in the user program. Have the source device or PLC write the 32 bit value once, then set a bit to have the source let the destination know when it is OK to copy the value. The value is copied 16 bits at a time (as 16 bit registers) to the destination, then move the re-constructed 32 bit number to the user program at the destination and set a bit to let the source know that the copy is complete. Obviously, this takes longer than just one communications transmission.

Also, some devices or protocols might do a byte or word swap. Make sure that the pieces of the longer word end up in the correct order.
 
L

Lynn August Linse

Dave brings up a good point I'd overlooked - allowing a user to read 1/2 of a float a few msec apart is probably a very danergous idea. One has no promise the first 1/2 matches the second 1/2, as many slave-devcies update their I/O multiple times per second. (a fake example - suppose I read the digits '1' + '9' in 2 pieces, but it changes to '2' + '0' in between ... my client might mistake the result as '10' or '29', both wrong.

I won't bother with the caching he suggests ... a client/master which can only read 1 x 16-bit word at a time is so crippled & rare, that it would be best to prevent (or block) such usage.

So I'd say in hindsight, the product design should force reading either both words in one-shot, or neither.
 
Top