增加消息队列及重连
This commit is contained in:
+2
-2
@@ -2,8 +2,8 @@
|
|||||||
"name" : "endoscope",
|
"name" : "endoscope",
|
||||||
"appid" : "__UNI__5A0A7D6",
|
"appid" : "__UNI__5A0A7D6",
|
||||||
"description" : "",
|
"description" : "",
|
||||||
"versionName" : "1.2.4",
|
"versionName" : "1.2.5",
|
||||||
"versionCode" : 113,
|
"versionCode" : 114,
|
||||||
"transformPx" : false,
|
"transformPx" : false,
|
||||||
/* 5+App特有相关 */
|
/* 5+App特有相关 */
|
||||||
"app-plus" : {
|
"app-plus" : {
|
||||||
|
|||||||
+6
-6
@@ -190,7 +190,7 @@ export default {
|
|||||||
this.startRS485()
|
this.startRS485()
|
||||||
this.startRS232()
|
this.startRS232()
|
||||||
// 启动心跳检测
|
// 启动心跳检测
|
||||||
this.startHeartbeat()
|
// this.startHeartbeat()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// console.log(e)
|
// console.log(e)
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
@@ -377,11 +377,11 @@ export default {
|
|||||||
this.readData()
|
this.readData()
|
||||||
|
|
||||||
// 串口打开成功后,重置重连计数器和时间戳
|
// 串口打开成功后,重置重连计数器和时间戳
|
||||||
this.reconnectCount = 0
|
// this.reconnectCount = 0
|
||||||
this.last485DataTime = Date.now()
|
// this.last485DataTime = Date.now()
|
||||||
|
|
||||||
// 记录通信恢复日志
|
// 记录通信恢复日志
|
||||||
this.addLog('485通信', '通信已恢复(重连成功)')
|
// this.addLog('485通信', '通信已恢复(重连成功)')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async startRS232() {
|
async startRS232() {
|
||||||
@@ -526,9 +526,9 @@ export default {
|
|||||||
|
|
||||||
readData() {
|
readData() {
|
||||||
this.timer = setInterval(async () => {
|
this.timer = setInterval(async () => {
|
||||||
cmd.getTemp()
|
await cmd.getTemp()
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
cmd.getPressure()
|
await cmd.getPressure()
|
||||||
}, 5000)
|
}, 5000)
|
||||||
},
|
},
|
||||||
async handle485HexData(hex) {
|
async handle485HexData(hex) {
|
||||||
|
|||||||
+98
-13
@@ -16,8 +16,94 @@
|
|||||||
// let RS232 = getApp().globalData.RS232
|
// let RS232 = getApp().globalData.RS232
|
||||||
import store from '@/store/index.js'
|
import store from '@/store/index.js'
|
||||||
|
|
||||||
|
// ========== RS485 优先级消息队列 START ==========
|
||||||
|
const PRIORITY = {
|
||||||
|
HIGH: 0, // 用户操作(开门/关门/控制指令)
|
||||||
|
LOW: 1, // 定时轮询(getTemp/getPressure)
|
||||||
|
}
|
||||||
|
|
||||||
|
const rs485Queue = []
|
||||||
|
let isRS485Sending = false
|
||||||
|
const RS485_SEND_DELAY = 200 // 每条指令间隔200ms,确保总线空闲
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将任务按优先级插入队列
|
||||||
|
* 高优先级(0)插入到队列前面低优先级任务之前
|
||||||
|
* 低优先级(1)追加到队列末尾
|
||||||
|
*/
|
||||||
|
const enqueueTask = (task) => {
|
||||||
|
if (task.priority === PRIORITY.HIGH) {
|
||||||
|
let insertIndex = rs485Queue.findIndex(t => t.priority === PRIORITY.LOW)
|
||||||
|
if (insertIndex === -1) {
|
||||||
|
rs485Queue.push(task)
|
||||||
|
} else {
|
||||||
|
rs485Queue.splice(insertIndex, 0, task)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rs485Queue.push(task)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const processRS485Queue = async () => {
|
||||||
|
if (isRS485Sending || rs485Queue.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
isRS485Sending = true
|
||||||
|
const task = rs485Queue.shift()
|
||||||
|
try {
|
||||||
|
let RS485 = getApp().globalData.RS485
|
||||||
|
if (!RS485) {
|
||||||
|
throw new Error('RS485 not initialized')
|
||||||
|
}
|
||||||
|
const result = RS485.sendDataString(task.cmd)
|
||||||
|
if (result && typeof result.then === 'function') {
|
||||||
|
await result
|
||||||
|
}
|
||||||
|
// 发送后延迟,确保总线空闲再发下一条
|
||||||
|
await new Promise(r => setTimeout(r, RS485_SEND_DELAY))
|
||||||
|
task.resolve()
|
||||||
|
} catch (error) {
|
||||||
|
console.error('RS485 send error:', error)
|
||||||
|
task.reject(error)
|
||||||
|
} finally {
|
||||||
|
isRS485Sending = false
|
||||||
|
processRS485Queue()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送RS485指令(带优先级)
|
||||||
|
* @param {string} cmd - 十六进制指令字符串
|
||||||
|
* @param {number} priority - 优先级,默认HIGH
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
const enqueueRS485Send = (cmd, priority = PRIORITY.HIGH) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
enqueueTask({ cmd, priority, resolve, reject })
|
||||||
|
processRS485Queue()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空队列(重连时调用,拒绝所有待发送请求)
|
||||||
|
*/
|
||||||
|
const clearRS485Queue = () => {
|
||||||
|
while (rs485Queue.length > 0) {
|
||||||
|
const task = rs485Queue.shift()
|
||||||
|
task.reject(new Error('RS485队列已清空(重连中)'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前队列长度(调试用)
|
||||||
|
*/
|
||||||
|
const getRS485QueueSize = () => {
|
||||||
|
return rs485Queue.length
|
||||||
|
}
|
||||||
|
// ========== RS485 优先级消息队列 END ==========
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
// 开左门指令 (通过RS485站号03)
|
// 开左门指令 (通过RS485站号03) - 高优先级
|
||||||
// 中盛4路数字IO模块,功能码06(写单个保持寄存器)
|
// 中盛4路数字IO模块,功能码06(写单个保持寄存器)
|
||||||
LeftDoor: async (status) => {
|
LeftDoor: async (status) => {
|
||||||
// 左门开门: 03 06 00 00 00 01 49 E8
|
// 左门开门: 03 06 00 00 00 01 49 E8
|
||||||
@@ -26,12 +112,10 @@ export default {
|
|||||||
store.state.relay.leftDoor = status
|
store.state.relay.leftDoor = status
|
||||||
// door状态 = leftDoor OR rightDoor (任一门打开则door为true)
|
// door状态 = leftDoor OR rightDoor (任一门打开则door为true)
|
||||||
store.state.relay.door = store.state.relay.leftDoor || store.state.relay.rightDoor
|
store.state.relay.door = store.state.relay.leftDoor || store.state.relay.rightDoor
|
||||||
let RS485 = getApp().globalData.RS485
|
await enqueueRS485Send(cmd, PRIORITY.HIGH)
|
||||||
await RS485.sendDataString(cmd)
|
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// 开右门指令 (通过RS485站号03)
|
// 开右门指令 (通过RS485站号03) - 高优先级
|
||||||
// 中盛4路数字IO模块,功能码06(写单个保持寄存器)
|
// 中盛4路数字IO模块,功能码06(写单个保持寄存器)
|
||||||
RightDoor: async (status) => {
|
RightDoor: async (status) => {
|
||||||
// 右门开门: 03 06 00 01 00 01 18 28
|
// 右门开门: 03 06 00 01 00 01 18 28
|
||||||
@@ -40,8 +124,7 @@ export default {
|
|||||||
store.state.relay.rightDoor = status
|
store.state.relay.rightDoor = status
|
||||||
// door状态 = leftDoor OR rightDoor (任一门打开则door为true)
|
// door状态 = leftDoor OR rightDoor (任一门打开则door为true)
|
||||||
store.state.relay.door = store.state.relay.leftDoor || store.state.relay.rightDoor
|
store.state.relay.door = store.state.relay.leftDoor || store.state.relay.rightDoor
|
||||||
let RS485 = getApp().globalData.RS485
|
await enqueueRS485Send(cmd, PRIORITY.HIGH)
|
||||||
await RS485.sendDataString(cmd)
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// 开门关门控制 01
|
// 开门关门控制 01
|
||||||
@@ -84,20 +167,22 @@ export default {
|
|||||||
let RS232 = getApp().globalData.RS232
|
let RS232 = getApp().globalData.RS232
|
||||||
await RS232.sendDataString(cmd);
|
await RS232.sendDataString(cmd);
|
||||||
},
|
},
|
||||||
// 获取温湿度
|
// 获取温湿度(通过RS485站号02)- 低优先级
|
||||||
getTemp: () => {
|
getTemp: () => {
|
||||||
let RS485 = getApp().globalData.RS485
|
|
||||||
let cmd = '020300000002C438';
|
let cmd = '020300000002C438';
|
||||||
RS485.sendDataString(cmd);
|
return enqueueRS485Send(cmd, PRIORITY.LOW)
|
||||||
},
|
},
|
||||||
|
|
||||||
// 获取压差指令
|
// 获取压差指令(通过RS485站号01)- 低优先级
|
||||||
getPressure: () => {
|
getPressure: () => {
|
||||||
let RS485 = getApp().globalData.RS485
|
|
||||||
let cmd = '01030001000295CB'
|
let cmd = '01030001000295CB'
|
||||||
RS485.sendDataString(cmd);
|
return enqueueRS485Send(cmd, PRIORITY.LOW)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 导出队列管理方法
|
||||||
|
clearRS485Queue,
|
||||||
|
getRS485QueueSize,
|
||||||
|
|
||||||
// 解析门卡数据,返回卡号
|
// 解析门卡数据,返回卡号
|
||||||
parse232dData: (hexString) => {
|
parse232dData: (hexString) => {
|
||||||
// IC卡 20 01 00 08 04 00 00 00 0e 26 fe ab 8f 03
|
// IC卡 20 01 00 08 04 00 00 00 0e 26 fe ab 8f 03
|
||||||
|
|||||||
Reference in New Issue
Block a user