我正在使用Modbus
pymodbus 库中的服务器示例代码来制作数据转发器(读取一些串行数据,格式化然后将数据分配给 modbus 寄存器以供 Modbus 主设备读取)。我的问题是串行延迟(从 6 个不同的设备读取,每个设备有 5 秒的延迟,但它是随机的,它们不会同时发送数据)。在此示例中,他们使用 LoopingCall,这对我的计时不利。我如何摆脱它,所以我可以自己调用函数?
我试图摆脱:
time = 2 # Update delay
loop = LoopingCall(f=updating_writer, a=(context,))
loop.start(time, now=False)
并简单地调用:
updating_writer()
但显然我错过了一些论点:
a=(context,)
我的部分代码:
def data_decoder6():
ser_read_JB1 = ser_JB6.read(386)
if len(ser_read_JB1) != 0:
# convert ascii to hex
aTOh_JB1 = "".join("{:02x}".format(c) for c in ser_read_JB1)
# convert to string
iTOs_JB1 = str(aTOh_JB1)
lokacija = [258,422,426,414,402,406,266,274,270,558,
530,534,538,322,326,330,334,338,342,346,
350,354,358,362,366,370,374,378,382,386,
390,394,398
]
values_JB1 = [0 for d in range(33)]
values_2_JB6 = [0 for e in range(33)]
for x in range(0, 33):
values_JB1[x] = (iTOs_JB1[lokacija[x]] + iTOs_JB1[lokacija[x]+1]
+ iTOs_JB1[lokacija[x]+2] + iTOs_JB1[lokacija[x]+3])
#values1 = values.replace('"', '')
values_2_JB6[x] = int(values_JB1[x], 16)
#values.append( 2009 )
return values_2_JB6
else:
values_2_JB6 = [0 for e in range(33)]
return values_2_JB6
def updating_writer(a):
allValues = [0 for d in range(198)]
allValues = (data_decoder1() + data_decoder2() + data_decoder3() +
data_decoder4() + data_decoder5() + data_decoder6())
# modbus part
log.debug("updating the context")
context = a[0]
register = 0x03
slave_id = 0x01
address = 0x01
values = context[slave_id].getValues(register, address, count=198)
values = allValues
log.debug("new values: " + str(values))
context[slave_id].setValues(register, address, values)
def run_updating_server():
store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [0]*1),
co = ModbusSequentialDataBlock(0, [0]*1),
hr = ModbusSequentialDataBlock(0, [0]*198),
ir = ModbusSequentialDataBlock(0, [0]*1))
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = 'pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'pymodbus Server'
identity.ModelName = 'pymodbus Server'
identity.MajorMinorRevision = '1.0'
time = 2 # Update delay
loop = LoopingCall(f=updating_writer, a=(context,))
loop.start(time, now=False)
StartTcpServer(context, identity=identity, address=("10.10.10.253", 502))
run_updating_server()