与Elfin EW11 RS485 wifi转换器通信
Communicate with Elfin EW11 RS485 wifi converter
我正在尝试与具有 RS485 modbus 接口的电表 (Orno WE-517) 通信。此串行接口连接到 Elfin EW11 modbus 到 Wifi 转换器。
使用 pymodbus,我可以连接到适配器,但无法从仪表读取任何内容。
这是我的代码:
from pymodbus.client.sync import ModbusTcpClient
from pprint import pprint
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
print('holding register')
result = client.read_holding_registers(28,1)
response = client.execute(result)
pprint(vars(response))
client.close()
这是脚本的输出:
python3 modBus.py
2021-12-30 10:46:44,112 MainThread DEBUG sync :216 Connection to Modbus server established. Socket ('192.168.0.10', 47763)
holding register
2021-12-30 10:46:44,113 MainThread DEBUG transaction :140 Current transaction state - IDLE
2021-12-30 10:46:44,114 MainThread DEBUG transaction :145 Running transaction 1
2021-12-30 10:46:44,114 MainThread DEBUG transaction :273 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1c 0x0 0x1
2021-12-30 10:46:44,115 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,115 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:44,216 MainThread DEBUG transaction :375 Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2021-12-30 10:46:44,217 MainThread DEBUG transaction :297 RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG socket_framer :147 Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG factory :266 Factory Response[ReadHoldingRegistersResponse: 3]
2021-12-30 10:46:44,219 MainThread DEBUG transaction :454 Adding transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :465 Getting transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-12-30 10:46:44,221 MainThread DEBUG transaction :140 Current transaction state - TRANSACTION_COMPLETE
2021-12-30 10:46:44,221 MainThread DEBUG transaction :145 Running transaction 2
2021-12-30 10:46:44,222 MainThread DEBUG transaction :273 SEND: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,222 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,223 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:47,227 MainThread DEBUG transaction :303 Transaction failed. (Modbus Error: [Invalid Message] No response received, expected at least 8 bytes (0 received))
2021-12-30 10:46:47,228 MainThread DEBUG socket_framer :147 Processing:
2021-12-30 10:46:47,228 MainThread DEBUG transaction :465 Getting transaction 2
2021-12-30 10:46:47,229 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
{'fcode': 3,
'message': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)',
'string': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)'}
如您所见,pymodbus 设法连接、发送请求并接收响应。
但是我无法从该响应中找到做某事的方法。
有没有人可以帮助我?
提前,非常感谢您的帮助:)
您要读取哪个仪表?
尝试添加
- 从 pymodbus.constants 导入 Endian
- 从 pymodbus.payload 导入 BinaryPayloadDecoder
改变
result = client.read_holding_registers(address=28, count=2, unit=1)
添加
if not result.isError():
print(BinaryPayloadDecoder.fromRegisters(result.registers,
byteorder=Endian.Big,
wordorder=Endian.Big).decode_32bit_float())
- 如果未解决post 准确答案的仪表型号
坚持寻找解决方案,我发现这些值实际上是浮动的。
这引导我找到以下解决方案:
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
result = client.read_holding_registers(0x1C, 2)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, '>', '>')
print(decoder.decode_32bit_float())
client.close()
真正的解决方案是找到 BinaryPayloadDecoder 的各种方法。
我正在尝试与具有 RS485 modbus 接口的电表 (Orno WE-517) 通信。此串行接口连接到 Elfin EW11 modbus 到 Wifi 转换器。
使用 pymodbus,我可以连接到适配器,但无法从仪表读取任何内容。 这是我的代码:
from pymodbus.client.sync import ModbusTcpClient
from pprint import pprint
import logging
FORMAT = ('%(asctime)-15s %(threadName)-15s '
'%(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
log = logging.getLogger()
log.setLevel(logging.DEBUG)
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
print('holding register')
result = client.read_holding_registers(28,1)
response = client.execute(result)
pprint(vars(response))
client.close()
这是脚本的输出:
python3 modBus.py
2021-12-30 10:46:44,112 MainThread DEBUG sync :216 Connection to Modbus server established. Socket ('192.168.0.10', 47763)
holding register
2021-12-30 10:46:44,113 MainThread DEBUG transaction :140 Current transaction state - IDLE
2021-12-30 10:46:44,114 MainThread DEBUG transaction :145 Running transaction 1
2021-12-30 10:46:44,114 MainThread DEBUG transaction :273 SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x3 0x0 0x1c 0x0 0x1
2021-12-30 10:46:44,115 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,115 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:44,216 MainThread DEBUG transaction :375 Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2021-12-30 10:46:44,217 MainThread DEBUG transaction :297 RECV: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG socket_framer :147 Processing: 0x0 0x1 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,218 MainThread DEBUG factory :266 Factory Response[ReadHoldingRegistersResponse: 3]
2021-12-30 10:46:44,219 MainThread DEBUG transaction :454 Adding transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :465 Getting transaction 1
2021-12-30 10:46:44,220 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2021-12-30 10:46:44,221 MainThread DEBUG transaction :140 Current transaction state - TRANSACTION_COMPLETE
2021-12-30 10:46:44,221 MainThread DEBUG transaction :145 Running transaction 2
2021-12-30 10:46:44,222 MainThread DEBUG transaction :273 SEND: 0x0 0x2 0x0 0x0 0x0 0x5 0x1 0x3 0x2 0x3e 0xf0
2021-12-30 10:46:44,222 MainThread DEBUG sync :76 New Transaction state 'SENDING'
2021-12-30 10:46:44,223 MainThread DEBUG transaction :287 Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2021-12-30 10:46:47,227 MainThread DEBUG transaction :303 Transaction failed. (Modbus Error: [Invalid Message] No response received, expected at least 8 bytes (0 received))
2021-12-30 10:46:47,228 MainThread DEBUG socket_framer :147 Processing:
2021-12-30 10:46:47,228 MainThread DEBUG transaction :465 Getting transaction 2
2021-12-30 10:46:47,229 MainThread DEBUG transaction :224 Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
{'fcode': 3,
'message': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)',
'string': '[Input/Output] Modbus Error: [Invalid Message] No response '
'received, expected at least 8 bytes (0 received)'}
如您所见,pymodbus 设法连接、发送请求并接收响应。 但是我无法从该响应中找到做某事的方法。
有没有人可以帮助我?
提前,非常感谢您的帮助:)
您要读取哪个仪表?
尝试添加
- 从 pymodbus.constants 导入 Endian
- 从 pymodbus.payload 导入 BinaryPayloadDecoder
改变
result = client.read_holding_registers(address=28, count=2, unit=1)
添加
if not result.isError():
print(BinaryPayloadDecoder.fromRegisters(result.registers,
byteorder=Endian.Big,
wordorder=Endian.Big).decode_32bit_float())
- 如果未解决post 准确答案的仪表型号
坚持寻找解决方案,我发现这些值实际上是浮动的。 这引导我找到以下解决方案:
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.payload import BinaryPayloadDecoder
client = ModbusTcpClient('192.168.0.18', 502)
if not client.connect():
print('Error connecting')
exit()
result = client.read_holding_registers(0x1C, 2)
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, '>', '>')
print(decoder.decode_32bit_float())
client.close()
真正的解决方案是找到 BinaryPayloadDecoder 的各种方法。