我正在使用pymodbus从设备读取数据。
这是我使用的代码,它可以正常工作!
import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient #initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#count= the number of registers to read
#unit= the slave unit this request is targeting
#address= the starting address to read from
client= ModbusClient(method = "rtu", port="/dev/ttyUSB0",stopbits = 1, bytesize = 8, parity = 'N', baudrate= 9600)
#Connect to the serial modbus server
connection = client.connect()
#print connection
#Starting add, num of reg to read, slave unit.
result= client.read_input_registers(0x00,10,unit= 0x01).registers
print(result)
#Closes the underlying socket connection
client.close()
结果:
pi@raspberrypi:~ $ python pyserial2.py
DEBUG:pymodbus.transaction:Current transaction state - IDLE
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:SEND: 0x1 0x4 0x0 0x0 0x0 0xa 0x70 0xd
DEBUG:pymodbus.client.sync:New Transaction state 'SENDING'
DEBUG:pymodbus.transaction:Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
DEBUG:pymodbus.transaction:Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
DEBUG:pymodbus.transaction:RECV: 0x1 0x4 0x14 0x1 0x52 0x3 0x12 0x0 0xfa 0x3 0x20 0x0 0x0 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x0 0x1 0xab 0xd6
DEBUG:pymodbus.framer.rtu_framer:Getting Frame - 0x4 0x14 0x1 0x52 0x3 0x12 0x0 0xfa 0x3 0x20 0x0 0x0 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x0 0x1
DEBUG:pymodbus.factory:Factory Response[ReadInputRegistersResponse: 4]
DEBUG:pymodbus.framer.rtu_framer:Frame advanced, resetting header!!
DEBUG:pymodbus.transaction:Adding transaction 1
DEBUG:pymodbus.transaction:Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
[338, 786, 250, 800, 0, 65535, 65535, 65535, 65535, 1]
结果显示为“ .registers”。 但是我不知道如何将值放入列表中。