Thingsboard gateway 3.4 接入modbus设备

Windows下载modbus模拟器

  • 安装串口模拟工具Virtual Serial Port Kit http://www.opdown.com/soft/125086.html
  • 安装Modbus从设备仿真器 ModbusSlave https://www.jb51.net/softs/597224.html#downintro2
  • 安装Modbus主机仿真器 Modbus Poll https://mip.onlinedown.net/soft/978634.htm

Mac下载

  • Appstore 搜索安装 ModRtuTcp

配置模拟器

port kit

在这里插入图片描述

Slave

在这里插入图片描述
A.Slave为Modbus从站地址,对应主画面中的ID值,默认为1。
B. Function 为寄存器功能码的选择,共四种,分别对应关系如下
在这里插入图片描述
C. Address 为寄存器起始地址。默认从1开始。
D. Length为寄存器连续个数。默认为10个。

Poll

在这里插入图片描述

配置Thingsboard gateway

源码安装(略)

配置修改 modbus.json

{
  "master": {
    "slaves": [
      {
        "host": "192.168.0.74",
        "port": 502,
        "type": "tcp",
        "method": "socket",
        "timeout": 35,
        "byteOrder": "LITTLE",
        "wordOrder": "LITTLE",
        "retries": true,
        "retryOnEmpty": true,
        "retryOnInvalid": true,
        "pollPeriod": 5000,
        "unitId": 1,
        "deviceName": "Temp Sensor",
        "sendDataOnlyOnChange": false,
        "connectAttemptTimeMs": 5000,
        "connectAttemptCount": 5,
        "waitAfterFailedAttemptsMs": 300000,
        "attributes": [
          {
            "tag": "certificateNumber",
            "type": "8int",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 0
          },
          {
            "tag": "string_read",
            "type": "string",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 1
          },
          {
            "tag": "bits_read",
            "type": "bits",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 5
          },
          {
            "tag": "8int_read",
            "type": "8int",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 6
          },
          {
            "tag": "16int_read",
            "type": "16int",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 7
          },
          {
            "tag": "32int_read_divider",
            "type": "32int",
            "functionCode": 3,
            "objectsCount": 2,
            "address": 8,
            "divider": 10
          },
          {
            "tag": "8int_read_multiplier",
            "type": "8int",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 10,
            "multiplier": 10
          },
          {
            "tag": "32int_read",
            "type": "32int",
            "functionCode": 3,
            "objectsCount": 2,
            "address": 11
          },
          {
            "tag": "64int_read",
            "type": "64int",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 13
          }
        ],
        "timeseries": [
          {
            "tag": "8uint_read",
            "type": "8uint",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 17
          },
          {
            "tag": "16uint_read",
            "type": "16uint",
            "functionCode": 3,
            "objectsCount": 2,
            "address": 18
          },
          {
            "tag": "32uint_read",
            "type": "32uint",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 20
          },
          {
            "tag": "64uint_read",
            "type": "64uint",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 24
          },
          {
            "tag": "16float_read",
            "type": "16float",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 25
          },
          {
            "tag": "32float_read",
            "type": "32float",
            "functionCode": 3,
            "objectsCount": 2,
            "address": 26
          },
          {
            "tag": "64float_read",
            "type": "64float",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 28
          },
          {
            "tag": "string_read",
            "type": "string",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 36
          }
        ],
        "attributeUpdates": [
          {
            "tag": "shared_attribute_write",
            "type": "32int",
            "functionCode": 6,
            "objectsCount": 2,
            "address": 29
          }
        ],
        "rpc": [
          {
            "tag": "setValue",
            "type": "bits",
            "functionCode": 5,
            "objectsCount": 1,
            "address": 31
          },
          {
            "tag": "getValue",
            "type": "bits",
            "functionCode": 1,
            "objectsCount": 1,
            "address": 31
          },
          {
            "tag": "setCPUFanSpeed",
            "type": "32int",
            "functionCode": 16,
            "objectsCount": 2,
            "address": 33
          },
          {
            "tag": "getCPULoad",
            "type": "32int",
            "functionCode": 3,
            "objectsCount": 2,
            "address": 35
          }
        ]
      }
    ]
  },
  "slave": {
    "type": "tcp",
    "host": "192.168.0.74",
    "port": 502,
    "method": "socket",
    "deviceName": "Modbus Slave Example",
    "deviceType": "default",
    "pollPeriod": 5000,
    "sendDataToThingsBoard": false,
    "byteOrder": "LITTLE",
    "wordOrder": "LITTLE",
    "unitId": 0,
    "values": {
      "holding_registers": [
        {
          "attributes": [
            {
              "address": 1,
              "type": "string",
              "tag": "sm",
              "objectsCount": 1,
              "value": "ON"
            }
          ],
          "timeseries": [
            {
              "address": 2,
              "type": "int",
              "tag": "smm",
              "objectsCount": 1,
              "value": "12334"
            }
          ],
          "attributeUpdates": [
            {
              "tag": "shared_attribute_write",
              "type": "32int",
              "functionCode": 6,
              "objectsCount": 2,
              "address": 29,
              "value": 1243
            }
          ],
          "rpc": [
            {
              "tag": "setValue",
              "type": "bits",
              "functionCode": 5,
              "objectsCount": 1,
              "address": 31,
              "value": 22
            }
          ]
        }
      ],
      "coils_initializer": [
        {
          "attributes": [
            {
              "address": 5,
              "type": "string",
              "tag": "sm",
              "objectsCount": 1,
              "value": "12"
            }
          ],
          "timeseries": [],
          "attributeUpdates": [],
          "rpc": []
        }
      ]
    }
  }
}

