Python与PLC通过Modbus通信

先安装modbus_tk包

import modbus_tk.modbus_tcp as mt
import modbus_tk.defines as md

# 远程连接到服务器端
master = mt.TcpMaster("192.168.1.2", 502)
master.set_timeout(5.0)
value = master.execute(slave=1, function_code=md.WRITE_SINGLE_REGISTER, starting_address=26, quantity_of_x=1, output_value=10)


#supported modbus functions; function code 功能码对应如下操作
# READ_COILS = 1 读线圈状态
# READ_DISCRETE_INPUTS = 2 读离散输入状态
# READ_HOLDING_REGISTERS = 3 读保持寄存器
# READ_INPUT_REGISTERS = 4 读输入寄存器
# WRITE_SINGLE_COIL = 5 写单个线圈
# WRITE_SINGLE_REGISTER = 6 写单个寄存器
# READ_EXCEPTION_STATUS = 7 读取异常状态
# DIAGNOSTIC = 8
# REPORT_SLAVE_ID = 17
# WRITE_MULTIPLE_COILS = 15 写多个线圈
# WRITE_MULTIPLE_REGISTERS = 16 写多个寄存器
# READ_WRITE_MULTIPLE_REGISTERS = 23 读写多个寄存器
# DEVICE_INFO = 43

你可能感兴趣的:(PLC,python,数字通信)