【问题标题】:ModbusTCP traffic showing up in WireShark as TCP packetsModbusTCP 流量在 WireShark 中显示为 TCP 数据包
【发布时间】:2021-05-10 20:41:33
【问题描述】:

我正在使用 pyModbusTCP 在我的本地主机上发送 ModbusTCP 数据包。我的代码似乎工作正常,但在 Wireshark 中我的数据包显示为 TCP 而不是 ModbusTCP。当我转到“分析 > 启用协议”时,ModbusTCP 已启用。另一个奇怪的事情是,上周我遵循了这个教程YouTube Video,我能够让 Wireshark 识别 ModbusTCP 数据包。我今天又试了一次,Wireshark 将 ModbusTCP 数据包标记为 TCP 时遇到了同样的问题。我不知道为什么会这样,希望得到一些建议?

服务器代码:

from pyModbusTCP.server import ModbusServer, DataBank
from time import sleep
from random import uniform

# set up server on Local Host port 12345
server = ModbusServer('127.0.0.1',12345, no_block=True)
# initialize register 0 with value of 80
DataBank.set_words(0, [80])

try:
        print("Start server...")
        server.start()
        print("Server is online...")
        # change register value every 5 seconds. 
        while True:
            # Set Register @ Address 0 to random int. value
            DataBank.set_words(0, [int(uniform(0,100))])
            sleep(5)
# when hit ctrl+C in CMD line, shut down server
except:
        print("Shutdown server....")
        server.stop()
        print("Server is offline...")
        sleep(0.5)

客户代码:

from pyModbusTCP.client import ModbusClient
from pyModbusTCP.server import ModbusServer, DataBank
import time

# Set up client, tell it to communicate with server on local host port 12345
c = ModbusClient()
c.host("127.0.0.1")
c.port(12345)

while True:
    if not c.is_open():
        if not c.open():
            print("Unable to connect to 127.0.0.1:12345")
    
    if c.is_open():
        # Read Register 0 and print it to cmd line
        regs = c.read_holding_registers(0, 1)
        if regs:
            print("Register #0: " + str(regs))
            
    time.sleep(2)

我的 Wireshark 窗口的图片:Wireshark Output

【问题讨论】:

    标签: python wireshark modbus-tcp pymodbustcp


    【解决方案1】:

    我想通了。 Wireshark 没有正确标记数据包,因为我将流量从端口 502 转移到端口 12345,因为我需要超级用户权限才能在端口 502 上通话。我在代码中将其切换回端口 502,现在 Wireshark 将它们标记为 ModbusTCP 数据包。

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 1970-01-01
      • 2020-11-03
      • 2016-04-19
      • 1970-01-01
      • 2012-10-20
      • 2014-02-17
      • 2016-09-26
      • 2022-08-06
      相关资源
      最近更新 更多