2.7.1
This commit is contained in:
+27
-3
@@ -15,6 +15,8 @@
|
||||
// 卡片放入和离开 起始位20, 所有卡片通过同一通道读取,通过数据库比对判断类型
|
||||
// let RS232 = getApp().globalData.RS232
|
||||
import store from '@/store/index.js'
|
||||
import storage from '@/utils/storage.js'
|
||||
import tcp485 from '@/utils/tcp485.js'
|
||||
|
||||
// ========== 统一消息队列 START ==========
|
||||
// RS485 和 RS232 共用同一队列,通过 type 字段区分串口实例
|
||||
@@ -30,6 +32,19 @@ const SEND_DELAY = 150 // 发送间隔150ms
|
||||
const MAX_QUEUE_SIZE = 5 // 队列最大长度
|
||||
const SEND_TIMEOUT = 1000 // 统一超时1秒
|
||||
|
||||
/**
|
||||
* 获取当前485通信实例(根据模式动态选择)
|
||||
* @returns {object} RS485实例或TCP485实例
|
||||
*/
|
||||
const get485Instance = () => {
|
||||
const mode = storage.get('rs485Mode') || 'serial'
|
||||
if (mode === 'tcp') {
|
||||
return tcp485
|
||||
} else {
|
||||
return getApp().globalData.RS485
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将任务按优先级插入队列
|
||||
* 规则:
|
||||
@@ -106,7 +121,7 @@ const sendWithTimeout = (instance, cmd) => {
|
||||
|
||||
/**
|
||||
* 处理队列中的下一个任务
|
||||
* 根据 task.type 自动获取对应串口实例(RS485 或 RS232)
|
||||
* 根据 task.type 自动获取对应串口实例(RS485/RS232/TCP485)
|
||||
*/
|
||||
const processQueue = async () => {
|
||||
if (isSending || queue.length === 0) {
|
||||
@@ -116,8 +131,17 @@ const processQueue = async () => {
|
||||
const task = queue.shift()
|
||||
|
||||
try {
|
||||
// 根据 type 获取对应的串口实例
|
||||
let instance = getApp().globalData[task.type]
|
||||
// 根据 type 获取对应的通信实例
|
||||
let instance = null
|
||||
|
||||
if (task.type === 'RS485') {
|
||||
// 动态选择485通信实例(串口或TCP)
|
||||
instance = get485Instance()
|
||||
} else if (task.type === 'RS232') {
|
||||
// RS232始终使用串口
|
||||
instance = getApp().globalData.RS232
|
||||
}
|
||||
|
||||
if (!instance) {
|
||||
throw new Error(`${task.type} not initialized`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user