一、背景
在工业控制场景中,管理人员需要随时知悉工业设备的实时状态和数据,并进行反控制。传统方式都是在PC端的组态软件对接PLC,对工业设备进行读写控制。在本项目中客户提供需要在手机端连接PLC,读取和写入流速计的实时流量数据。这里基于Uniapp环境开发Android端的APP,通过ModbusTcp协议与PLC通讯,实现寄存器的读写。
二、拓扑框架
说明:
- 这里将PLC作为服务器,手机APP作为客户端主动连接PLC,进行数据读写;
- 手机APP、PLC、工业设备、物联网关可以基于局域网或互联网进行组网;
- 手机APP和PLC、PLC和工业设备之间,采用ModbusTCP协议进行通讯;
三、开发对接
1、准备事宜
- 工具:PLC仿真调试软件MicroWIN SMART、MODSCAN32、HBuildX
- 硬件:西门子PLC200-Smart、物联网关
- 开发对接之前,需要需要设置PLC起始地址,确保程序读写时有正确的地址偏移。如设置起始地址为VD900(DINT类型,转换为MODBUS地址为40001)。
地址映射:
2、关键代码
链接PLC,实现与PLC的通讯:
<template>
<view class="content flex align_center flex_column page_bg">
<text class="content_title font_bold">测压点数据调校</text>
<view class="item flex align_center flex_column mb_30" @click="goDataMonitor()">
<view class="icon_box flex align_center justify_center circle">
<view class="item_icon circle">
<image src="@/static/start_1.png" mode=""></image>
</view>
</view>
<text class="font_bold">
连接设备
</text>
</view>
<view class="item flex align_center flex_column" @click="exitd">
<view class="icon_box bg_red flex align_center justify_center circle">
<view class="item_icon circle">
<image src="@/static/start_2.png" mode=""></image>
</view>
</view>
<text class="font_bold">
退出
</text>
</view>
</view>
</template>
<script>
const testModbus = uni.requireNativePlugin("firefly-modbus")
const app = getApp()
export default {
data() {
return {
}
},
onLoad() {
},
onShow() {
testModbus.disconnect();
testModbus.disconnectByClientId(1);
},
methods: {
exitd(){
plus.runtime.quit();
},
goDataMonitor(){
testModbus.connect(
{
'clientId' : app.globalData.clientId, // 客户端ID,1-255,默认为1
'unitId' : app.globalData.unitId, // Modbus模块的设备id,默认为1
'host' : app.globalData.host, // PLC的服务IP
'port' : app.globalData.port, // PLC的服务端口
'useRTU' : app.globalData.useRTU,
'reconnectInterval' : app.globalData.reconnectInterval,
},
(result) => {
if(result.code == 0){
uni.showToast({
title:result.msg,
icon:'none',
duration:2000
})
setTimeout(function(){
// console.log(122)
uni.navigateTo({
url:'/pages/dataMonitor/dataMonitor'
})
}, 1500);
}else{
uni.showToast({
title:result.msg,
icon:'none',
duration:2000
})
}
console.log(result);
}
);
// uni.navigateTo({
// url:'/pages/dataMonitor/dataMonitor'
// })
}
}
}
</script>
读取和写入寄存器数值:
uni.getStorage({
key: 'zx1addr',
success: function (res) {
that.zx1addr = res.data.addr
testModbus.checkConnection(app.globalData.clientId, res.data.addr, (result) => {
console.log(result.msg)
if (result.code != 0) {
uni.showToast({
title: result.msg,
icon: 'none',
duration: 2000
});
return;
}
})
testModbus.readRegistersByClientId(app.globalData.clientId, res.data.addr ,2, (res) => {
let data = res.data;
let vd = data[0]*65536+data[1]
that.zx1val = vd
console.log('VD值:'+vd);
})
},
fail: function (err) {
uni.showToast({
title:'请先配置正向一的通道',
icon: 'none'
})
}
});
let high16 = Math.floor(value / 65536);
let low16 = value % 65536;
console.log(high16+'----'+low16);
testModbus.writeRegistersByClientId(app.globalData.clientId, addr, [high16, low16], (res) => {
console.log(res);
console.log(high16+'----'+low16);
uni.showToast({
title:'校准成功',
icon: 'none',
duration:2000
})
})
四、效果展示
各位技术同路人:我是 90 后全栈开发格子衫哥哥(V:qizhijicheng),专治各种协议不通、数据断层和物联网的 “水土不服”。从敲代码到搭系统,从网页到智能,通通不在话下!每一行代码都是我对技术的深情告白,创作不易,还望各位高抬贵手,点赞评论收藏三连。
人间风雨总相欺,且敛眉心意自怡。
慢点烟霞云雾里,千般愁绪化春曦。