我使用pymodbus在树莓派3B上出现连接错误。使用MAX14854G作为RS-485收发器设计了一块板,该板连接到RPI3的UART引脚(8&10)。它通过T1,T2和T4(GND)引脚连接到m系统电能表(53U-1211-AD4 / H)。
附加信息:通过以下过程,我正在使用UART的全部功能:
这是我的代码:
In [3]: import pymodbus
In [4]: import serial.rs485
In [5]: from pymodbus.client.sync import ModbusSerialClient as ModbusClient
In [6]: from pymodbus.register_read_message import ReadInputRegistersResponse
In [7]: msys = ModbusClient(method='rtu',port='/dev/ttyAMA0',stopbits=1,bytesize=8,parity='N',baudrate=9600,timeout=5)
In [8]: msys.connect()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-31c23cb3ed21> in <module>()
----> 1 msys.connect()
/usr/local/lib/python3.5/dist-packages/pymodbus/client/sync.py in connect(self)
474 self.close()
475 if self.method == "rtu":
--> 476 self.socket.interCharTimeout = self.inter_char_timeout
477 self.last_frame_end = None
478 return self.socket is not None
更新:
遵循@BenyaminJafari的建议并更改了root权限:
sudo chmod 777 /dev/ttyAMA0
我终于可以按照以下代码连接,但是导致了另一个错误:
In [39]: msys.connect()
Out[39]: True
In [40]: valueHoldingRegisters = msys.read_holding_registers(1,1,unit=1)
In [41]: valueHoldingRegisters = msys.read_holding_registers(1,1,unit=1)
In [42]: valueHoldingRegisters.isError()
Out[42]: True
In [43]: print(valueHoldingRegisters)
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
错误日志:
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x0 0x1 0x0 0x1 0xd5 0xca
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received))
DEBUG:pymodbus.framer.rtu_framer:Frame - [b''] not ready
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
Unhandled error
更新(添加了一些新行):
In [1]: import pymodbus
In [2]: import serial
In [3]: from pymodbus.pdu import ModbusRequest
In [4]: from pymodbus.client.sync import ModbusSerialClient as ModbusClient
In [5]: from pymodbus.transaction import ModbusRtuFramer
In [6]: from pymodbus.register_read_message import ReadInputRegistersResponse
In [7]: import logging
In [8]: logging.basicConfig()
In [9]: log = logging.getLogger()
In [10]: log.setLevel(logging.DEBUG)
In [11]: msys = ModbusClient(method='rtu',port='/dev/ttyAMA0',stopbits=1,bytesize=8,parity='N',baudrate=9600,timeout=2)
In [12]: connection = msys.connect()
In [13]: print(connection)
True
In [14]: HR1 = msys.read_holding_registers(0,1,unit=1)
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x0 0x0 0x0 0x1 0x84 0xa
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Transaction failed. (Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received))
DEBUG:pymodbus.framer.rtu_framer:Frame - [b''] not ready
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
In [15]: print(HR1)
Modbus Error: [Input/Output] Modbus Error: [Invalid Message] Incomplete message received, expected at least 2 bytes (0 received)
In [25]: HR2= msys.read_holding_registers(1,1,unit=1)
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 2
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x0 0x1 0x0 0x1 0xd5 0xca
DEBUG:pymodbus.framer.rtu_framer:Changing state to IDLE - Last Frame End - None, Current Time stamp - 1478195437.918486
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Incomplete message received, Expected 7 bytes Recieved 0 bytes !!!!
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV:
DEBUG:pymodbus.framer.rtu_framer:Frame - [b''] not ready
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
In [26]: print(HR2)
Modbus Error: [Input/Output] No Response received from the remote unit/Unable to decode response
In [27]: HR3 = msys.read_holding_registers(2,1,unit=1)
DEBUG:pymodbus.transaction:Current transaction state - TRANSACTION_COMPLETE
DEBUG:pymodbus.transaction:Running transaction 3
DEBUG:pymodbus.transaction:SEND: 0x1 0x3 0x0 0x2 0x0 0x1 0x25 0xca
DEBUG:pymodbus.framer.rtu_framer:Changing state to IDLE - Last Frame End - 1478195439.927521, Current Time stamp - 1478195492.844382
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Incomplete message received, Expected 7 bytes Recieved 0 bytes !!!!
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV:
DEBUG:pymodbus.framer.rtu_framer:Frame - [b''] not ready
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
In [28]: print(HR3)
Modbus Error: [Input/Output] No Response received from the remote unit/Unable to decode response
有人可以帮忙,建议我要怎么做吗?
参考方案
我设置了Modbus从站日志记录以获取更多信息:
import logging
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
msys = ModbusClient(
method='rtu', port='/dev/ttyAMA0',
stopbits=1, bytesize=8, parity='N',
baudrate=9600, timeout=5
)
if msys.connect():
try:
valueHoldingRegisters = msys.read_holding_registers(1, 1, unit=1)
if not valueHoldingRegisters.isError():
print(valueHoldingRegisters.registers)
else:
raise ValueError(valueHoldingRegisters)
except Exception as exc:
print(exc)
else:
print('Connection error')
msys.close()
[注意]:
使用以下命令确保您的端口具有root权限:
sudo chmod 777 /dev/ttyAMA0
Raspberry Pi在python和raspistill中捕获的图像质量 - python我正在使用覆盆子pi来检测我的猫何时在桌子上,并且我在几个图像捕获部件上遇到了一些麻烦。这是我正在运行的相关代码:from picamera.array import PiRGBArray from picamera import PiCamera import time import cv2 import subprocess #method 1 with…
在树莓派上运行python脚本 - python我正在研究rapiberry pi 3大约3个月,开始使用它时遇到了问题。我找不到在树莓派打开时在其上运行python脚本的有效,安全的方法(没有监视器,鼠标和键盘)。此刻,我在/ etc / profile中添加了“ $ sudo run myscript.py&”但是有时候,当我打开它时,直到将显示器,鼠标和键盘连接到该脚本并使用GUI运行脚本,脚本才能…
如果一个Cron作业失败,请继续执行下一个作业 - python我已经设置了Crontab来执行一系列Python脚本,以便查询太阳能逆变器并将结果上传到PVOutput:*/5 * * * * cd /home/Pi/; python pvout_upload.py; */5 * * * * cd /home/Pi/; python weather.py; 问题是如果Internet断开,则这些脚本将失败。因此,我创建…
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Raspberry Pi Google Assistant上的Google自定义设备操作出错 - python我正在Raspberry Pi 3上创建一个Google助手,并且试图创建一个自定义设备操作来最终打开我的车库门。此时,它所做的全部就是用LED播放。这是我的actions.json文件:{ "manifest": { "displayName": "Garage door", "invo…