ModbusTCPClient

ModbusTCPClient implements the Modbus TCP client. ModbusTCPClient support function codes 1 (Read Coils), 2 (Read Discrete Inputs), 3 (Read Holding Registers), 4 (Read Input Registers), 5 (Write Single Coil), 6 (Write Single Holding Register), 15 (Write Multiple Coils), and 16 (Write Multiple Holding Registers).

UiFlow2 Example

CoreS3 TCP Client

Open the cores3_tcp_client_example.m5f2 project in UiFlow2.

This example demonstrates how to use ModbusTCPClient to implement a Modbus TCP client.

UiFlow2 Code Block:

cores3_tcp_client_example.png

Example output:

None

MicroPython Example

CoreS3 TCP Client

This example demonstrates how to use ModbusTCPClient to implement a Modbus TCP client.

MicroPython Code Block:

  1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8import modbus
  9import time
 10
 11
 12label0 = None
 13label1 = None
 14label2 = None
 15label3 = None
 16label4 = None
 17label5 = None
 18label6 = None
 19label7 = None
 20label8 = None
 21label9 = None
 22label14 = None
 23label19 = None
 24label24 = None
 25label10 = None
 26label15 = None
 27label20 = None
 28label25 = None
 29label11 = None
 30label16 = None
 31label21 = None
 32label26 = None
 33label12 = None
 34label17 = None
 35label22 = None
 36label27 = None
 37label13 = None
 38label18 = None
 39label23 = None
 40label28 = None
 41modbustcpclient_0 = None
 42
 43
 44hr = None
 45coil = None
 46res = None
 47
 48
 49def setup():
 50    global \
 51        label0, \
 52        label1, \
 53        label2, \
 54        label3, \
 55        label4, \
 56        label5, \
 57        label6, \
 58        label7, \
 59        label8, \
 60        label9, \
 61        label14, \
 62        label19, \
 63        label24, \
 64        label10, \
 65        label15, \
 66        label20, \
 67        label25, \
 68        label11, \
 69        label16, \
 70        label21, \
 71        label26, \
 72        label12, \
 73        label17, \
 74        label22, \
 75        label27, \
 76        label13, \
 77        label18, \
 78        label23, \
 79        label28, \
 80        modbustcpclient_0, \
 81        hr, \
 82        coil, \
 83        res
 84
 85    M5.begin()
 86    Widgets.setRotation(1)
 87    Widgets.fillScreen(0x222222)
 88    label0 = Widgets.Label("co", 65, 8, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 89    label1 = Widgets.Label("di", 135, 8, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 90    label2 = Widgets.Label("hr", 205, 8, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 91    label3 = Widgets.Label("ir", 275, 8, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 92    label4 = Widgets.Label("1000", 4, 45, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 93    label5 = Widgets.Label("1001", 4, 85, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 94    label6 = Widgets.Label("1002", 4, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 95    label7 = Widgets.Label("1003", 4, 165, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 96    label8 = Widgets.Label("1004", 4, 205, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 97    label9 = Widgets.Label("a00", 65, 45, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 98    label14 = Widgets.Label("a01", 135, 45, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 99    label19 = Widgets.Label("a02", 205, 45, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
100    label24 = Widgets.Label("a03", 275, 45, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
101    label10 = Widgets.Label("a10", 65, 85, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
102    label15 = Widgets.Label("a11", 135, 85, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
103    label20 = Widgets.Label("a12", 205, 85, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
104    label25 = Widgets.Label("a13", 275, 85, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
105    label11 = Widgets.Label("a20", 65, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
106    label16 = Widgets.Label("a21", 135, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
107    label21 = Widgets.Label("a22", 205, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
108    label26 = Widgets.Label("a23", 275, 125, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
109    label12 = Widgets.Label("a30", 65, 165, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
110    label17 = Widgets.Label("a31", 135, 165, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
111    label22 = Widgets.Label("a32", 205, 165, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
112    label27 = Widgets.Label("a33", 275, 165, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
113    label13 = Widgets.Label("a40", 65, 205, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
114    label18 = Widgets.Label("a41", 135, 205, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
115    label23 = Widgets.Label("a42", 205, 205, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
116    label28 = Widgets.Label("a43", 275, 205, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
117
118    modbustcpclient_0 = modbus.ModbusTCPClient("192.168.52.60", 5000, verbose=True)
119    modbustcpclient_0.connect()
120    hr = 0
121    res = modbustcpclient_0.read_coils(1, 1000, 5, timeout=2000)
122    label9.setText(str(res[0]))
123    label10.setText(str(res[1]))
124    label11.setText(str(res[2]))
125    label12.setText(str(res[3]))
126    label13.setText(str(res[4]))
127    res = modbustcpclient_0.read_discrete_inputs(1, 1000, 5, timeout=2000)
128    label14.setText(str(res[0]))
129    label15.setText(str(res[1]))
130    label16.setText(str(res[2]))
131    label17.setText(str(res[3]))
132    label18.setText(str(res[4]))
133    res = modbustcpclient_0.read_holding_registers(1, 1000, 5, timeout=2000)
134    label19.setText(str(res[0]))
135    label20.setText(str(res[1]))
136    label21.setText(str(res[2]))
137    label22.setText(str(res[3]))
138    label23.setText(str(res[4]))
139    res = modbustcpclient_0.read_input_registers(1, 1000, 5, timeout=2000)
140    label24.setText(str(res[0]))
141    label25.setText(str(res[1]))
142    label26.setText(str(res[2]))
143    label27.setText(str(res[3]))
144    label28.setText(str(res[4]))
145
146
147def loop():
148    global \
149        label0, \
150        label1, \
151        label2, \
152        label3, \
153        label4, \
154        label5, \
155        label6, \
156        label7, \
157        label8, \
158        label9, \
159        label14, \
160        label19, \
161        label24, \
162        label10, \
163        label15, \
164        label20, \
165        label25, \
166        label11, \
167        label16, \
168        label21, \
169        label26, \
170        label12, \
171        label17, \
172        label22, \
173        label27, \
174        label13, \
175        label18, \
176        label23, \
177        label28, \
178        modbustcpclient_0, \
179        hr, \
180        coil, \
181        res
182    M5.update()
183    hr = (hr + 1) % 65535
184    coil = not coil
185    modbustcpclient_0.write_single_coil(1, 1000, coil, timeout=2000)
186    modbustcpclient_0.write_single_register(1, 1000, hr, timeout=2000)
187    modbustcpclient_0.write_multiple_coils(1, 1000, [coil, coil, coil, coil, coil], timeout=2000)
188    label9.setText(str(coil))
189    label10.setText(str(coil))
190    label11.setText(str(coil))
191    modbustcpclient_0.write_multiple_registers(1, 1000, [hr, hr, hr, hr, hr], timeout=2000)
192    label19.setText(str(hr))
193    label20.setText(str(hr))
194    label21.setText(str(hr))
195    time.sleep(1)
196
197
198if __name__ == "__main__":
199    try:
200        setup()
201        while True:
202            loop()
203    except (Exception, KeyboardInterrupt) as e:
204        try:
205            from utility import print_error_msg
206
207            print_error_msg(e)
208        except ImportError:
209            print("please update to latest firmware")

Example output:

None

API

ModbusTCPClient

class modbus.ModbusTCPClient(host: str, port: int = 502, verbose: bool = False)

Create a ModbusTCPClient object.

Parameters:
  • host (str) – Hostname or IP address.

  • port (int) – Port number.

  • verbose (bool) – Verbose mode.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from modbus import ModbusTCPClient
client = ModbusTCPClient('192.168.1.100', 502)
connect() None

Connect to the Modbus server.

UiFlow2 Code Block:

connect.png

MicroPython Code Block:

client.connect()
disconnect() None

Disconnect from the Modbus server.

UiFlow2 Code Block:

disconnect.png

MicroPython Code Block:

client.disconnect()
read_coils(address, register, quantity, timeout: int = 2000) list

Read coils.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the coils. The address is 0x0000 to 0xFFFF.

  • quantity (int) – Quantity of registers to read.

  • timeout (int) – Timeout in milliseconds.

Returns:

A list of coils. The item of the list is True or False.

UiFlow2 Code Block:

read_coils.png

MicroPython Code Block:

client.read_coils(1, 0, 10)
read_discrete_inputs(address, register, quantity, timeout: int = 2000) list

Read discrete inputs.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the discrete inputs. The address is 0x0000 to 0xFFFF.

  • quantity (int) – Quantity of registers to read.

  • timeout (int) – Timeout in milliseconds.

Returns:

A list of discrete inputs. The item of the list is True or False.

UiFlow2 Code Block:

read_discrete_inputs.png

MicroPython Code Block:

client.read_discrete_inputs(1, 0, 10)
read_holding_registers(address, register, quantity, timeout: int = 2000) list

Read holding registers.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the holding registers. The address is 0x0000 to 0xFFFF.

  • quantity (int) – Quantity of registers to read.

  • timeout (int) – Timeout in milliseconds.

Returns:

A list of holding registers. The item of the list is 0x0000 to 0xFFFF.

UiFlow2 Code Block:

read_holding_registers.png

MicroPython Code Block:

client.read_holding_registers(1, 0, 10)
read_input_registers(address, register, quantity, timeout: int = 2000) list

Read input registers.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the input registers. The address is 0x0000 to 0xFFFF.

  • quantity (int) – Quantity of registers to read.

  • timeout (int) – Timeout in milliseconds.

Returns:

A list of input registers. The item of the list is 0x0000 to 0xFFFF.

UiFlow2 Code Block:

read_input_registers.png

MicroPython Code Block:

client.read_input_registers(1, 0, 10)
write_single_coil(address, register, value, timeout: int = 2000) bool

Write a single coil.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the coils. The address is 0x0000 to 0xFFFF.

  • value (int) – Value to write. The value is True or False.

  • timeout (int) – Timeout in milliseconds.

Returns:

The value of the coil.

UiFlow2 Code Block:

write_single_coil.png

MicroPython Code Block:

client.write_single_coil(1, 0, True)
write_single_register(address, register, value, timeout: int = 2000) int

Write a single register.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the holding registers. The address is 0x0000 to 0xFFFF.

  • value (int) – Value to write. The value is 0x0000 to 0xFFFF.

  • timeout (int) – Timeout in milliseconds.

Returns:

the written value

UiFlow2 Code Block:

write_single_register.png

MicroPython Code Block:

client.write_single_register(1, 0, 100)
write_multiple_coils(address, register, values, timeout: int = 2000) int

Write multiple coils.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the coils. The address is 0x0000 to 0xFFFF.

  • values (list) – Values to write. The item of the list is True or False.

  • timeout (int) – Timeout in milliseconds.

Returns:

the written count.

UiFlow2 Code Block:

write_multiple_coils.png

MicroPython Code Block:

client.write_multiple_coils(1, 0, [True, False, True])
write_multiple_registers(address: int, register: int, values: list, timeout: int = 2000) int

Write multiple registers.

Parameters:
  • address (int) – Slave address. The address is 0 to 247.

  • register (int) – Start address of the holding registers. The address is 0x0000 to 0xFFFF.

  • values (list) – Values to write. The item of the list is 0x0000 to 0xFFFF.

  • timeout (int) – Timeout in milliseconds.

Returns:

the written count.

UiFlow2 Code Block:

write_multiple_registers.png

MicroPython Code Block:

client.write_multiple_registers(1, 0, [100, 200, 300])