12 063
правки
(Новая страница: «For Modbus RTU:») |
(Новая страница: «More detailed description of the request and response data structure can be found on the Modbus Protocol page.») |
||
(не показаны 23 промежуточные версии этого же участника) | |||
Строка 56: | Строка 56: | ||
* '''-p''' — контроль четности ('''-p none''' — нет проверки, '''-p even''' — передается бит контроля на четность, '''-p odd''' — передается бит контроля на нечетность). По умолчанию передается бит контроля на четность(E). | * '''-p''' — контроль четности ('''-p none''' — нет проверки, '''-p even''' — передается бит контроля на четность, '''-p odd''' — передается бит контроля на нечетность). По умолчанию передается бит контроля на четность(E). | ||
For Modbus TCP: | |||
* '''-p''' — | *'''-p''' — TCP port number of the device which the controller communicates with. | ||
This is followed by the file name of the RS-485 port or host address, and at the end (optional, only for recording functions) — data. | |||
<span id="примеры-использования в modbus rtu"></span> | <span id="примеры-использования в modbus rtu"></span> | ||
== | == Examples of use in Modbus RTU == | ||
Here are some examples illustrating the capabilities of this utility. | |||
---- | ---- | ||
1. | 1. Write the new address of the WB-MR14 device to the 0x80 register using the 0x06 (Write Single Register) function. | ||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a0x00 -t0x06 -r0x80 0x02</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a0x00 -t0x06 -r0x80 0x02</syntaxhighlight> | ||
Where 0x02 is the address to be set. | |||
Answer: | |||
<syntaxhighlight lang="bash">Data to write: 0x2 | <syntaxhighlight lang="bash">Data to write: 0x2 | ||
Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | ||
Строка 83: | Строка 83: | ||
ERROR occured!</syntaxhighlight> | ERROR occured!</syntaxhighlight> | ||
The error message always occurs when writing to the special address 0 (-a0x00). Now the device needs to contact us at the address 0x02. | |||
An example of '''improper''' use: | |||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a0x00 -t0x06 -r0x80</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a0x00 -t0x06 -r0x80</syntaxhighlight> | ||
It does not specify the address to be given to the device and the device will receive an unknown address. To fix this, you need to access the device at the special address 0x00 (see the first command). | |||
---- | ---- | ||
2. | 2. Check: read the contents of the register 0x80 (now from the device with the address 0x02) using the function 0x03 (Read Holding Registers). Note that in Wiren Board devices, the functions 0x03 and 0x04 are interchangeable and lead to the same result. | ||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a2 -t0x03 -r0x80</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a2 -t0x03 -r0x80</syntaxhighlight> | ||
Строка 102: | Строка 102: | ||
---- | ---- | ||
3. | 3. Read the registers of the relay module WB-MR14 with address 0x01, containing the signature of the device, WBMR14. It is known that the signature is stored at 200 and occupies 6 registers. | ||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x03 -r200 -c 6</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x03 -r200 -c 6</syntaxhighlight> | ||
Строка 113: | Строка 113: | ||
Data: 0x0057 0x0042 0x004d 0x0052 0x0031 0x0034 </syntaxhighlight> | Data: 0x0057 0x0042 0x004d 0x0052 0x0031 0x0034 </syntaxhighlight> | ||
In the answer we received 6 16-bit values, each of which contains the code of one ASCII-character. Convert them, replacing the initial '''0x00''' with '''/x''' and removing spaces, to the form \x57, etc, which is understandable to the echo command, and display the resulting: | |||
<syntaxhighlight lang="bash">echo -e `modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 \ | <syntaxhighlight lang="bash">echo -e `modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 \ | ||
-a1 -t0x03 -r200 -c 6 | \ | -a1 -t0x03 -r200 -c 6 | \ | ||
grep Data | sed -e 's/0x00/\\\x/g' -e 's/Data://' -e 's/\s//g'`| xxd -r -p </syntaxhighlight> | grep Data | sed -e 's/0x00/\\\x/g' -e 's/Data://' -e 's/\s//g'`| xxd -r -p </syntaxhighlight> | ||
Answer: | |||
<syntaxhighlight lang="bash">WBMR14</syntaxhighlight> | <syntaxhighlight lang="bash">WBMR14</syntaxhighlight> | ||
In older firmware versions <syntaxhighlight lang="bash">| xxd -r -p</syntaxhighlight> wasn't necessary | |||
---- | ---- | ||
4. | 4. Determine the current address of the device connected to the Wiren Board. The address is unknown to us and we do not want to change it. To do this, at the command line, run a cyclic command to poll the register of 0x80 devices with addresses from 1 to 247: | ||
<syntaxhighlight lang="bash">root@wirenboard:~# for i in {1..247}; do modbus_client -mrtu /dev/ttyAPP1 --debug -a$i -t3 -r0x80 -s2 -pnone; done 2>/dev/null | grep Data: </syntaxhighlight> | <syntaxhighlight lang="bash">root@wirenboard:~# for i in {1..247}; do modbus_client -mrtu /dev/ttyAPP1 --debug -a$i -t3 -r0x80 -s2 -pnone; done 2>/dev/null | grep Data: </syntaxhighlight> | ||
Answer | |||
<syntaxhighlight lang="bash"> Data: 0x0072</syntaxhighlight> | <syntaxhighlight lang="bash"> Data: 0x0072</syntaxhighlight> | ||
Result: the address of the connected device is 0x0072, i.e. 114. It takes a little more than 2 minutes to search all addresses from 1 to 247. | |||
---- | ---- | ||
5. | 5.5. Let's turn on relay 6 on WB-MR14 relay module(addresses of flags registers start from zero, remember that!). Use the command 0x05 (Write Single Coil): | ||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x05 -r0x05 0x01</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x05 -r0x05 0x01</syntaxhighlight> | ||
Answer: | |||
<syntaxhighlight lang="bash">Data to write: 0x1 | <syntaxhighlight lang="bash">Data to write: 0x1 | ||
Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | ||
Строка 142: | Строка 141: | ||
SUCCESS: written 1 elements!</syntaxhighlight> | SUCCESS: written 1 elements!</syntaxhighlight> | ||
Note that the modbus_client utility replaced 1 with 0x00FF when recording, because this value is used to turn on the relay. Any nonzero value is changed to 0x00FF, give it a try. | |||
---- | ---- | ||
6. | 6. Turn on all the odd relays and turn off all the even ones. To do this, use the function 15 (Write Multiple Coils). There are only 14 relays in the module, so we have to pass values for 14 registers from 0 to 13. | ||
<syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x0f -r0x00 -c 14 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000</syntaxhighlight> | <syntaxhighlight lang="bash">modbus_client --debug -mrtu -pnone -s2 /dev/ttyAPP1 -a1 -t0x0f -r0x00 -c 14 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000 0x00FF 0x0000</syntaxhighlight> | ||
Answer: | |||
<syntaxhighlight lang="bash">Data to write: 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 | <syntaxhighlight lang="bash">Data to write: 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 0xff 0x00 | ||
Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | Opening /dev/ttyAPP1 at 9600 bauds (N, 8, 2) | ||
Строка 157: | Строка 156: | ||
Note the structure of the query data: | |||
*[01] — | *[01] — address | ||
*[0F] — | *[0F] — write Multiple Coils function code | ||
*[00][00] — | *[00][00] — address of the first flag register to be recorded | ||
*[00][0E] — | *[00][0E] — number of items to record (14) | ||
*[02] — | *[02] — number of data bytes (14 bits are placed in 2 bytes) | ||
*[55][15] — 01010101 00010101 ( | *[55][15] — 01010101 00010101 (the first relay is the lowest bit of the first byte, 8th relay is the highest bit of the first byte, 9th relay is the lowest bit of the second byte) | ||
*[1A][97] — CRC16 | *[1A][97] — CRC16 | ||
As well as note the structure of the answer: | |||
*<01> — | *<01> — address | ||
*<0F> — | *<0F> — write Multiple Coils function code | ||
*<00><00> — | *<00><00> — address of the first flag register to be recorded | ||
*<00><0E> — | *<00><0E> — number of recorded flag registers | ||
*<D4><0F> — CRC16 | *<D4><0F> — CRC16 | ||
More detailed description of the request and response data structure can be found on the [[Протокол Modbus/en|Modbus Protocol]] page. |
правки