通过 IoT Edge 设备网关连接 Modbus TCP 设备Connect Modbus TCP devices through an IoT Edge device gateway

若要将使用 Modbus TCP 或 RTU 协议的 IoT 设备连接到 Azure IoT 中心,可以使用 IoT Edge 设备作为网关。If you want to connect IoT devices that use Modbus TCP or RTU protocols to an Azure IoT hub, you can use an IoT Edge device as a gateway. 此网关设备从 Modbus 设备读取数据,然后使用支持的协议将该数据传送到云。The gateway device reads data from your Modbus devices, then communicates that data to the cloud using a supported protocol.

Modbus 设备通过 IoT Edge 网关连接到 IoT 中心

本文介绍如何针对 Modbus 模块创建自己的容器映像(也可使用预构建的示例),然后将其部署到会充当网关的 IoT Edge 设备。This article covers how to create your own container image for a Modbus module (or you can use a prebuilt sample) and then deploy it to the IoT Edge device that will act as your gateway.

本文假定你使用的是 Modbus TCP 协议。This article assumes that you're using Modbus TCP protocol. 若要详细了解如何配置支持 Modbus RTU 的模块,请参阅 GitHub 上的 Azure IoT Edge Modbus 模块项目。For more information about how to configure the module to support Modbus RTU, see the Azure IoT Edge Modbus module project on GitHub.

先决条件Prerequisites

  • Azure IoT Edge 设备。An Azure IoT Edge device. 若要详细了解如何设置一个,请参阅在 Windows 中部署 Azure IoT Edge在 Linux 中部署 Azure IoT EdgeFor a walkthrough on how to set up one, see Deploy Azure IoT Edge on Windows or Linux.
  • IoT Edge 设备的主键连接字符串。The primary key connection string for the IoT Edge device.
  • 支持 Modbus TCP 的物理或模拟 Modbus 设备。A physical or simulated Modbus device that supports Modbus TCP. 需要知道其 IPv4 地址。You will need to know its IPv4 address.

准备 Modbus 容器Prepare a Modbus container

若要测试 Modbus 网关功能,可以使用 Microsoft 提供的示例模块。If you want to test the Modbus gateway functionality, Microsoft has a sample module that you can use. 可以通过 Azure 市场 Modbus 或映像 URI mcr.microsoft.com/azureiotedge/modbus:1.0 访问模块。You can access the module from the Azure Marketplace, Modbus, or with the image URI, mcr.microsoft.com/azureiotedge/modbus:1.0.

如果需要创建自己的模块并根据环境对其自定义,可以使用 GitHub 上的开源 Azure IoT Edge Modbus 模块项目。If you want to create your own module and customize it for your environment, there is an open-source Azure IoT Edge Modbus module project on GitHub. 按照该项目中的指南创建自己的容器映像。Follow the guidance in that project to create your own container image. 若要创建容器映像,请参阅在 Visual Studio 中开发 C# 模块在 Visual Studio Code 中开发模块To create a container image, refer to Develop C# modules in Visual Studio or Develop modules in Visual Studio Code. 这些文章说明了如何创建新模块并将容器映像发布到注册表。Those articles provide instructions on creating new modules and publishing container images to a registry.

试用此解决方案Try the solution

此部分详述如何将 Microsoft 的示例 Modbus 模块部署到 IoT Edge 设备。This section walks through deploying Microsoft's sample Modbus module to your IoT Edge device.

  1. Azure 门户中转到 IoT 中心。On the Azure portal, go to your IoT hub.

  2. 转到“IoT Edge” ,然后单击 IoT Edge 设备。Go to IoT Edge and click on your IoT Edge device.

  3. 选择“设置模块” 。Select Set modules.

  4. 在“IoT Edge 模块”部分中,添加 Modbus 模块 :In the IoT Edge Modules section, add the Modbus module:

    1. 单击“添加”下拉列表,然后选择“市场模块” 。Click the Add dropdown and select Marketplace Module.
    2. 搜索 Modbus,并选择 Microsoft 的“Modbus TCP 模块” 。Search for Modbus and select the Modbus TCP Module by Microsoft.
    3. IoT 中心的模块会自动进行配置,并显示在 IoT Edge 模块列表中。The module is automatically configured for your IoT Hub and appears in the list of IoT Edge Modules. 路由也会自动进行配置。The Routes are also automatically configured. 选择“查看 + 创建” 。Select Review + create.
    4. 查看部署清单并选择“创建” 。Review the deployment manifest and select Create.
  5. 在列表中选择 Modbus 模块 ModbusTCPModule,然后选择“模块孪生设置”选项卡 。自动填充模块孪生所需属性的必需 JSON。Select the Modbus module, ModbusTCPModule, in the list and select the Module Twin Settings tab. The required JSON for the module twin desired properties is auto populated.

  6. 查找 JSON 中的 SlaveConnection 属性,并将其值设置为 Modbus 设备的 IPv4 地址 。Look for the SlaveConnection property in the JSON and set its value to the IPv4 address of your Modbus device.

  7. 选择“更新” 。Select Update.

  8. 选择“查看 + 创建”,检查部署,然后选择“创建” 。Select Review + create, review the deployment, and then select Create.

  9. 返回到“设备详细信息”页,并选择“刷新” 。Return to the device details page and select Refresh. 此时会看到新的 ModbusTCPModule 模块与 IoT Edge 运行时一起运行。You should see the new ModbusTCPModule module running along with the IoT Edge runtime.

查看数据View data

查看通过 Modbus 模块发送过来的数据:View the data coming through the Modbus module:

iotedge logs modbus

也可使用适用于 Visual Studio Code 的 Azure IoT 中心扩展(以前称为 Azure IoT 工具包扩展)查看设备正在发送的遥测数据。You can also view the telemetry the device is sending by using the Azure IoT Hub extension for Visual Studio Code (formerly Azure IoT Toolkit extension).

后续步骤Next steps