首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PyModbus.server端口502没有打开StartTcpServer

PyModbus.server端口502没有打开StartTcpServer
EN

Stack Overflow用户
提问于 2017-11-13 12:41:41
回答 1查看 1.6K关注 0票数 1

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

有人有这方面的经验吗?

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

代码语言:javascript
运行
复制
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已关闭。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-13 17:58:04

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

代码语言:javascript
运行
复制
StartTcpServer(context, identity=identity, address=('localhost', 502))

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47264617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档