diff --git a/public/sms_test.bin b/public/sms_test.bin new file mode 100644 index 0000000..dea7a28 Binary files /dev/null and b/public/sms_test.bin differ diff --git a/src/utils/serial.js b/src/utils/serial.js index 94ecfd7..a6b427a 100644 --- a/src/utils/serial.js +++ b/src/utils/serial.js @@ -749,10 +749,14 @@ const FONT_MAPPING_118_MAP = ['啊', '阿', '埃', '挨', '哎', '唉', '哀', ' let globalWriteReader = null let globalReadReader = null -function globalRelease(){ +function globalRelease(target = 'all'){ try { - if(globalReadReader)globalReadReader.releaseLock() - if(globalWriteReader)globalWriteReader.releaseLock() + if(target != 'read'){ + if(globalWriteReader)globalWriteReader.releaseLock() + } + if(target != 'write'){ + if(globalReadReader)globalReadReader.releaseLock() + } } catch {} } @@ -869,7 +873,7 @@ async function readPacket(port, expectedData, timeout = 1000) { return unpacketize(new Uint8Array(sessionStorage.getItem('webusb').split(','))); } // Create a reader to read data from the serial port - globalRelease() + globalRelease('read') const reader = port.readable.getReader(); globalReadReader = reader; let buffer = new Uint8Array(); @@ -974,7 +978,7 @@ async function readPacket(port, expectedData, timeout = 1000) { */ async function readPacketNoVerify(port, timeout = 1000) { // Create a reader to read data from the serial port - globalRelease() + globalRelease('read') const reader = port.readable.getReader(); globalReadReader = reader; let buffer = new Uint8Array(); @@ -1063,7 +1067,7 @@ async function sendPacket(port, data) { } try { // create writer for port - globalRelease() + globalRelease('write') const writer = port.writable.getWriter(); globalWriteReader = writer; // prepare packet @@ -1514,12 +1518,11 @@ function unpackVersion(encoded_firmware) { async function sendSMSPacket(port, message){ try { - globalRelease() + globalRelease('write') const writer = port.writable.getWriter(); globalWriteReader = writer; const formattedMessage = `SMS:${message}\r\n`; const packet = new TextEncoder().encode(formattedMessage); - console.log(packet) await writer.write(packet); writer.releaseLock(); } catch (error) { @@ -1530,7 +1533,7 @@ async function sendSMSPacket(port, message){ } function readSMSPacket(port) { - globalRelease() + globalRelease('read') const reader = port.readable.getReader(); globalReadReader = reader; return reader; diff --git a/src/views/list/chat/index.vue b/src/views/list/chat/index.vue index 9eef603..65a9d16 100644 --- a/src/views/list/chat/index.vue +++ b/src/views/list/chat/index.vue @@ -3,7 +3,7 @@
呼号: @@ -41,10 +41,12 @@ import { computed, reactive, watch, nextTick, ref, onUnmounted } from 'vue'; import { eeprom_init, sendSMSPacket, readSMSPacket } from '@/utils/serial.js'; import { useAppStore } from '@/store'; +import { useRouter } from 'vue-router'; import { Message, Modal } from '@arco-design/web-vue'; import { useI18n } from 'vue-i18n'; const { t } = useI18n(); +const router = useRouter() const appStore = useAppStore(); const msgList: any = ref(null) @@ -90,7 +92,29 @@ const scrollIt = () => { } const downloadFirmware = () => { - Message.error('敬请期待') + router.push({ + path: '/tool/flash', + query: { + url: "/sms_test.bin" + } + }); +} + +function stringToUtf8Hex(str: string) { + const encoder = new TextEncoder(); + const utf8Bytes = encoder.encode(str); + let hex = ''; + utf8Bytes.forEach(byte => { + hex += byte.toString(16).padStart(2, '0').toUpperCase(); + }); + return hex; +} + +function hexToUtf8String(hex: string) { + // 将十六进制字符串转换为字节数组 + const bytes = new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); + const decoder = new TextDecoder(); + return decoder.decode(bytes); } const sendMsg = async () => { @@ -99,8 +123,7 @@ const sendMsg = async () => { Message.error('请输入正确的呼号') return } - console.log(state.callsign.trim() + ":" + state.devid + ":" + state.sendInput.trim()) - sendSMSPacket(appStore.connectPort, state.callsign.trim() + ":" + state.devid + ":" + state.sendInput.trim()) + sendSMSPacket(appStore.connectPort,"START:" + state.callsign.trim() + ":" + state.devid + ":" + stringToUtf8Hex(state.sendInput.trim()) + ":END") state.msgList.push({ callsign: state.callsign.trim(), devid: state.devid, @@ -128,6 +151,7 @@ const connectIt = async () => { const asyncMsg = async (reader: any) => { let buffer = ''; const decoder = new TextDecoder(); + const regex = /START:[A-Z0-9]+:[0-9]+:[A-F0-9]+:END/g; while (true) { try { const { value, done } = await reader.read(); @@ -138,7 +162,26 @@ const asyncMsg = async (reader: any) => { message = message.trim(); buffer += message; console.log(buffer) + const matches = buffer.match(regex); + if (matches && matches.length > 0) { + buffer = '' + try { + const content = matches[matches.length - 1].split(':'); + state.msgList.push({ + callsign: content[1], + devid: content[2], + content: hexToUtf8String(content[3]) + }) + scrollIt() + } catch { + console.log('ERROR') + } + } } catch (error) { + console.log('已断开') + state.connect = false + state.reader.releaseLock(); + state.startChat = '开始聊天' break; } }