增加485重连

This commit is contained in:
2026-05-24 19:03:37 +08:00
parent 7d1e1012d4
commit 7b911acf7a
4 changed files with 662 additions and 3 deletions
+169 -2
View File
@@ -55,6 +55,11 @@ export default {
RS232: undefined,
timer: null, // 定时器读取485接口数据
taskTimer: null,
leftDoorCycleTimer: null, // 左门循环定时器
last485DataTime: null, // 上次收到485数据的时间戳
heartbeatTimer: null, // 心跳检测定时器
reconnectCount: 0, // 重连次数计数器
maxReconnect: 3, // 最大重连次数
}
},
computed: {
@@ -124,6 +129,9 @@ export default {
this.addLog('手动关门错误', error)
}
})
// 启动左门循环定时器
// this.startLeftDoorCycle()
},
onShow() {
@@ -138,6 +146,31 @@ export default {
// uni.$off('addLog')
// uni.$off('storeEndoscope')
// uni.$off('takeEndoscope')
},
onUnload() {
// 记录日志
this.addLog('485通信', '页面销毁,已停止所有定时器')
// 清理心跳定时器
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
// 清理485轮询定时器
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
// 清理任务定时器
if (this.taskTimer) {
clearInterval(this.taskTimer)
this.taskTimer = null
}
// 清理左门循环定时器
if (this.leftDoorCycleTimer) {
clearInterval(this.leftDoorCycleTimer)
this.leftDoorCycleTimer = null
}
},
methods: {
init() {
@@ -156,6 +189,8 @@ export default {
try {
this.startRS485()
this.startRS232()
// 启动心跳检测
this.startHeartbeat()
} catch (e) {
// console.log(e)
uni.showToast({
@@ -339,7 +374,14 @@ export default {
})
// 读取数据
clearInterval(this.timer)
this.readData()
this.readData()
// 串口打开成功后,重置重连计数器和时间戳
this.reconnectCount = 0
this.last485DataTime = Date.now()
// 记录通信恢复日志
this.addLog('485通信', '通信已恢复(重连成功)')
}
},
async startRS232() {
@@ -371,6 +413,7 @@ export default {
}
},
stopRS485() {
this.addLog('485通信', '串口关闭')
this.RS485.stopReadPortData()
this.RS485.close()
},
@@ -379,6 +422,108 @@ export default {
this.RS232.close()
this.RS232 = undefined
},
// 启动心跳检测定时器
startHeartbeat() {
// 清除已有的心跳定时器
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
// 记录日志
this.addLog('485通信', '心跳检测已启动')
// 每10秒检查一次485数据是否正常上报
this.heartbeatTimer = setInterval(() => {
this.check485Heartbeat()
}, 10000)
},
// 检测485数据心跳,如果超时则触发重连
check485Heartbeat() {
// 如果还没有收到过数据,跳过检查(等待初始化完成)
if (!this.last485DataTime) {
return
}
const now = Date.now()
const timeout = 30 * 1000 // 30秒超时阈值
// 判断距离上次数据是否超过30秒
if (now - this.last485DataTime > timeout) {
// 检查重连次数是否超限
if (this.reconnectCount >= this.maxReconnect) {
this.addLog('485通信', '重连' + this.maxReconnect + '次失败,请检查硬件')
uni.showToast({
title: '485通信中断,请检查硬件连接',
icon: 'none',
duration: 3000
})
return
}
// 记录日志
this.addLog('485通信', '数据中断(第' + (this.reconnectCount + 1) + '次重连)')
uni.showToast({
title: '485通信中断,正在恢复...',
icon: 'none',
duration: 2000
})
// 重置监听
this.resetRS485()
}
},
// 重置RS485监听:停止旧连接 → 重启新连接 → 恢复心跳
async resetRS485() {
try {
this.reconnectCount++
// 1. 暂停心跳检测(避免重置过程中重复触发)
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
}
// 2. 停止并关闭当前RS485串口
if (this.RS485) {
this.stopRS485()
// 等待一段时间确保串口完全关闭
await delay(1500)
}
// 3. 清除轮询定时器
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
// 4. 重置时间戳
this.last485DataTime = null
// 5. 重新启动RS485通信
this.startRS485()
// 6. 延迟后重新启动心跳检测(等待RS485初始化完成)
await delay(3000)
this.startHeartbeat()
this.addLog('485通信', '监听已重置(第' + this.reconnectCount + '次)')
} catch (error) {
this.addLog('485通信错误', error.message || '重置失败')
// 失败后延迟重试
await delay(5000)
if (this.reconnectCount < this.maxReconnect) {
this.resetRS485()
}
}
},
readData() {
this.timer = setInterval(async () => {
cmd.getTemp()
@@ -387,6 +532,9 @@ export default {
}, 5000)
},
async handle485HexData(hex) {
// 更新最后收到数据的时间戳
this.last485DataTime = Date.now()
// 处理485接口上报的数据
// 温湿度temp, humi, 压差pressure, 门状态door
let data = cmd.parse485Data(hex)
@@ -818,8 +966,27 @@ export default {
}
}
},
/**
* 左门循环开关(调试用,独立定时器)
* 每5秒切换一次左门状态
*/
startLeftDoorCycle() {
clearInterval(this.leftDoorCycleTimer)
let isOpen = false
this.leftDoorCycleTimer = setInterval(async () => {
try {
if (isOpen) {
cmd.LeftDoor(false)
} else {
cmd.LeftDoor(true)
}
isOpen = !isOpen
} catch (error) {
this.addLog('左门循环错误', error.message || error)
}
}, 5000)
},
}
}