gpt4 book ai didi

python - Modbus RTU master - 带有minimalmodbus的python脚本

转载 作者:行者123 更新时间:2023-12-03 08:48:26 31 4
gpt4 key购买 nike

我想在 MODBUS RTU 中使用 python 脚本控制执行器掌握。我尝试使用库minimalmodbus进行通信(写位,写入和读取寄存器)与我的奴隶。

当我启动代码时,出现一些错误。那么,有人可以帮我找到解决方案吗?

我的代码:


import minimalmodbus
import os
import struct
import sys
import serial
import time

instrument = minimalmodbus.Instrument('/dev/ttyRS485', 1)
instrument.serial.port
instrument.serial.baudrate = 9600
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.mode = minimalmodbus.MODE_RTU
instrument.serial.timeout = 0.05


modbus = instrument.write_bit(0x0427, 1)
print (modbus)

alarme = instrument.write_bit(0x0404, 1)
print (alarme)

alarme = instrument.write_bit(0x0404, 0)
print (alarme)

on = instrument.write_bit(0x0403, 1)
print (on)

home = instrument.write_bit(0x040B, 1)
print (home)

position = instrument.write_register(0x9900, 0, number_of_decimals=2,functioncode=16, signed=False)
print (position)

posi = instrument.write_register(0x9901, 6000, number_of_decimals=2,functioncode=16, signed=False)
print (posi)

错误:

    ========================= RESTART: /home/pi/test.py =========================
None
None
None
None
None
None
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 2448, in _pack
result = struct.pack(formatstring, value)
struct.error: 'H' format requires 0 <= number <= 65535

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/test.py", line 36, in <module>
posi = instrument.write_register(0x9901, 6000, number_of_decimals=2, functioncode=16, signed=False)
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 518, in write_register
payloadformat=_PAYLOADFORMAT_REGISTER,
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 1166, in _generic_command
payloadformat,
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py",line 1514, in _create_payload
value, number_of_decimals, signed=signed
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 1991, in
_num_to_twobyte_string outstring = _pack(formatcode, integer)
File "/home/pi/.local/lib/python3.5/site-packages/minimalmodbus.py", line 2454, in _pack
raise ValueError(errortext.format(value, formatstring))
ValueError: The value to send is probably out of range, as the num-to-bytestring conversion failed.
Value: 600000 Struct format code is: >H

最佳答案

为了响应您在评论中对替代库的请求,以下是我使用 pymodbus 库读取 modbus 的内容:

import pymodbus
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer

client = ModbusClient(
method = 'rtu'
,port='/dev/tty.usbserial-AQ00BYCR'
,baudrate=38400
,parity = 'O'
,timeout=1
)
connection = client.connect()
registers = client.read_holding_registers(0,100,unit=1)# start_address, count, slave_id
print (registers.registers)

请注意,在上面,对于slave_id 1,读取从地址0开始并继续到地址100。

要写入寄存器,请执行以下操作:

write  = client.write_register(1,425,unit=1)# address = 1, value to set = 425, slave ID = 1

关于python - Modbus RTU master - 带有minimalmodbus的python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60431210/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com