modbus_tk Rru
import serial
import struct
import logging
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_rtu as modbus_rtu
logger = modbus_tk.utils.create_logger("console")
if __name__ == '__main__':
try:
serial = serial.Serial(port="COM2",baudrate=9600,bytesize=8,parity='N',stopbits=1)
master = modbus_rtu.RtuMaster(serial)
master.set_timeout(5.0)
master.set_verbose(True)
logger.info("connected!")
# 写单个寄存器
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 0, output_value=99))
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 1))
# 写单个寄存器 负数
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 1, output_value=-123))
logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 1, 1))
# 写多个寄存器 起始地址为4的保持寄存器,操作寄存器个数为4 ,根据列表长度来判断写入个数
logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 4, output_value=[3,-13, 6,24]))
# 读也读4个寄存器
read_data = master.execute(1, cst.READ_HOLDING_REGISTERS, 4, 4)
# 写寄存器 起始地址为8的保持寄存器,操作寄存器个数为 4 ,一个浮点数float 占两个寄存器;
# 写浮点数时一定要加 data_format 参数,两个ff 表示要写入两个浮点数,以此类推
# 我这里模拟的是小端模式,具体可参考 struct 用法。和数据源保持一致即可。
logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 8, output_value=[3.3,-6.4],data_format='<ff'))
# 读对应的 4个寄存器,指定数据格式
read_data = master.execute(1, cst.READ_HOLDING_REGISTERS, 8, 4,data_format='<ff')
logger.info(read_data)
# 写入 双精度 数据到寄存器
logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 16, output_value=[3.313, -6.414], data_format='<dd'))
read_data = master.execute(1, cst.READ_HOLDING_REGISTERS, 16, 8, data_format='<dd')
# 写单个线圈,写寄存器地址为0的线圈寄存器,写入内容为1 位操作)
logger.info(master.execute(1, cst.WRITE_SINGLE_COIL, 0, output_value= 1))
logger.info(master.execute(1,cst.READ_COILS,0,1))
# 写多个线圈 写寄存器地址为1的线圈寄存器,写入内容为列表内容 位操作)
logger.info(master.execute(1, cst.WRITE_MULTIPLE_COILS,1,output_value=[0,1,1,0]))
logger.info(master.execute(1,cst.READ_COILS,1,4))
# 读保持寄存器,从 8 开始读 8 个,元组形式返回
data = master.execute(1, cst.READ_HOLDING_REGISTERS, 8, 8)
logger.info(data)
# 读输入寄存器,从 2 开始读 5 个,元组形式返回
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 2, 5))
# 读线圈寄存器
logger.info(master.execute(1, cst.READ_COILS, 8, 8))
# 读离散输入寄存器
logger.info(master.execute(1, cst.READ_DISCRETE_INPUTS, 8, 8))
# 同时读写多个寄存器
# ??
except modbus_tk.modbus.ModbusError as e:
logger.error("%s-ErrCode=%d" % (e, e.get_exception_code()))
参考链接:https://blog.csdn.net/evenio/article/details/105156457