From c3bccb98f816eb79eae28274ea1a33e1e4809e65 Mon Sep 17 00:00:00 2001 From: surenjun Date: Fri, 28 Mar 2025 15:49:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=80=E4=BB=A3=E6=9C=BA=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/UdpByOne.ts | 115 ++++++++++---------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/entry/src/main/ets/common/utils/UdpByOne.ts b/entry/src/main/ets/common/utils/UdpByOne.ts index 9383e20f..9ee24aa3 100644 --- a/entry/src/main/ets/common/utils/UdpByOne.ts +++ b/entry/src/main/ets/common/utils/UdpByOne.ts @@ -27,6 +27,8 @@ export default class UdpByOne { // GPS消息 private GPSMsg: any; + private timer: number; + constructor() { this.init() } @@ -49,51 +51,12 @@ export default class UdpByOne { this.GPSUDP.bind({ address: this.LocalIp, port: parseInt(this.GPSLocalIpPort), family: 1 }); - } - // 重新绑定 - public rebind() { - this.PLCUDP.bind(this.LocalIp, this.PLCLocalIpPort); - this.GPSUDP.bind(this.LocalIp, this.GPSLocalIpPort); - } - // PLC发送消息 - public sendPLCMsg(msg: string) { - // this.PLCUDP.send({ - // data: '111111', - // address: { - // address: this.OppositeIp, - // port: parseInt(this.PLCOppositeIpPort), - // } - // }) - } - - // GPS发送消息 - public sendGPSMsg(msg: string) { - // this.GPSUDP.send({ - // data: '111111', - // address: { - // address: this.OppositeIp, - // port: parseInt(this.GPSOppositeIpPort), - // } - // }) - - } - - // 接受消息 - public receiveMsg(callback) { - console.log('heartMsg', 'getCallback') - this.sendPLCMsg('1111') - this.sendGPSMsg('1111') this.PLCUDP.on("message", (res, remoteInfo) => { - console.log('heartMsg', 'getPlc') - console.log('heartMsg', 'getGps') this.PLCMsg = res.message; - // 组合数据 - let newMessage = this.handleMsg() - callback(newMessage) }) + this.GPSUDP.on("message", (res1, remoteInfo) => { - console.log('heartMsg', 'getGps') let dataView = new DataView(res1.message) let str = "" for (let i = 0; i < dataView?.byteLength; ++i) { @@ -103,18 +66,57 @@ export default class UdpByOne { } } this.GPSMsg = str; - if(str.length < 10){ - return - } - let newMessage = this.handleMsg() - callback(newMessage) }) } + // 重新绑定 + public rebind() { + this.PLCUDP.bind(this.LocalIp, this.PLCLocalIpPort); + this.GPSUDP.bind(this.LocalIp, this.GPSLocalIpPort); + } + + // PLC发送消息 + public sendPLCMsg(msg: string) { + this.PLCUDP.send({ + data: '111111', + address: { + address: this.OppositeIp, + port: parseInt(this.PLCOppositeIpPort), + } + }) + } + + // GPS发送消息 + public sendGPSMsg(msg: string) { + this.GPSUDP.send({ + data: '111111', + address: { + address: this.OppositeIp, + port: parseInt(this.GPSOppositeIpPort), + } + }) + } + + // 接受消息 + public receiveMsg(callback):number { + //TODO 临时处理关闭消息接收 + this.cancelMsg(this.timer) + this.timer = setInterval(() => { + let newMessage = this.handleMsg() + callback(newMessage) + }, 200) + return this.timer + } + + //取消订阅 + public cancelMsg(timer:number){ + clearInterval(timer) + } + + // 处理消息 public handleMsg() { let newMessage = PLCGPSData; - console.log('heartMsg000', PLCGPSData) if (this.GPSMsg) { // 使用正则表达式提取$GPGGA消息 let GPGGAMsg = this.GPSMsg.match(/\$GPGGA[^$]*/)[0]; @@ -169,7 +171,6 @@ export default class UdpByOne { if (PLCByteArr.length < 55) { return newMessage.join(",") } - console.log("heartMsgheartMsg1", PLCByteArr.toString()); // 左方向灯 2 newMessage[2] = PLCByteArr[6][5]; // 右方向灯 3 . @@ -229,17 +230,18 @@ export default class UdpByOne { newMessage[26] = parseInt(PLCByteArr[33], 2) + ''; // 方向盘角度 27 // TODO 档位 磁档位为外接信号 - newMessage[28] = parseInt(PLCByteArr[13], 2) + ''; + newMessage[28] = globalThis.chuankoMsg == '0'?(parseInt(PLCByteArr[13], 2) + ''): globalThis.chuankoMsg; + // newMessage[27] = globalThis.chuankoMsg // 超声波1 29 - let Data52 = parseInt(PLCByteArr[52], 2); - let Data53 = parseInt(PLCByteArr[53], 2); - - newMessage[29] = (PLCByteArr[4][1] >0 ? 800 : 0) +'' + newMessage[29] = (PLCByteArr[4][1] >0 ? '300' : '1200') // 超声波2 30 - newMessage[30] = (PLCByteArr[4][0] >0 ? 800:0 )+'' - // 超声波3 31 - // 超声波4 32 + newMessage[30] = (PLCByteArr[4][0] >0 ? '300': '1200' ) + + //TODO 超声波3 临时写死 + newMessage[31] = '1200' + //TODO 超声波4 临时写死 + newMessage[32] = '1200' // 触摸1 33 // 触摸2 34 // 触摸3 35 @@ -271,9 +273,6 @@ export default class UdpByOne { // 已工作时长/设定的工作时长 61 // 改正数数据长度*数据长度-基准站RTCM改正数类型 62 } - - console.log('heartMsgend', newMessage.join(",")) - return newMessage.join(",") }