Modbus RTU/TCP
RTU
RS232
This demo uses the Nucleo U575ZI Cortex-M33 development board. It depends on FreeRTOS for demo purposes.
The current temperature of an LM75 I2C sensor is read in Q8.8 format.
Two TTYs are utilised. The ST-LINK tty is used for the modbus I/O and the other for debug output and was only needed during development so can be ignored.
Another demo for the STM32 F4 Discovery performs modbus rtu over its USB OTG port using the TinyUSB stack.
RS485
This demo uses the Nucleo H7A3ZI-Q Cortex-M7 development board. It depends on FreeRTOS for demo purposes.
The modbus I/O with this demo uses a MAX485 transceiver and demonstrates how to utilise it.
TCP
This demo uses the Nucleo G491RE Cortex-M4 development board and an ENC28J60 ethernet MAC/PHY. It depends on FreeRTOS with FreeRTOS-PLUS-TCP.
VREFINT is read as an input to the STM32 internal ADC.
Another demo for the Nucleo H563ZI utilises its built-in MAC/PHY.
Install
Firmware
Install mercurial if needed then fetch stm32 microcontroller tests.
hg clone http://hg.kewl.org/pub/Archimedes
Toolchain
Refer to README in the source directory and install dependencies.
mbpoll
apt install mbpoll
STM32U575
cd NUCLEO-U575ZI-Q make program debug
References
Register | Address | Format | Mode | Comment |
---|---|---|---|---|
LM75 temperature | 0x75 | Q8.8 | Read only | Divide by 256.0 to convert to floating point |
Peek
mbpoll parameter breakdown:
- Verbose output: -v
- Parity: -P none
- Slave address: -a 1
- Reference base zero: -0
- Poll once: -1
- Reference: -r 0x75
- 16-bit register: -t 4:hex
- 2 stop bits: -s 2
- 1 register: -c 1
mbpoll -v -P none -a 1 -0 -1 -r 0x75 -t 4:hex -s 2 -c 1 /dev/ttySTLINK3-U575 debug enabled Set rtu parity=none iGetIntList(1) Integer found: 1 iCount=1 Set start reference=117 Set function=4 Set format=hex Set rtu stop bits=2 Set number of values=1 Set mode to RTU for serial port Set device=/dev/ttySTLINK3-U575 mbpoll 1.0-0 - FieldTalk(tm) Modbus(R) Master Simulator Copyright © 2015-2019 Pascal JEAN, https://github.com/epsilonrt/mbpoll This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions; type 'mbpoll -w' for details. Opening /dev/ttySTLINK3-U575 at 19200 bauds (N, 8, 2) Set response timeout to 1 sec, 0 us Protocol configuration: Modbus RTU Slave configuration...: address = [1] start reference = 117, count = 1 Communication.........: /dev/ttySTLINK3-U575, 19200-8N2 t/o 1.00 s, poll rate 1000 ms Data type.............: 16-bit register, output (holding) register table -- Polling slave 1... [01][03][00][75][00][01][95][D0] Waiting for a confirmation... <01><03><02><1A><00><B3><24> [117]: 0x1A00
The response above is the Q8.8 value of the temperature where 0x1A00 represents 26 degrees Celcius.
If an error occurs reading the temperature, the value returned would be -1 (0xFFFF) which is an invalid value and cannot be achieved in normal use.
STM32G491
peek
mbpoll -v -a 1 -0 -1 -r 0x100 -t 4 -c 1 192.168.0.205 debug enabled iGetIntList(1) Integer found: 1 iCount=1 Set start reference=256 Set function=4 Set number of values=1 Set device=192.168.0.205 mbpoll 1.0-0 - FieldTalk(tm) Modbus(R) Master Simulator Copyright © 2015-2019 Pascal JEAN, https://github.com/epsilonrt/mbpoll This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions; type 'mbpoll -w' for details. Connecting to [192.168.0.205]:502 Set response timeout to 1 sec, 0 us Protocol configuration: Modbus TCP Slave configuration...: address = [1] start reference = 256, count = 1 Communication.........: 192.168.0.205, port 502, t/o 1.00 s, poll rate 1000 ms Data type.............: 16-bit register, output (holding) register table -- Polling slave 1... [00][01][00][00][00][06][01][03][01][00][00][01] Waiting for a confirmation... <00><01><00><00><00><05><01><03><02><0C><CB> [256]: 3275
tcpdump
tcpdump host stm32g4 and port 502 -X tcpdump: verbose output suppressed, use -v[v]... for full protocol decode listening on wlan0, link-type EN10MB (Ethernet), snapshot length 262144 bytes 12:06:19.553003 IP pi1.kewl.org.58674 > stm32g4.kewl.org.502: Flags [S], seq 1040100054, win 64240, options [mss 1460,sackOK,TS val 3692783361 ecr 0,nop,wscale 7], length 0 0x0000: 4510 003c 521f 4000 4006 65ce c0a8 00a1 E..<R.@.@.e..... 0x0010: c0a8 00cd e532 01f6 3dfe aad6 0000 0000 .....2..=....... 0x0020: a002 faf0 b735 0000 0204 05b4 0402 080a .....5.......... 0x0030: dc1b 6701 0000 0000 0103 0307 ..g......... 12:06:19.558698 IP stm32g4.kewl.org.502 > pi1.kewl.org.58674: Flags [S.], seq 1354047842, ack 1040100055, win 536, options [mss 536], length 0 0x0000: 4510 002c 0014 0000 8006 b7e9 c0a8 00cd E..,............ 0x0010: c0a8 00a1 01f6 e532 50b5 2162 3dfe aad7 .......2P.!b=... 0x0020: 6012 0218 d4c5 0000 0204 0218 0000 `............. 12:06:19.558806 IP pi1.kewl.org.58674 > stm32g4.kewl.org.502: Flags [.], ack 1, win 64240, length 0 0x0000: 4510 0028 5220 4000 4006 65e1 c0a8 00a1 E..(R.@.@.e..... 0x0010: c0a8 00cd e532 01f6 3dfe aad7 50b5 2163 .....2..=...P.!c 0x0020: 5010 faf0 f00d 0000 P....... 12:06:19.579324 IP pi1.kewl.org.58674 > stm32g4.kewl.org.502: Flags [P.], seq 1:13, ack 1, win 64240, length 12 0x0000: 4510 0034 5221 4000 4006 65d4 c0a8 00a1 E..4R!@.@.e..... 0x0010: c0a8 00cd e532 01f6 3dfe aad7 50b5 2163 .....2..=...P.!c 0x0020: 5018 faf0 edee 0000 0001 0000 0006 0103 P............... 0x0030: 0100 0001 .... 12:06:19.580447 IP stm32g4.kewl.org.502 > pi1.kewl.org.58674: Flags [.], ack 13, win 536, length 0 0x0000: 4510 0028 0015 0000 8006 b7ec c0a8 00cd E..(............ 0x0010: c0a8 00a1 01f6 e532 50b5 2163 3dfe aae3 .......2P.!c=... 0x0020: 5010 0218 e8da 0000 0000 0000 0000 P............. 12:06:19.581469 IP stm32g4.kewl.org.502 > pi1.kewl.org.58674: Flags [P.], seq 1:12, ack 13, win 536, length 11 0x0000: 4510 0033 0016 0000 8006 b7e0 c0a8 00cd E..3............ 0x0010: c0a8 00a1 01f6 e532 50b5 2163 3dfe aae3 .......2P.!c=... 0x0020: 5018 0218 1ab2 0000 0001 0000 0005 0103 P............... 0x0030: 020c cb ... 12:06:19.581605 IP pi1.kewl.org.58674 > stm32g4.kewl.org.502: Flags [.], ack 12, win 64229, length 0 0x0000: 4510 0028 5222 4000 4006 65df c0a8 00a1 E..(R"@.@.e..... 0x0010: c0a8 00cd e532 01f6 3dfe aae3 50b5 216e .....2..=...P.!n 0x0020: 5010 fae5 f001 0000 P....... 12:06:19.581868 IP pi1.kewl.org.58674 > stm32g4.kewl.org.502: Flags [F.], seq 13, ack 12, win 64229, length 0 0x0000: 4510 0028 5223 4000 4006 65de c0a8 00a1 E..(R#@.@.e..... 0x0010: c0a8 00cd e532 01f6 3dfe aae3 50b5 216e .....2..=...P.!n 0x0020: 5011 fae5 f000 0000 P....... 12:06:19.582625 IP stm32g4.kewl.org.502 > pi1.kewl.org.58674: Flags [R.], seq 12, ack 13, win 64229, length 0 0x0000: 4510 0028 0017 0000 8006 b7ea c0a8 00cd E..(............ 0x0010: c0a8 00a1 01f6 e532 50b5 216e 3dfe aae3 .......2P.!n=... 0x0020: 5014 fae5 effd 0000 0000 0000 0000 P............. 12:06:19.582921 IP stm32g4.kewl.org.502 > pi1.kewl.org.58674: Flags [R.], seq 12, ack 13, win 64229, length 0 0x0000: 4510 0028 0018 0000 8006 b7e9 c0a8 00cd E..(............ 0x0010: c0a8 00a1 01f6 e532 50b5 216e 3dfe aae3 .......2P.!n=... 0x0020: 5014 fae5 effd 0000 0000 0000 0000 P.............