桃之夭夭,灼灼其华

Thingsboards Gateway 接入 MODBUS

Word count: 891Reading time: 4 min
2021/02/25 Share

Modbus是一种串行通信协议,是Modicon公司(现在的施耐德电气 Schneider Electric)于1979年为使用可编程逻辑控制器(PLC)通信而发表。Modbus已经成为工业领域通信协议的业界标准(De facto),并且现在是工业电子设备之间常用的连接方式。
Modbus协议是一个master/slave架构的协议。有一个节点是master节点,其他使用Modbus协议参与通信的节点是slave节点。每一个slave设备都有一个唯一的地址。在串行和MB+网络中,只有被指定为主节点的节点可以启动一个命令(在以太网上,任何一个设备都能发送一个Modbus命令,但是通常也只有一个主节点设备启动指令)

Thingsboard IOT 工作流程

图片

使用工具

  • ModbusSlave 模拟设备数据推送
  • thingsboard gateway 负责收集设备数据通过mqtt推送给thingsboard
  • thingsboard 展示推送数据,其他操作

TCP/UDP方式推送数据

  • ModbusSlave 设置信息
    1.png
  • ModbusSlave 配置连接方式TCP,端口5020
    2.png
  • thingsboard IOT 配置tb_gateway.yaml 和 modbus.json,启动ThingsBoard IOT Gateway
    • tb_gateway.yaml
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      thingsboard: //用于连接ThingsBoard平台的配置
      host: 192.168.22.26 //thingsboard ip
      port: 1883 //thingsboard mqtt 端口
      remoteShell: false
      remoteConfiguration: false
      security:
      accessToken: Th2qBIyaasQq4CYEXicT // 设备访问令牌
      qos: 1
      storage: //配置本地存储来自设备的传入数据
      type: memory
      read_records_count: 100
      max_records_count: 100000
      connectors: //连接器阵列及其使用的配置

      -
      name: Modbus Connector
      type: modbus
      configuration: modbus.json
    • modbus.json
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      {
      "server":{
      "type":"tcp", //连接类型tcp, udp or serial
      "host":"127.0.0.1", //Modbus server IP 地址
      "port":5020, //Modbus server 端口
      "timeout":35, //超时时间
      "method":"socket",//framer类型 socket or rtu
      "byteOrder":"BIG",
      "devices":[ // 设备集合
      {
      "unitId":1, //Modbus 唯一ID
      "deviceName":"testDevice",
      "attributesPollPeriod":5000,
      "timeseriesPollPeriod":5000,
      "sendDataOnlyOnChange":false,
      "attributes":[ //对应Thingsboard 设备属性
      {
      "byteOrder":"BIG",
      "tag":"count",
      "type":"16int", //数据类型
      "functionCode":3,
      "objectsCount":1,
      "address":0 //寄存器地址
      }
      ],
      "timeseries":[ // 对应Thingsboard 最新遥感数据
      {
      "byteOrder":"BIG",
      "tag":"count",
      "type":"16int",
      "functionCode":3,
      "objectsCount":1,
      "address":0
      }
      ]
      },
      {
      "unitId":2,
      "deviceName":"testDevice2",
      "attributesPollPeriod":5000,
      "timeseriesPollPeriod":5000,
      "sendDataOnlyOnChange":false,
      "attributes":[
      {
      "byteOrder":"BIG",
      "tag":"count",
      "type":"16int",
      "functionCode":3,
      "objectsCount":1,
      "address":0
      }
      ],
      "timeseries":[
      {
      "byteOrder":"BIG",
      "tag":"count",
      "type":"16int",
      "functionCode":3,
      "objectsCount":1,
      "address":0
      }
      ],
      "rpc":[ //Tingsboard 传送RPC 请求到设备
      {
      "tag":"setValue", // RPC 调用的方法
      "type":"16int", //传入设备类型
      "functionCode":6,
      "objectsCount":1,
      "address":8 //设备寄存器上的位置
      },
      {
      "tag":"getValue",
      "type":"16int",
      "functionCode":1,
      "objectsCount":1,
      "address":9
      }
      ]
      }
      ]
      }
      }
  • 在Tingsboard查看数据
    3.png

    服务端 RPC 操作设备

  • modbus.json 配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    {
    "server": {
    "type": "tcp",
    "host": "127.0.0.1",
    "port": 5020,
    "timeout": 35,
    "method": "socket",
    "byteOrder": "BIG",
    "devices": [
    {
    "unitId": 1,
    "deviceName": "RpcTestIot",
    "attributesPollPeriod": 5000,
    "timeseriesPollPeriod": 5000,
    "sendDataOnlyOnChange": true,
    "timeseries": [
    {
    "byteOrder": "BIG",
    "tag": "count",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 1,
    "address": 0
    },
    {
    "byteOrder": "BIG",
    "tag": "status",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 1,
    "address": 1
    },
    {
    "byteOrder": "BIG",
    "tag": "temperature",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 2,
    "address": 2
    }
    ],
    "rpc": [
    {
    "tag": "setCount",
    "type": "16int",
    "functionCode": 6,
    "objectsCount": 1,
    "address": 0
    },
    {
    "tag": "getCount",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 1,
    "address": 0
    },
    {
    "tag": "setValue",
    "type": "16int",
    "functionCode": 6,
    "objectsCount": 1,
    "address": 1
    },
    {
    "tag": "getValue",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 1,
    "address": 1
    },
    {
    "tag": "setTemperature",
    "type": "16int",
    "functionCode": 6,
    "objectsCount": 2,
    "address": 2
    },
    {
    "tag": "getTemperature",
    "type": "16int",
    "functionCode": 3,
    "objectsCount": 2,
    "address": 2
    }
    ]
    }
    ]
    }
    }
  • RPC控制
    • 添加仪表板控件控制
      Knob Control
      4.png
      Round switch
      5.png
      查看结果
      6.png
      7.png
    • 调用接口控制
      1
      2
      3
      curl --location --request POST 'http://localhost:8080/api/plugins/rpc/{callType}/{deviceId}' \
      --header 'X-Authorization: {JWT_TOKEN}' \
      --header 'Content-Type: application/json' \
      用例:
      1
      2
      3
      4
      5
      6
      7
      curl --location --request POST 'http://localhost:8080/api/plugins/rpc/oneway/{deviceId}' \
      --header 'X-Authorization: {JWT_TOKEN}' \
      --header 'Content-Type: application/json' \
      --data-raw '{
      "method": "setCount",
      "params": 11
      }'
      设备查看值
      8.png

相关文档
IOT GateWay:http://www.ithingsboard.com/docs/iot-gateway/getting-started/
IOT GateWay安装:http://www.ithingsboard.com/docs/iot-gateway/install/source-installation/
modbus配置:http://www.ithingsboard.com/docs/iot-gateway/config/modbus/

CATALOG
  1. 1. Thingsboard IOT 工作流程
  2. 2. 使用工具
  3. 3. TCP/UDP方式推送数据
  4. 4. 服务端 RPC 操作设备