modbus協議分為基於tcp/ip的modbus tcp和和基於RS485接口的modbus rtu兩種通信方式。
modbus方式虛擬設備部署測試
如下:參閱:https://docs.edgexfoundry.org/1.2/examples/Ch-ExamplesAddingModbusDevice/
1. 首先登錄控制台edgex-ui-go頁面,導入device profile文件,和mqtt導入相同。文件名為modbus.device.profile.yml。內容如下:
name: "Network Power Meter" manufacturer: "Dent Instruments" model: "PS3037" description: "Power Scout Meter" labels: - "modbus" - "powerscout" deviceResources: - name: "Current" description: "Average current of all phases" attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "9" } properties: value: { type: "UINT16", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "min"} - name: "Energy" description: "System Total True Energy" attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "4001" } properties: value: { type: "FLOAT32", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "min"} - name: "Power" description: "System Total True Power " attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "4003" } properties: value: { type: "UINT16", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "min"} - name: "Voltage" description: "Voltage Line to line (Volts) Average" attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "4017" } properties: value: { type: "UINT16", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "min"} - name: "DemandWindowSize" description: "Demand window size in minutes; default is 15 min" attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "4603" } properties: value: { type: "UINT16", readWrite: "R", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "min"} - name: "LineFrequency" description: "Line frequency setting for metering: 50=50 Hz, 60=60Hz" attributes: { primaryTable: "HOLDING_REGISTERS", startingAddress: "4609" } properties: value: { type: "UINT16", readWrite: "R", scale: "1"} units: { type: "String", readWrite: "R", defaultValue: "Hz"} deviceCommands: - name: "Current" get: - { index: "1", operation: "get", deviceResource: "Current" } - name: "Values" get: - { index: "1", operation: "get", deviceResource: "Energy" } - { index: "2", operation: "get", deviceResource: "Power" } - { index: "3", operation: "get", deviceResource: "Voltage" } - name: "Configuration" set: - { index: "1", operation: "set", deviceResource: "DemandWindowSize" } - { index: "2", operation: "set", deviceResource: "LineFrequency" } get: - { index: "1", operation: "get", deviceResource: "DemandWindowSize" } - { index: "2", operation: "get", deviceResource: "LineFrequency" } coreCommands: - name: "Current" get: path: "/api/v1/device/{deviceId}/Current" responses: - code: "200" description: "Get the Current" expectedValues: ["Current"] - code: "500" description: "internal server error" expectedValues: [] - name: "Values" get: path: "/api/v1/device/{deviceId}/Values" responses: - code: "200" description: "Get the Values" expectedValues: ["Energy","Power","Voltage"] - code: "500" description: "internal server error" expectedValues: [] - name: "Configuration" get: path: "/api/v1/device/{deviceId}/Configuration" responses: - code: "200" description: "Get the Configuration" expectedValues: ["DemandWindowSize","LineFrequency"] - code: "500" description: "internal server error" expectedValues: [] put: path: "/api/v1/device/{deviceId}/Configuration" parameterNames: ["DemandWindowSize","LineFrequency"] responses: - code: "204" description: "Set the Configuration" expectedValues: [] - code: "500" description: "internal server error" expectedValues: []
對要讀取的modbus地址和存儲的數據類型需要在profile中進行編輯。
2 修改modbus服務端的容器編排文件docker-compose.yml,將以下代碼解注釋:
然后重新啟動:docker-compose up -d。
# device-modbus: # image: edgexfoundry/docker-device-modbus-go:1.2.1 # ports: # - "127.0.0.1:49991:49991" # container_name: edgex-device-modbus # hostname: edgex-device-modbus # networks: # - edgex-network # environment: # <<: *common-variables # Service_Host: edgex-device-modbus # depends_on: # - data # - command
3.和MQTT方式相同,在服務下添加設備,配置地址和端口,獲得最終的控制台界面:
4.配置虛擬modebus設備。下載軟件中的jar包,打開,點擊Add,為方便測試,默認全選即可,其他不需要配置。然后點擊Run。
http://modbuspal.sourceforge.net/
5.在控制台點擊Send,獲取結果,觀察結果。另外,其中的Float32的Base64編碼結果測試結果如下:
原始結果先解碼,然后將byte數組所有表示為16進制,變成4個字節。其中高位兩個字節表示為讀取單位的值(INT),最大到0x7F7F,低位兩個字節表示為讀取位置的下一個位置的值。
當值超出范圍或者仿真設置不正確時,Send返回結果會報錯。
附上Python解碼Base64並全體16進制顯示的腳本:
import base64 bytes = base64.b64decode("f38AAA==") l = [hex(int(i)) for i in bytes] print(" ".join(l))