import serial,time,struct import modbus_tk.defines as cst from modbus_tk import modbus_rtu # from modbus_tk.utils import create_master_string def main(): # 打开两个串口 com1 = serial.Serial(port='com2', baudrate=38400, bytesize=8, parity='N', stopbits=1) com2 = serial.Serial(port='com3', baudrate=38400, bytesize=8, parity='N', stopbits=1) # 创建Modbus RTU主机(master) master = modbus_rtu.RtuMaster(com1) master.set_timeout(1.0) master.set_verbose(True) # time.sleep(2) # 读取保持寄存器数据 red = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 9) print(red) red1 = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 9)[0] # 这里可以修改需要读取的功能码 print(red1) new = int(red1 * 0.98) print(new) master.execute(1, function_code=cst.WRITE_MULTIPLE_REGISTERS, starting_address=0, output_value=[new]) time.sleep(0.5) red = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 9) print(red,0) # 读取输入寄存器数据 input_regs = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 9) print(input_regs, 1) # 读取输入线圈数据 input_bits = master.execute(1, cst.READ_COILS, 0, 9) print(input_bits, 2) # 读取输出线圈数据 output_bits = master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 9) print(output_bits, 3)。现在将所有读取数据打包为Modbus RTU消息并写入com2串口。要求打包的Modbus RTU消息格式要跟 master = modbus_rtu.RtuMaster(com1)一样
时间: 2023-09-10 21:05:05 浏览: 172
wuziqi.rar_chess_conio.h_time.h_五子棋 dos_五子棋辅助
好的,我理解你的要求了。你可以使用modbus_tk.utils模块中的create_master_string函数来将Modbus RTU消息打包。下面是一个示例代码,可以将上面的读取数据和写入数据的代码整合在一起,并将结果写入com2串口:
```
import serial
import time
import struct
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
from modbus_tk.utils import create_master_string
def main():
# 打开两个串口
com1 = serial.Serial(port='com2', baudrate=38400, bytesize=8, parity='N', stopbits=1)
com2 = serial.Serial(port='com3', baudrate=38400, bytesize=8, parity='N', stopbits=1)
# 创建Modbus RTU主机(master)
master = modbus_rtu.RtuMaster(com1)
master.set_timeout(1.0)
master.set_verbose(True)
# 读取保持寄存器数据
red = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 9)
print(red)
red1 = red[0] # 这里可以修改需要读取的功能码
print(red1)
new = int(red1 * 0.98)
print(new)
master.execute(1, function_code=cst.WRITE_MULTIPLE_REGISTERS, starting_address=0, output_value=[new])
time.sleep(0.5)
red = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 9)
print(red, 0)
# 读取输入寄存器数据
input_regs = master.execute(1, cst.READ_INPUT_REGISTERS, 0, 9)
print(input_regs, 1)
# 读取输入线圈数据
input_bits = master.execute(1, cst.READ_COILS, 0, 9)
print(input_bits, 2)
# 读取输出线圈数据
output_bits = master.execute(1, cst.READ_DISCRETE_INPUTS, 0, 9)
print(output_bits, 3)
# 将所有读取数据打包为Modbus RTU消息并写入com2串口
msg = create_master_string(slave=1, function_code=cst.READ_HOLDING_REGISTERS, starting_address=0, quantity_of_x=9, output_value=red)
com2.write(msg)
msg = create_master_string(slave=1, function_code=cst.READ_INPUT_REGISTERS, starting_address=0, quantity_of_x=9, output_value=input_regs)
com2.write(msg)
msg = create_master_string(slave=1, function_code=cst.READ_COILS, starting_address=0, quantity_of_x=9, output_value=input_bits)
com2.write(msg)
msg = create_master_string(slave=1, function_code=cst.READ_DISCRETE_INPUTS, starting_address=0, quantity_of_x=9, output_value=output_bits)
com2.write(msg)
if __name__ == '__main__':
main()
```
请注意,create_master_string函数的参数与execute函数的参数非常相似,但有一些差异。具体来说,create_master_string函数需要传递slave参数,它表示Modbus从站的地址。此外,它的function_code参数表示要执行的Modbus功能码,而不是execute函数中的function_code参数。最后,output_value参数是一个可选参数,表示要写入的数据,如果是读操作,则可以忽略它。
阅读全文