PyModbus.server 502 端口未打开 StartTcpServer

PyModbus.server port 502 not open StartTcpServer

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

有人有这方面的经验吗?

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。

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

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

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