1

Topic: Modbus TCP, byte order AND bit order

Kind time of days! Do not swear that again about , but I refuse to trust in . Docks-) If to begin with sources I write abstract  the master who on a kick from the user clings the specific protocol. Now I was engaged modbus tcp. Long struggled with  that it to force to receive synchronously packets and not to hang at a communication breakaway. But it is details. And here I reached implementation of the protocol and here miracles began. With Big Endian it is possible to reconcile, if clearly in which cases it is necessary to do a swap of bytes. And it is not so clear. But it after all not the most terrible. Terrible is a reverse order of bits in byte. And different simulators  return different results on identical requests. At me while such pattern was added: o If a variable (in both sides) is more than byte, it is necessary to change bytes on Big Endian and on the contrary after reception o If pack DI is accepted or ' coils - let there will be a contact ' - that length of a variable - 1 byte, and to change the order there is no place, but it is necessary to turn all packet of the data. o very strange piece at usage mod_RSsim. If to it to thrust in di to 0 address zero bit, in the answer 128. I.e. bits are turned, but somehow especially about it I did not find anything in docks. In Modbus Slave the such it is noted. You could not clear up these questions? Thanks! P.S. In the core I am guided MODBUS Application Protocol Specification V1.1

2

Re: Modbus TCP, byte order AND bit order

Hello, plastictown, you wrote: P> o very strange piece at usage mod_RSsim. If to it to thrust in di to 0 address zero bit, in the answer 128. I.e. bits are turned, but somehow especially about it I did not find anything in docks. In Modbus Slave the such it is noted. P> you could not clear up these questions? The first time the such I hear http://www.simplymodbus.ca/FC01.htm

3

Re: Modbus TCP, byte order AND bit order

Hello, kov_serg, you wrote: _> the First time the such I hear _> http://www.simplymodbus.ca/FC01.htm Well fine what to tell. The first time I see so indefinite protocol: The Modbus specification does not define exactly how the data is stored in the registers. Therefore, some manufacturers implemented modbus in their equipment to store and transmit the higher byte first followed by the lower byte. (AE before 41). Alternatively, others store and transmit the lower byte first (41 before AE). Similarly, when registers are combined to represent 32-bit data types, Some devices store the higher 16 bits (high word) in the first register and the remaining low word in the second (AE41 before 5652) while others do the opposite (5652 before AE41) If to accept as the fact that it is not necessary to touch bits, then there is one question: If the user asked 2 bytes to interchange the position of them, it is clear, and if 4 to overturn all 4 or to change word places? Well i.e. If to assume that always big endian, irrespective of the vendor of a piece of iron. Or the user of my client should give this information somehow?

4

Re: Modbus TCP, byte order AND bit order

Hello, plastictown, you wrote: P> If to accept as the fact that it is not necessary to touch bits, then there is one question: P> If the user asked 2 bytes to interchange the position of them, it is clear, and if 4 to overturn all 4 or to change word places? Well i.e. if to assume that always big endian, irrespective of the vendor of a piece of iron. Or the user of my client should give this information somehow? It is not necessary to change anything. Even register representation in uint16_t not the best idea because I should know is simple, in what order your library packs them, if I do not specify it explicitly. In a variant uint8_t [2] it would be obvious. For example, one register can be responsible for regulation of two parameters: the first in the first byte, the second in the second. (There is at me such device). You can any  do. Here at libmodbus there is a such: MODBUS_API float modbus_get_float_abcd (const uint16_t *src); MODBUS_API float modbus_get_float_dcba (const uint16_t *src); MODBUS_API float modbus_get_float_badc (const uint16_t *src); MODBUS_API float modbus_get_float_cdab (const uint16_t *src);

5

Re: Modbus TCP, byte order AND bit order

6

Re: Modbus TCP, byte order AND bit order

Hello, kov_serg, you wrote: _> it seems To me that you confuse something  and registers generally should not be connected _ in any way> is one-bit coils and discret inputs, and is 16 registers (which ro and rw-holding) _> communication of these of 4 entities to the protocol has no relation. I do not say that they are connected. The client can simply ask to produce me to it 4 registers, for example. To me 8 byte comes, and in what order the concepts given I lie I have no, besides, that is written to specifications that it is accepted to use big endian. It turns out, it is necessary to change bytes in each register separately. But proceeding from the citation earlier, it turns out that the word order also can be reverse. Well and plus to all all this amorphous  comes back to front. About the order of bits in byte, it is probable all the same a simulator something behaves in a queer way.

7

Re: Modbus TCP, byte order AND bit order

Hello, plastictown, you wrote: P> I do not say that they are connected. The client can simply ask to produce me to it 4 registers, for example. To me 8 byte comes, and in what order the concepts given I lie I have no, besides, that is written to specifications that it is accepted to use big endian. It turns out, it is necessary to change bytes in each register separately. But proceeding from the citation earlier, it turns out that the word order also can be reverse. Well and plus to all all this amorphous  comes back to front. The network order byte and 16 registers in modbus was always used. Into them packed 32 bit values, but that such value to read it would be necessary to read at once two registers or more, and here in what order values 32 bit lie and more values depends on the device, here a card of registers it is necessary to look. P> about the order of bits in byte, it is probable all the same a simulator something behaves in a queer way. Try another. https://habrahabr.ru/post/281430/http:/ … lator.org/

8

Re: Modbus TCP, byte order AND bit order

Hello, kov_serg, you wrote: P>> About the order of bits in byte, it is probable all the same a simulator something behaves in a queer way. _> try another. https://habrahabr.ru/post/281430/http:/ … lator.org/ _> Image: 633601a0db318202ee47aa7d5d5f84a9.jpg So quitted that it and is that simulator, with which problems