本篇介绍下如何使用ModbusSlave/ModbusPoll模拟ModbusRTU与ModbusTCP协议并与TB的Python网关对接。
1.准备工具
1.ModbusPoll(链接: https://pan.baidu.com/s/1cX_jow-G8cmQNaYpAZ42fg 提取码: p2gk)
2.ModbusSlave(链接: https://pan.baidu.com/s/1Fq5O5a-EuWhUBlaNUa98OQ 提取码: 95x7)
3.虚拟com端口工具VSPD(链接: https://pan.baidu.com/s/14nN79wXnh8tVYhhn6N3iNQ 提取码: f6p5)
2.ModbusRTU协议模拟
2.1本地ModbusRTU模拟
1.虚拟两个com端口,COM7,COM8
2.打开ModbusSlave,点击setup->slave(从机)definition配置从端,如图:
3.打开ModbusPoll,点击read/write(主机)definition配置主端,如图:
以上配置的是10个寄存器从机设备ID为1但只读取前4个寄存器的数据,可根据自己的需要设置。后面为了简便,只配置了一个寄存器。
4.接下来分别点击”connection”进行连接,注意修改连接界面的端口,因为我只用到了RTU模式,所以其他选项如波特率9600,比特位8,校验位1,无等价位等可以保持不变,然后点击确定进行连接,未连接成功窗口第二行会给出提示信息。
5.通讯开始,主机端显示:TX=发送命令次数,Err=错误次数,ID=从机ID,F=功能号,SR=轮询间隔时间。使用ModbusSlave工具栏的”Display->Communication Traffic按钮,可以显示出当前发送命令和接受的数据。
2.2.连接thingsBoard-gateway
1.我的网关是装在树莓派中的,所以需要用到其他转换器,整体的连接是:
电脑COM3端口–>USB转485转换器–>485转TTL转换器–>树莓派串口
2.将ModbusSlave的端口指向COM3
3.配置tb_gateway.yaml与modbus_serial.json;在tb_gateway.yaml中将连接配置指向modbus_serial.json
tb_gateway.yaml中内容如下:
thingsboard:
host: "ip"
port: 1883
remoteConfiguration: false
security:
accessToken: "token"
storage:
type: memory
read_records_count: 100
max_records_count: 100000
# type: file
# data_folder_path: ./data/
# max_file_count: 10
# max_read_records_count: 10
# max_records_per_file: 10000
connectors:
-
name: Modbus Connector
type: modbus
configuration: modbus_serial.json
modbus_serial.json中内容如下:
{
"server": {
"name": "Modbus Default Server",
"type": "serial",
"method": "rtu",
"port": "/dev/ttyAMA0",
"baudrate": 9600,
"timeout": 35,
"devices": [
{
"unitId": 1,
"deviceName": "VuritalDevice",
"attributesPollPeriod": 5000,
"timeseriesPollPeriod": 5000,
"sendDataOnlyOnChange": false,
"timeseries": [
{
"byteOrder": "BIG",
"tag": "count",
"type": "16int",
"functionCode": 3,
"registerCount": 1,
"address": 0
}
]
}
]
}
}
最后在TB平台上可以看到遥测值在更新
3.ModbusTCP协议模拟
3.1本地ModbusTCP模拟
修改上面Poll/Slave的连接信息,将Connection选择Modbus TCP/IP,点击连接后可看到传输的数据
3.2连接thingsBoard-gateway
配置tb_gateway.yaml与modbus_serial.json;在tb_gateway.yaml中将连接配置指向modbus.json。
tb_gateway.yaml中内容如下:
thingsboard:
host: "ip"
port: 1883
remoteConfiguration: false
security:
accessToken: "token"
storage:
type: memory
read_records_count: 100
max_records_count: 100000
# type: file
# data_folder_path: ./data/
# max_file_count: 10
# max_read_records_count: 10
# max_records_per_file: 10000
connectors:
-
name: Modbus Connector
type: modbus
configuration: modbus.json
modbus.json内容如下:
{
"server": {
"type": "tcp",
"host": "10.49.0.66",
"port": 502,
"timeout": 35,
"method": "socket",
"byteOrder": "BIG",
"devices": [
{
"unitId": 1,
"deviceName": "TCPDevice",
"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": [
{
"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": 4,
"objectsCount": 2,
"address": 35
}
]
}
]
}
}
最后在TB平台上能看到设备的遥测值: