This commit is contained in:
2026-06-02 15:18:26 +08:00
parent 05a72ac7dd
commit 67d64cc02d
4 changed files with 494 additions and 65 deletions
+86 -60
View File
@@ -58,7 +58,7 @@ export default {
last485DataTime: null, // 上次收到485数据的时间戳
heartbeatTimer: null, // 心跳检测定时器
reconnectCount: 0, // 重连次数计数器
maxReconnect: 3, // 最大重连次数
maxReconnect: 5, // 最大重连次数
}
},
computed: {
@@ -180,9 +180,7 @@ export default {
try {
this.startRS485()
this.startRS232()
} catch (e) {
uni.showToast({
title: '初始化串口失败',
icon: 'error'
@@ -357,10 +355,15 @@ export default {
icon: 'none'
})
this.RS485.startAutoReadData((res) =>{
// console.log('485 data', res)
// 转换成十六进制字符串
let hex = this.RS485.byte2HexString(res)
this.handle485HexData(hex)
try {
// console.log('485 data', res)
// 转换成十六进制字符串
let hex = this.RS485.byte2HexString(res)
this.handle485HexData(hex)
} catch (e) {
// console.error('[RS485回调异常]', e)
this.addLog('485回调异常', e.message || e)
}
})
// 读取数据
clearInterval(this.timer)
@@ -405,9 +408,14 @@ export default {
icon: 'none'
})
this.RS232.startAutoReadData((res) =>{
// 转换成十六进制字符串
let hex = this.RS232.byte2HexString(res)
this.handle232HexData(hex)
try {
// 转换成十六进制字符串
let hex = this.RS232.byte2HexString(res)
this.handle232HexData(hex)
} catch (e) {
// console.error('[RS232回调异常]', e)
this.addLog('232回调异常', e.message || e)
}
})
this.startAutoTask()
}
@@ -421,8 +429,7 @@ export default {
},
stopRS232() {
this.RS232.stopReadPortData()
this.RS232.close()
this.RS232 = undefined
this.RS232.close()
},
// 启动心跳检测定时器
@@ -445,8 +452,7 @@ export default {
if (!this.heartbeatTimer) {
// 心跳未运行
return
}
}
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
this.addLog('485通信', '心跳检测已停止')
@@ -458,9 +464,8 @@ export default {
if (!this.last485DataTime) {
return
}
const now = Date.now()
const timeout = 30 * 1000 // 30秒超时阈值
const timeout = 10000 // 10秒超时阈值
// 判断距离上次数据是否超过30秒
if (now - this.last485DataTime > timeout) {
@@ -484,56 +489,82 @@ export default {
duration: 2000
})
// 重置监听
this.resetRS485()
// 统一重置双串口
this.resetSerialPorts()
}
},
// 重置RS485监听:停止旧连接 → 重启新连接 → 恢复心跳
async resetRS485() {
/**
* 统一双串口重连入口
* 执行顺序: 停定时器 → 停双串口 → 清空485队列 → 等待释放 → 重启485 → 延迟→ 重启232
* 失败处理: 指数退避重试 / 耗尽则 plus.runtime.restart()
*/
async resetSerialPorts() {
try {
this.reconnectCount++
// 1. 暂停心跳检测(避免重置过程中重复触发)
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer)
this.heartbeatTimer = null
// ===== Step 1: 停止所有定时器(必须最先执行!)=====
// T1: 心跳检测定时器
if (this.heartbeatTimer) {
clearInterval(this.heartbeatTimer);
this.heartbeatTimer = null
}
// T2: 485轮询定时器
if (this.timer) {
clearInterval(this.timer);
this.timer = null
}
// T3: 业务任务定时器(关键!原resetRS485遗漏此项)
if (this.taskTimer) {
clearInterval(this.taskTimer);
this.taskTimer = null
}
// 2. 停止并关闭当前RS485串口
if (this.RS485) {
this.stopRS485()
// 等待一段时间确保串口完全关闭
await delay(1500)
// ===== Step 2: 停止两个串口 =====
if (this.RS485) {
this.RS485.stopReadPortData();
this.RS485.close()
}
if (this.RS232) {
this.RS232.stopReadPortData();
this.RS232.close()
}
// 3. 清除轮询定时器
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
// 4. 清空RS485队列中积压的失效指令
// ===== Step 3: 清空RS485消息队列(RS232无队列,无需清理)=====
cmd.clearRS485Queue()
// 5. 重置时间戳
// ===== Step 4: 等待端口释放(指数退避,最少2500ms)=====
const backoff = Math.min(1000 * Math.pow(2, this.reconnectCount - 1), 30000)
const waitTime = Math.max(backoff, 2500)
console.log(`[重连] 第${this.reconnectCount}次, 等待${waitTime}ms...`)
await delay(waitTime)
// ===== Step 5: 重置时间戳 =====
this.last485DataTime = null
// 6. 重新启动RS485通信
// ===== Step 6: 依次重启两个串口 =====
// 先启动RS485(会自动重建T1心跳 + T2轮询)
this.startRS485()
// 错开启动时间,避免同时打开端口竞争
await delay(500)
// 再启动RS232(会自动重建T3 taskTimer
this.startRS232()
this.addLog('串口通信', `双串口重连完成(第${this.reconnectCount}次)`)
} catch (error) {
this.addLog('485通信错误', error.message || '重置失败')
this.addLog('串口通信错误', error.message || '重置失败')
// 失败后延迟重试
await delay(5000)
if (this.reconnectCount < this.maxReconnect) {
// 还有重连机会,继续重连
this.resetRS485()
this.resetSerialPorts()
} else {
// 重连次数耗尽,自动重启应用(彻底恢复native层状态)
this.addLog('485通信', `重连${this.maxReconnect}次均失败,即将自动重启...`)
// 重连次数耗尽,自动重启应用
this.addLog('串口通信', `重连${this.maxReconnect}次均失败,即将自动重启应用`)
uni.showToast({
title: '通信异常,3秒后重启',
icon: 'none',
@@ -556,7 +587,7 @@ export default {
await cmd.getPressure()
}, 5000)
},
async handle485HexData(hex) {
handle485HexData(hex) {
// 更新最后收到数据的时间戳
this.last485DataTime = Date.now()
@@ -582,8 +613,6 @@ export default {
// 左门触点触发(关门)
// 调用cmd.LeftDoor(false)来更新门状态
cmd.LeftDoor(false)
// 触发关门事件
// this.closeDoorEvent()
uni.showToast({
title: '左门已关闭',
icon: 'none'
@@ -594,8 +623,6 @@ export default {
// 右门触点触发(关门)
// 调用cmd.RightDoor(false)来更新门状态
cmd.RightDoor(false)
// 触发关门事件
// this.closeDoorEvent()
uni.showToast({
title: '右门已关闭',
icon: 'none'
@@ -604,11 +631,10 @@ export default {
}
if (data.action == 'door' && data.status == 'closed') {
try {
await cmd.LeftDoor(false)
await delay(200)
await cmd.RightDoor(false)
// 触发关门事件
await this.closeDoorEvent()
cmd.LeftDoor(false)
cmd.RightDoor(false)
// 触发关门事件,不需要重复触发,监听器触发
// this.closeDoorEvent()
uni.showToast({
title: '两门已关闭',
icon: 'none'
@@ -764,12 +790,12 @@ export default {
closePop() {
this.$refs.popup.close()
},
send485Data(cmd) {
this.RS485.sendDataString(cmd);
},
async send232Data(cmd) {
await this.RS232.sendDataString(cmd);
},
// send485Data(cmd) {
// this.RS485.sendDataString(cmd);
// },
// async send232Data(cmd) {
// await this.RS232.sendDataString(cmd);
// },
// 开门事件
async openDoorEvent() {
// 关闭真空泵和消毒,打开照明,打开门锁,打开风机