Python implements modbus communication-modbus_tk

Implement Modbus RTU and Modbus TCP client/server using Modbus_Tk

Introduction

Modbus_Tk is a Modbus protocol library for the Python programming language, which provides functions to implement Modbus RTU and Modbus TCP communications. This article will introduce how to use the Modbus_Tk library to implement the client and server of Modbus RTU and Modbus TCP.

Preparation

Before we start, we need to complete the following preparations:

  1. Install Python: Make sure you have installed the Python interpreter, you can download and install the latest version from the Python official website.
  2. Install Modbus_Tk: Run with pip command pip install modbus_tkto install Modbus_Tk library.

Modbus RTU client

Below is a sample code showing how to implement a Modbus RTU client using Modbus_Tk:

from modbus_tk import modbus_rtu

# 创建一个Modbus RTU主站
# master = modbus_rtu.RtuMaster('/dev/ttyUSB0')
master = modbus_rtu.RtuMaster(
            serial.Serial(port='/dev/ttyUSB0', baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0)
        )

# 设置串口参数
master.set_timeout(5.0)
master.set_verbose(True)

# 连接到设备
master.open()

# 读取保持寄存器的值
result = master.execute(1, modbus_rtu.READ_HOLDING_REGISTERS, 0, 10)

# 打印结果
print(result)

# 关闭连接
master.close()

In the above code, we first imported modbus_rtuthe module, and then created a Modbus RTU master object, specifying the serial device path. Next, we set the timeout and verbose output mode of the serial port. We then use executethe method to send a request to read the holding register and print the result. Finally, we close the connection.

Modbus RTU server

The following is a sample code showing how to implement Modbus RTU server using Modbus_Tk:

from modbus_tk import modbus_rtu

# 创建一个Modbus RTU从站
# slave = modbus_rtu.RtuSlave('/dev/ttyUSB0')
slave = modbus_rtu.RtuSlave( 
	serial.Serial(port='/dev/ttyUSB0', baudrate=9600, bytesize=8, parity='N', 			stopbits=1, xonxoff=0)
)

# 设置从站ID
slave.set_slave(1)

# 启动从站
slave.start()

# 设置保持寄存器的初始值
slave.set_values('holding_registers', 0, [1, 2, 3, 4, 5])

# 运行从站
slave.join()

In the above code, we also created a Modbus RTU slave object and specified the serial device path. Then, we set the ID of the slave and the initial values ​​of the holding registers. Finally, we joinrun the slave by calling the method.

Modbus TCP client

Below is a sample code showing how to implement a Modbus TCP client using Modbus_Tk:

from modbus_tk import modbus_tcp

# 创建一个Modbus TCP主站
master = modbus_tcp.TcpMaster('192.168.1.1', 502)

# 读取保持寄存器的值
result = master.execute(1, modbus_tcp.READ_HOLDING_REGISTERS, 0, 10)

# 打印结果
print(result)

In the above code, we create a Modbus TCP Master object, specifying the IP address and port number for the connection. We then use executethe method to send a request to read the holding register and print the result.

Modbus TCP server

The following is a sample code showing how to implement Modbus TCP server using Modbus_Tk:

from modbus_tk import modbus_tcp

# 创建一个Modbus TCP从站
slave = modbus_tcp.TcpSlave('192.168.1.1', 502)

# 设置从站ID
slave.set_slave(1)

# 启动从站
slave.start()

# 设置保持寄存器的初始值
slave.set_values('holding_registers', 0, [1, 2, 3, 4, 5])

# 运行从站
slave.join()

In the above code, we also created a Modbus TCP slave object and specified the listening IP address and port number. Then, we set the ID of the slave and the initial values ​​of the holding registers. Finally, we joinrun the slave by calling the method.

Summarize

By using the Modbus_Tk library, we can easily implement the client and server of Modbus RTU and Modbus TCP. This article provides sample code to help readers get started quickly. If you have more requirements for the Modbus_Tk library, you can check the official documentation for more information and sample codes.

Hope this article can help you!

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/131758350#comments_28140036