我想停止一个pymodbus
异步ModbusTcpServer
,然后启动一个新的服务器,因此我尝试使用以下简化的代码段,但出现了一个错误:
from pymodbus.server.async import StartTcpServer, StopServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from time import sleep
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
def main(name='Pymodbus'):
store = ModbusSlaveContext(hr=ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = name
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'
StartTcpServer(
context,
identity=identity,
address=("localhost", 5020),
defer_reactor_run=True
)
sleep(3)
name += 'stuff'
StopServer()
sleep(3)
main(name) # Recursive
main()
输出:
^{pr2}$[更新]
另外,我尝试使用另一个线程来停止ModbusTcpServer
,在ModbusTcpServer
中使用defer_reactor_run=Flase
参数(作为默认值),但即使这样,行为仍然保持不变:
import threading
import logging
from pymodbus.server.async import StartTcpServer, StopServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
def stop():
StopServer()
def main(name='Pymodbus'):
store = ModbusSlaveContext(hr=ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)
identity = ModbusDeviceIdentification()
identity.VendorName = name
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'
t = threading.Timer(5, stop)
t.daemon = True
t.start()
StartTcpServer(
context,
identity=identity,
address=("localhost", 5020),
defer_reactor_run=False
)
name += 'stuff'
main(name) # Recursive
main()
输出:
INFO:pymodbus.server.async:Starting Modbus TCP Server on localhost:5020
DEBUG:pymodbus.server.async:Running in Main thread
DEBUG:pymodbus.server.async:Running in spawned thread
DEBUG:pymodbus.server.async:Stopping Server from another thread
INFO:pymodbus.server.async:Starting Modbus TCP Server on localhost:5020
DEBUG:pymodbus.server.async:Running in Main thread
Traceback (most recent call last):
File "stack.py", line 41, in <module>
main()
File "stack.py", line 39, in main
main() # Recursive
File "stack.py", line 35, in main
defer_reactor_run=False
File "/usr/local/lib/python3.6/dist-packages/pymodbus/server/async.py", line 257, in StartTcpServer
reactor.run(installSignalHandlers=_is_main_thread())
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 1260, in run
self.startRunning(installSignalHandlers=installSignalHandlers)
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 1240, in startRunning
ReactorBase.startRunning(self)
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/base.py", line 748, in startRunning
raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable
我找到了另一种解决方案,可以通过另一个Python代码来停止和启动异步ModbusTcpServer,因为显然,我们无法重新启动
reactor
事件循环。在这是
runner.py
代码:这是
^{pr2}$async_server.py
代码段:输出:
在第一个示例中,由于您将param defer_reactor_run设置为True,所以TCP服务器没有运行,但是您没有明确地运行它。因此,当您尝试停止它时,它会失败,因为它尚未启动。在
在第二个例子中,您运行它,但是下次使用main(name)调用递归地调用它!所以,它失败了,因为它已经启动了! 下一个代码应该有效:
如果要延迟运行,必须调用:
^{pr2}$相关问题 更多 >
编程相关推荐