# Proper mapping of registers to coils

T

#### Timmy Brolin

Let's say I have theese variables mapped to the beginning of the holding register area:

reg no
0x0000 [15.......a.....0] 16bit word
0x0001 [15.......b.....0] 16bit word
0x0002 [7..c..0][7..d..0] Array of four bytes
0x0003 [7..e..0][7..f..0]
0x0004 [31......g.....16] 32bit dword
0x0005 [15......g......0]

What I want is to make all theese variables available in the coils area as well.
Problem is, the holding register area is defined to be big endian, while the coil area is little endian by nature.
If I simply let the 12 most significant bits of the coil number represent the holding register, and the 4 least significant bits address the bits within the word, (bit 0-15), then the translation of register addresses to coil numbers becomes very easy, but the mapping would look as follows:

coil number
0x00-0x0F : word a, bit 0-15
0x10-0x1F : word b, bit 0-15
0x20-0x27 : byte d, bit 0-7
0x28-0x2F : byte c, bit 0-7
0x30-0x37 : byte f, bit 0-7
0x38-0x3F : byte e, bit 0-7
0x40-0x4F : dword g, bit 16-31
0x50-0x5F : dword g, bit 0-15

As can be seen, 16bit words are fine, but the order of arrays of bytes are byte swapped, and dwords are word-swapped. A "read coils" command reading coils 0x00-0x5F to PLC memory will however result in the same order of data in the PLC memory when compared to the equivalent "read holding registers 0x00-0x05" command. (at least on a modicon PLC).

The second alternative is to take into consideration the data types when translating coil numbers to register addresses:

coil number
0x00-0x0F : word a, bit 0-15
0x10-0x1F : word b, bit 0-15
0x20-0x27 : byte c, bit 0-7
0x28-0x2F : byte d, bit 0-7
0x30-0x37 : byte e, bit 0-7
0x38-0x3F : byte f, bit 0-7
0x40-0x4F : dword g, bit 0-15
0x50-0x5F : dword g, bit 16-31

This coil-number ordering makes a little more sense to humans, but the translation from coil-numbers to register addresses become non-linear and non-trivial to both humans and computers, and a read coils command reading coils 0x00-0x5F will result in the byte array as well as the dword beeing byte-swapped in the PLC's memory (at least on a modicon PLC).

What is the "standard" solution for mapping the register area to the coil area? Right now I'am favoring the first solution, since it is fairly logical once you tell the customers that the coil commands consider all data to be 16bit words.

Regards,
Timmy Brolin

F

#### Friedrich Haase

Moin Mr. Brolin,
moin all,

"Coil area is little endian by nature"? Are you sure? I think coils are big
endian by definition too. It remains up to you to swap bytes within the
protocol if your hardware is little endian.

regards
Friedrich Haase

Ing.-Büro Dr. Friedrich Haase
Consulting - Automatisierungstechnik
email [email protected]
WEB http://www.61131.com

T

#### Timmy Brolin

From the modbus specification, page 12:
"The LSB of the first data byte contains the output addressed in the query (the first coil). the other coils follow toward the high order end of this byte, and from low order to high order in subsequent bytes"

So yes, coils are very much little endian.

Coils could be considered big endian only if bit 15 of a word is considered to be the "first coil" and bit 0 is considered to be the "last coil", but this would not make much sense, and it would result in all data ending up bit-swapped in the PLC memory.

The endianness of my hardware is not really relevant to this problem, I swap the endianness of the data any way I need.

Regards,
Timmy Brolin

A

#### Automation Linse

Yes, I can confirm Timmy Brolin's comment since my code has to bridge between Modbus and Rockwell protocols. Rockwell (DF1/PCCC) is LittleEndian - I need to swap bytes during word/register functions and must NOT swap during bit/coil functions to be get 0x00001 to properly map to say N7:0/0 instead of N7:0/8.

As to a solution - I need to think a bit about Timmy's options before I comment.

best regards
- Lynn A Linse, www.digi.com (Ethernet-to-Serial Experts)

F

#### Friedrich Haase

Moin Mr. Brolin,
moin all,

I never view the order of bits/coils as you did. Indeed with your
interpretations coils look very much like little endian.

regards
Friedrich Haase
Ing.-Büro Dr. Friedrich Haase
Consulting - Automatisierungstechnik
email [email protected]
WEB http://www.61131.com