修改thingsboard.json

"connectors": [
    {
      "name": "Modbus Connector",
      "type": "modbus",
      "configuration": "modbus.json"
    }
  ]

启动成功

出现下面语句表示启动正常

"2023-09-26 18:00:29" - |INFO| - [tb_loader.py] - tb_loader - import_module - 68 - Import ModbusConnector from /thingsboard-gateway/thingsboard_gateway/connectors/modbus.
"2023-09-26 18:00:29" - |INFO| - [tb_gateway_service.py] - tb_gateway_service - __init__ - 298 - Gateway started.

启动失败

  • cryptography版本过低问题
[tb_loader.py] - tb_loader - import_module - 75 - deprecated() got an unexpected keyword argument 'name'"

原因是因为cryptography 3.4版本与新版OpenSSL不兼容导致,需要升级,修改requirements.txt,cryptography==41.0.3

  • pymodbus版本问题,修改为3.0.0版本 pymodbus==3.0.0

模拟器与gateway处理关键细节说明

pymodbus 连接测试

modbus的连接主要是用的 pymodbus 3.0.0,在初期连接不上时,可以用下面的代码进行简单测试

from pymodbus.client import ModbusTcpClient
from thingsboard_gateway.connectors.modbus.modbus_converter import ModbusConverter

client = ModbusTcpClient("192.168.0.74",502,timeout=1)

class CustomModbusConverter(ModbusConverter):
    def __init__(self, config, logger):
        self._log = logger
        self.__config = config.get('converter')

    @property
    def config(self):
        return self.__config

def main(cmdline=None):
    """Combine setup and run."""
    request1 = client.read_coils(0, 10, slave=2)
    result1 = request1.bits
    client.write_coil(1,True,2)
    # print(result.bits[0])
    print(result1)
    client.close()

if __name__ == "__main__":
    main()

其中slave是标识ID,read_coils 是读取状态配置,要将Funcion修改为01
在这里插入图片描述
运行后可以出现布尔数组标识可以连上modbus了,否则继续排查下网关连接

网关字段类型说明

在这里插入图片描述

  • 定义的类型需要与functionCode对应起来
  • objectsCount表示该字段的长度范围
  • 注意address地址长度的匹配
{
            "tag": "certificateNumber",
            "type": "8int",
            "functionCode": 4,
            "objectsCount": 1,
            "address": 0
          },
          {
            "tag": "string_read",
            "type": "string",
            "functionCode": 3,
            "objectsCount": 4,
            "address": 1
          },
          {
            "tag": "bits_read",
            "type": "bits",
            "functionCode": 3,
            "objectsCount": 1,
            "address": 5
          },

如何使用modbus传递字符串

如果要将字符串 “a” 模拟传递给 Modbus Slave 工具中定义的长度为 4 位的字段,将字符串 “a” 转换为字节数组,元素的值是 97,是字符 “a” 的 ASCII 码值,并将其存储在保持寄存器中:
字节数组:[0, 0, 0, 97]

如何在gateway中添加多个设备

https://github.com/thingsboard/thingsboard-gateway/issues/1118
在这里插入图片描述

启动成功页面上变化

在这里插入图片描述
最新遥测数据每10秒更新,这里要注意属性,将只变化才发送的选项关闭,否则不会更新,要么就去修改值后再观察

“sendDataOnlyOnChange”: false

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ThingsBoard Gateway Modbus设备定义是指在ThingsBoard Gateway中使用Modbus协议进行通信时,能够支持连接和管理多个设备。 首先,我们需要在ThingsBoard Gateway配置文件中定义每个设备的连接信息和设备参数。这包括设备的地址、端口号、协议类型(例如Modbus TCP或RTU)、通信参数(例如波特率、奇偶校验等)等。通过配置文件,我们可以设置每个设备的唯一标识符和名称,以便在后续的操作中识别和访问这些设备。 其次,我们需要在配置文件中定义每个设备的寄存器映射。这些寄存器映射指定了每个设备中的Modbus寄存器地址和对应的数据类型。这些映射是为了实现设备与ThingsBoard之间的数据交换。我们可以定义输入寄存器、保持寄存器、离散输入和线圈等不同的寄存器类型,并指定它们之间的映射关系。 一旦设备和寄存器映射都定义完毕,ThingsBoard Gateway就可以与这些设备建立连接并实时获取它们的数据。它会定期轮询设备上的寄存器,然后将获取到的数据发送到ThingsBoard平台上。 需要注意的是,ThingsBoard Gateway Modbus设备定义的性能和稳定性受到硬件资源和通信网络条件的限制。如果想要连接大量的设备,可能需要考虑增加硬件资源的配置或者优化网络环境。 总之,通过ThingsBoard Gateway Modbus设备定义,我们可以方便地连接和管理多个Modbus设备,实现设备与ThingsBoard之间的数据交换和监控。这为物联网应用的开发和管理提供了便捷性和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_三石_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值