我正在使用通过串行控制台连接到其他系统的 RasPi。我的另一个系统正在运行 modbus master,而 Raspi 正在充当 modbus slave。Raspi 已写入一些寄存器值,并且 master 不断要求一些寄存器值。
下面是奴隶的代码
from pymodbus.server.sync import StartSerialServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer
from threading import Thread
from pandas import read_csv
import time
import logging
logging.basicConfig()
log = logging.getLogger("pymodbus")
#log.setLevel(logging.ERROR)
log.setLevel(logging.DEBUG)
class test:
# '''
# '''
def __init__(self, configfile='map_default.csv', timeoutval = .05, baudrateval=115200 ):
#---------------------------------------------------------------------------#
# initialize data store
#---------------------------------------------------------------------------#
log.info("Initializing MODBUS simulator data store...")
block = ModbusSequentialDataBlock(0, [0]*2000)
self.cphm_sim = ModbusSlaveContext()
self.context = ModbusServerContext(slaves=self.cphm_sim, single=True)
self.identity = ModbusDeviceIdentification()
self.setIdentity( {"VendorName" : "inc",
"ProductCode" : Sim",
"VendorUrl" : "url.com/",
"ProductName" : "Simulator",
"ModelName" : "Simulator",
"MajorMinorRevision" : "1.0"})
self.readConfig_init(configfile)
thread = Thread(target=self.start_server, args=(timeoutval, baudrateval))
thread.start()
def start_server(self, timeoutval, baudrateval):
log.error("Modbus Serial server started.Simulator data store initialization finished.")
StartSerialServer(self.context, framer=ModbusRtuFramer, identity=self.identity, port='/dev/ttyUSB0', timeout=timeoutval, baudrate=baudrateval)
在 Raspi 上获取日志为
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer -
DEBUG:pymodbus.server.sync:Error: Socket error occurred device reports readiness to read but returned no data (device disconnected or multiple access on port?)
DEBUG:pymodbus.framer.rtu_framer:Getting Frame - 0x3 0x5 0x3a 0x0 0x2
DEBUG:pymodbus.factory:Factory Request[3]
DEBUG:pymodbus.framer.rtu_framer:Frame advanced, resetting header!!
DEBUG:pymodbus.datastore.context:validate[3] 1339:2
DEBUG:pymodbus.datastore.context:getValues[3] 1339:2
DEBUG:pymodbus.server.sync:send: 01030400000000fa33
DEBUG:pymodbus.framer.rtu_framer:Resetting frame - Current Frame in buffer -
DEBUG:pymodbus.server.sync:Error: Socket error occurred device reports readiness to read but returned no data (device disconnected or multiple access on port?)
在 Master 上,无法检索某些寄存器。有时它能够读取寄存器,下一次读取成功。
任何人都知道我在这里做错了什么