Modbus is a communications protocol that is widely adopted for connecting industrial electronic devices. Link IoT Edge offers Modbus drivers to help you easily connect industrial electronic devices. This topic introduces Modbus drivers and provides the details about how to use them.
Modbus is a widely adopted communications protocol at the application layer. Alibaba Cloud provides Modbus drivers for you to easily connect devices. Modbus drivers allow you to connect devices that use Modbus RTU and Modbus TCP protocols.
Modbus drivers provide the following functions: read input status, read input registers, read and write coil status, and read and write holding registers.
Link IoT Edge offers Modbus drivers for C and Python programming languages. It offers various Modbus drivers for the C programming language based on the gateway CPU architecture. You can use the IoT Platform console to deploy Modbus drivers to gateways. You can also download Modbus driver code from the console and modify the code based on your business requirements.
This topic provides an example of how to use Modbus drivers. The procedure is described as follows.
Step 1: Assign a driver
- Log on to the IoT Platform console. In the left-side navigation pane, choose View in the Actions column.. On the page that appears, find an existing edge instance, and click
- On the Instance Details page, click the Devices & Drivers tab, and click the
+icon on the right side of All Drivers.
- In the Assign Driver dialog box, select Official Drivers from the drop-down list. Then, find the required Modbus driver based on the gateway
CPU architecture, and click Assign in the Actions column. Then, click Close.
- Modbus drivers for the C programming language can be used only in Link IoT Edge version 1.8.4 and later.
- Modbus drivers for the Python programming language can be used only in Link IoT Edge Pro Edition.
Step 2: Configure the driver
- On the Devices & Drivers tab, click the Modbus driver that has been assigned to the edge instance, and click Driver Configurations next to Devices.
- In the dialog box that appears, click Add Channel.
Channels connect gateways and physical devices.
On the page that appears, specify the parameters as prompted, and click OK.
Parameter Description Channel Name The name of the channel. The channel name must be unique for the gateway that the channel connects. The name must be 1 to 30 characters in length and can contain letters, digits, and underscores (_). Transmission Mode The transmission mode. The valid values are RTU and TCP. If you select RTU, you must specify the following parameters. Serial Port The serial port, for example, /dev/ttyUSB0 and /dev/ttyUSB1. The value must be 1 to 64 characters in length, and can contain letters, digits, forward slashes (/), and underscores (_). Baud Rate The number of symbols that are transferred every second. Select an option from the drop-down list. Data Bit The number of bits that are contained in a group of data. Select an option from the drop-down list. Parity Bit The parity check settings. The valid values are No Parity Check, Odd Parity Check, and Even Parity Check. Stop Bit The last bit of a package. Select an option from the drop-down list. If you select TCP, you must specify the following parameters. IP Address The IP address of the Modbus device. Enter an address in the dotted decimal notation format. Port Number The port number of the Modbus device. Enter an integer that ranges from 1 to 65535.
- Optional. On the right side of Devices, click Container Configurations. On the page that appears, configure the container settings for the driver. After
you complete the configuration, click Save.
Note You can specify Container Configurations only when the Instance Type parameter is set to Pro Edition.
Table 1. Parameters Parameter Description Host Mode Specifies whether to isolate the container network from the host network. The valid values are described as follows:
- Yes: This value indicates that the container network is the same as the host network.
- No: This value indicates that the container network is isolated from the host network. If you select this option, you must configure the Network Port Mapping settings.
Network Port Mapping The mappings between host network ports and container network ports. This parameter is available only when Host Mode is set to No. The network where the function runs is isolated from the host network. You can map the listening port of the function in the container to a host network port. This allows client programs on various hosts to access the services that are provided by the function. You can specify a maximum of 10 entries.
For example, the
fc-http-serverfunction runs in a host container, and provides services by using socket port 80. The client programs on other hosts cannot access the
fc-http-serverfunction by accessing port 80 on the current host. To enable the client programs on other hosts to access the
fc-http-serverfunction, you must map the network port (port 80) in the container where the function runs to a host network port, such as port 8080. Then, the client programs on other hosts can access the
IP address:port 8080in the host network, and use the services provided by the
Specifies whether to enable the privilege mode. Root users of containers can access host services only as general users. If you want to change the system time or run the mount command in containers, you must be granted the required root permissions. In this scenario, you must enable the privilege mode for the containers.Note If you enable the privilege mode, applications and programs in the containers are granted the host root permissions, and all the host devices are mapped to the containers. Therefore, you do not need to configure the Device Mapping settings.
Device Mapping The device mappings. This parameter is available only when you select No for Privilege Mode. The network where the device management system resides is isolated from the host network. To enable a function to access a host device such as a serial interface, you must map the device to the container where the function runs. You can specify a maximum of 10 entries. Volume Mapping The volume mappings. The network where the file system resides is isolated from the host network. To enable a function to access a host file, you must map the file to the container where the function runs. You can specify a maximum of 10 entries.
Step 3: Assign a sub-device
- In the Devices section, click Assign Sub-device. In the Modbus driver settings, assign a device to the edge instance.
You can select an existing Modbus device or create a new device. To create a device, follow these steps:Note If you want to select an existing device, the device must be connected to a gateway by using the Modbus protocol. For more information, see Create a product.
- On the Assign Sub-device page that appears on the right side of the console, click Add Sub-device.
- In the Add Device dialog box, click Create Product to create a product to which the Modbus device belongs.
- In the Create Product dialog box, specify the required parameters, and click OK.
Table 2. Parameters Parameter Description Product Name The name of the product. The product name must be unique for the current account. The product name must be 4 to 30 characters in length, and can contain letters, digits, underscores (_), hyphens (-), at signs (@), and parentheses (). Gateway Connection Protocol The communications protocol. You must set this parameter to Modbus. Authentication Mode The authentication method. Select an authentication method that is suitable for your device. For more information, see Authenticate devices. Product Description The description of the product. This parameter is optional.
- In the Add Device dialog box, click Configure. On the page that appears, define the product features. The name of product that
you have created is automatically filled in the Product parameter.
Note You can configure Modbus products by using the Modbus debugging tool. For more information, see Modbus debugging tool. Before using the debugging tool, you must first add a device and assign the device to the edge instance.
You are directed to the Product Details page. On the Define Features tab of this page, click Edit Draft, and click Add Self-defined Feature.
- In the Add Self-defined Feature dialog box, specify the property parameters based on the actual conditions. For more
information, see Define features.
In this step, click Add Extended Information. In the dialog box that appears, specify the parameters that are shown in the following figure. After the configuration is completed, the property information is transferred to the specified register. The Modbus driver requests data from devices based on the specified properties, and converts the received Modbus data into TSL data.
The following table describes the parameters for the extended information. For more information, see Define features.
Parameter Description Operation Type The operation type that is indicated by the function code.
In this example, this parameter is set to Holding Registers (read and write, 0x03-read, 0x06-write).
Register Address The IP address of the register. You must enter a hexadecimal IP address that starts with
You must specify the IP address of the register based on your device property. For example, if the device temperature is indicated by 1 in the IP address, set the parameter to 0x1.
Original Data Type The data type of the raw data. For example, the data type of the temperature is floating point. Value Range The range of values that are obtained after the raw data is processed based on the zoom factor. The data that falls outside the range of values is discarded. Switch High Byte and Low Byte in Register Specifies whether to swap the first 8 bits and the last 8 bits of the 16-bit data in the register. In this example, this parameter is set to true. Switch Register Bits Sequence Specifies whether to swap the bits of the original 32-bit data. In this example, this parameter is set to false. Zoom Factor The zoom factor. For example, if the collected value is 100 and the actual value is 10, set this parameter to 0.1. If the collected value is 100 and the actual value is 1000, set this parameter to 10. Data Report The trigger of reporting data. Valid values:
- At Specific Time: Data is collected and reported at the specified collection interval.
- Report Changes: The report is triggered if the collected data changes.
- Go back to the Add Device page, and add the Modbus device.
- Assign the created Modbus device to the edge instance.
- In the Actions column, click Device Configurations for the assigned device and configure the required parameters. The specified channel
in this step connects the device to the Modbus driver.
Table 3. Parameters Parameter Description Associate Channel The channel that connects the device to the Modbus driver. In this example, select the channel that is created in Step 2 Step 2: Configure the driver. Device Station Number The Modbus device identifier, which is unique for each channel. Data Collection Interval The interval at which data is collected. Modbus is a communications protocol with half-duplex transmission. Gateways send requests to collect data from devices. Therefore, you must specify the data collection interval. The unit is millisecond.Note If 60 milliseconds is required to collect the data of each property, the total time required to collect the data of all properties is calculated as follows:
If the channel connects 10 Modbus devices and each device has 10 properties, the total required time is 6000 ms (60 ms × 10 × 10 = 6000 ms). To ensure that data is reported as expected, you must set the data collection interval to a value that is greater than or equal to 6000 ms.
Total required time = Time required to collect the data of each property (60 ms) × Number of properties for the channel
Step 4: Deploy the edge instance
- Optional. Before deploying the edge instance, you can use the debugging tool to check whether the gateway connects to the Modbus device as expected. You can also check whether the TSL model of the product to which the Modbus device belongs is correctly configured. For more information, see Modbus debugging tool.
- In the upper-right corner of the Instance Details page, click Deploy to deploy the edge instance.
Do Modbus drivers send data to the cloud based on the device dimension or the properties dimension?
Modbus drivers collect and send data to the cloud based on the device dimension in each data collection cycle. For example, if a Modbus device has 100 properties and the data collection interval is 5 seconds, the Modbus driver sends one message about the device to the cloud every 5 seconds.