PyModbus.server服务器端口502未打开StartCP

2024-09-24 00:34:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在我的Linux PC上设置Modbus TCP服务器。 当我的502端口没有打开时,我用下面的代码检查。在

有人有这方面的经验吗?在

https://pymodbus.readthedocs.io/en/latest/examples/synchronous-server.html

from pymodbus.server.async import StartTcpServer

from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

store = ModbusSlaveContext(
    di = ModbusSequentialDataBlock(0, [17]*100),
    co = ModbusSequentialDataBlock(0, [17]*100),
    hr = ModbusSequentialDataBlock(0, [17]*100),
    ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)

    #---------------------------------------------------------------------------# 
# initialize the server information
#---------------------------------------------------------------------------# 
# If you don't set this or any fields, they are defaulted to empty strings.
    #---------------------------------------------------------------------------# 
identity = ModbusDeviceIdentification()
identity.VendorName  = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl   = 'http://github.com/riptideio/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName   = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'

    #---------------------------------------------------------------------------#
# run the server you want
    #---------------------------------------------------------------------------# 
# Tcp:
StartTcpServer(context, identity=identity, address=('localhost', 502))

编辑: 如果我用同一台计算机上的客户机连接到服务器,则服务器正在工作。如果我关闭服务器,客户机响应端口502关闭。在


Tags: 端口storefromimport服务器serveridentitypymodbus
1条回答
网友
1楼 · 发布于 2024-09-24 00:34:23

在此调用中作为服务器侦听地址提供的'localhost'字符串:

StartTcpServer(context, identity=identity, address=('localhost', 502))

告诉您的服务器只侦听与服务器在同一系统上运行的客户端的连接。如果希望服务器接受来自其他系统的连接,则传递一个空字符串''作为地址,而不是'localhost'。空字符串告诉服务器监听这个系统的所有接口。在

相关问题 更多 >