This commit is contained in:
Silent YANG 2024-11-17 07:14:03 +08:00
parent 05d5bbbf6a
commit 38c134f2c0
3 changed files with 59 additions and 13 deletions

BIN
public/sms_test.bin Normal file

Binary file not shown.

View file

@ -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;

View file

@ -3,7 +3,7 @@
<Breadcrumb :items="[$t('menu.list'), $t('menu.flash')]" />
<a-card class="general-card">
<template #title>
开发中无线电聊天需使用<a-link @click="downloadFirmware">这个</a-link>固件
无法正常使用开发中无线电聊天需使用<a-link @click="downloadFirmware">这个</a-link>固件
</template>
<div style="display: flex; align-items: center; margin: 10px;">
<span>呼号</span>
@ -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;
}
}