连接协议-Modbus
更新时间:2018-10-13 17:54:07
背景概要
概述
Modbus 是工业现场较为常用的总线协议,是应用层报文传输协议(OSI模型第7层),支持1对1传输、1对多传输。支持的模式有:modbus-TCP、modbus-RTU、modbus-ASCII。其中modbus-TCP是基于TCP/IP之上的应用协议。modbus-RTU、modbus-ASCII 是串口协议,主要的电气接口有:RS232、RS485。
通信栈

物理连接拓扑关系

物模型
- 样例
{
    "properties": [
        {
            "identifier": "YellowLight",
            "function": [
                "(0x01)ReadCoils",
                "(0x0F)WriteMultipleCoils"
            ],
            "args": {
                "startAddress": 80,
                "quantity": 1
            },
            "dataType": {
                "type": "enum",
                "specs": {
                    "0": "light off",
                    "1": "light on"
                }
            },
            "pollingTime": 2000,
            "triggerType": 1,
            "desc": "control the light"
        },
        {
            "identifier": "A_voltage",
            "function": [
                "(0x04)ReadInputRegisters"
            ],
            "args": {
                "startAddress": 30,
                "quantity": 1
            },
            "dataType": {
                "type": "float",
                "specs": {
                    "unit": "℃",
                    "min": 0,
                    "max": 20
                }
            },
            "encodeExpress": "",
            "decodeExpress": "swap16(A)",
            "scale": 1,
            "pollingTime": 3000,
            "triggerType": 1,
            "desc": "return the value of the specified property"
        },
        {
            "identifier": "airConditionerTemp",
            "function": [
                "(0x04)ReadInputRegisters",
                "(0x06)WriteSingleRegister"
            ],
            "args": {
                "startAddress": 63,
                "quantity": 1
            },
            "dataType": {
                "type": "float",
                "specs": {
                    "unit": "℃",
                    "min": 0,
                    "max": 20
                }
            },
            "encodeExpress": "",
            "decodeExpress": "",
            "scale": 1,
            "pollingTime": 3000,
            "triggerType": 1,
            "desc": "return the value of the specified property"
        }
    ],
    "services": [],
    "events": []
}
样例说明
- 属性集(properties): 主要是描述设备属性的集合: 
- identifier:属性名称。 
- function: 功功能码,包含“读功能码”、”写功能码”。目前的功能码有: 
| 功能码 | 支持 | 
|---|---|
| (0x01)ReadCoils | √ | 
| (0x02)ReadDiscreteInputs | √ | 
| (0x03)ReadHoldingRegisters | √ | 
| (0x04)ReadInputRegisters | √ | 
| (0x05)WriteSingleCoil | × | 
| (0x06)WriteSingleRegister | √ | 
| (0x0F)WriteMultipleCoils | √ | 
| (0x10)WriteMultipleregisters | × | 
- args:参数信息,包含“起始地址”、“个数”。 
- dataType:与TMP保持一致 
- encodeExpress: 编码表达式,指将人能识别的数据转换为modbus数据,入参用A、B、C、D表示。目前支持的运算符和函数:附件-1 
- decodeExpress: 解码表达式,指将modbus数据转换为人能识别的数据,入参用A、B、C、D表示。目前支持的运算符和函数:附件-1 
- scale:表示数据精度,如0.1,scale是1。 
- pollingTime :采集间隔, 单位是ms。 
- triggerType :对采集后的数据的上报方式,目前有“按时上报”:1和“变更上报”:2。 
- description :对本规则进行简单的描述。用户通过这些基本的描述就能明白这条“规则”的功能和作用。 
- 方法集(services): 无 
- 事件集(events): 无 
驱动配置信息
对应 LinkEdge镜像V1.2.0版本的“驱动管理”:
{
    "connect": [
        {
            "protocol": "ModbusTCP",
            "ip": "localhost",
            "port": 15020,
            "slaveId": 255,
            "refProductConfig": "modbus_productConfig.json",
            "deviceId": "2131432543543543"
        },
        {
            "protocol": "ModbusTCP",
            "ip": "localhost",
            "port": 15021,
            "slaveId": 255,
            "refProductConfig": "modbus_productConfig.json",
            "deviceId": "2131432543543544"
        },
        {
            "protocol": "ModbusTCP",
            "ip": "localhost",
            "port": 15022,
            "slaveId": 255,
            "refProductConfig": "modbus_productConfig.json",
            "deviceId": "2131432543543545"
        },
        {
            "protocol": "ModbusTCP",
            "ip": "localhost",
            "port": 15023,
            "slaveId": 255,
            "refProductConfig": "modbus_productConfig.json",
            "deviceId": "2131432543543546"
        },
        {
            "protocol": "ModbusRTU",
            "serialPort": "/dev/tty0", "baudRate": 9600, "byteSize": 8, "parity": "N","stopBits": 1,
            "slaveId": 1 , "refProductConfig": "modbus_productConfig.json",
            "deviceId":"2131432543543547"
        }
    ]
}
对应 LinkEdge镜像V1.2.0版本的“驱动管理”:
{
    "DeviceList": [
        {
            "DeviceConfig": {
                "protocol": "ModbusTCP",
                "port": 502,
                "ip": "localhost",
                "slaveId": 255
            },
            "ProductKey": "b1ykrrOmkti",
            "DeviceName": "w9scB7mwK1JZwS6LiFiM"
        }
    ],
    "DriverConfig": {
        "transMode": "half_duplex",
        "serverPort": 502
    }
}
其中DrvierConfig是针对modbus slave(server)的配置项。
- deviceConfig字段解说
| protocol | desc | 
|---|---|
| ModbusTCP | ip: IP地址 port: 端口号 slaveId:子设备的从地址 refProductConfig:关联productConfig文件 deviceId: 子设备唯一的标识信息 | 
| ModbusRTU/ModbusASCII | serialPort:串口号 baudRate:波特率 byteSize: 字长(The number of bits in a byte of serial data. This can be one of5, 6, 7, or 8) parity:奇偶校验位('E': 偶校验, 'O':奇校验, 'N': 无校验) stopBits:停止位(The number of bits sent after each characterin a message to indicate the end of the byte. This defaults to 1) slaveId: 子设备的从地址 refProductConfig:关联productConfig文件 deviceId: 子设备唯一的标识信息 | 
附件-1
| 运算符 | 描述 | 备注 | 
|---|---|---|
| + | 加法运算符 | N/A | 
| - | 减法运算符 | N/A | 
| * | 乘法运算符 | N/A | 
| / | 除法运算符 | N/A | 
| 函数 | 描述 | 备注 | 
|---|---|---|
| htof() | 16进制转换为浮点 | N/A | 
| ftoh() | 浮点转换为16进制 | N/A | 
| swap16() | 把原始数据的前后8个bits互换(byte1byte2 -> byte2byte1) | N/A | 
| swap32() | 把原始数据的前后16个bits互换(byte1byte2byte3 byte4 ->byte3byte4byte1 byte2) | N/A | 
- 说明:- 1、运算符可以和函数一起混合使用,如:(1 + swap16(A))* 3
- 2、当有多个参数时,可依次用A、B、C、D来表示。目前最多支持四个入参。