From 2d1cadbbe3799b4b9428a2274a790f269339ea53 Mon Sep 17 00:00:00 2001 From: wangzhongjie Date: Mon, 17 Feb 2025 16:41:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=B9=E6=8E=A51=E4=BB=A3=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/UdpByOne.ts | 74 +++++++++++---------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/entry/src/main/ets/common/utils/UdpByOne.ts b/entry/src/main/ets/common/utils/UdpByOne.ts index 04b6de3b..093f1a36 100644 --- a/entry/src/main/ets/common/utils/UdpByOne.ts +++ b/entry/src/main/ets/common/utils/UdpByOne.ts @@ -5,19 +5,19 @@ export default class UdpByOne { // PLC udp private PLCUDP: any; // PLC localIp - private PLCLocalIp: string='192.168.7.170'; + private LocalIp: string = '192.168.7.170'; // PLC localIpPort - private PLCLocalIpPort: string='31012'; + private PLCLocalIpPort: string = '31012'; + private OppositeIp: string = '192.168.7.124' // PLC oppositeIpPort - private PLCOppositeIpPort: string='30012'; + private PLCOppositeIpPort: string = '30012'; // PLC消息 private PLCMsg: number[]; // GPS udp private GPSUDP: any; // GPS localIp - private GPSLocalIp: string='192.168.7.124'; // GPS localIpPort - private GPSLocalIpPort: string='30013'; + private GPSLocalIpPort: string = '30013'; // GPS oppositeIpPort private GPSOppositeIpPort: string; // GPS消息 @@ -28,17 +28,19 @@ export default class UdpByOne { this.PLCUDP = socket.constructUDPSocketInstance(); // this.PLCUDP.bind(this.PLCLocalIp, this.PLCLocalIpPort); this.PLCUDP.bind({ - // address: '192.168.7.170', port: 20122, family: 1 - address: this.PLCLocalIp, port: parseInt(this.PLCLocalIpPort), family: 1 + address: this.LocalIp, port: parseInt(this.PLCLocalIpPort), family: 1 }); - // this.GPSUDP = socket.constructUDPSocketInstance(); + this.GPSUDP = socket.constructUDPSocketInstance(); // this.GPSUDP.bind(this.GPSLocalIp, this.GPSLocalIpPort); + this.GPSUDP.bind({ + address: this.LocalIp, port: parseInt(this.GPSLocalIpPort), family: 1 + }); } // 重新绑定 public rebind() { - this.PLCUDP.bind(this.PLCLocalIp, this.PLCLocalIpPort); - this.GPSUDP.bind(this.GPSLocalIp, this.GPSLocalIpPort); + this.PLCUDP.bind(this.LocalIp, this.PLCLocalIpPort); + this.GPSUDP.bind(this.LocalIp, this.GPSLocalIpPort); } // PLC发送消息 @@ -46,36 +48,42 @@ export default class UdpByOne { this.PLCUDP.send({ data: '111111', address: { - address: '192.168.7.124', + address: this.OppositeIp, port: parseInt(this.PLCOppositeIpPort), } }) - // this.PLCUDP.sendTo(msg, this.PLCOppositeIpPort); } // GPS发送消息 public sendGPSMsg(msg: string) { - this.GPSUDP.sendTo(msg, this.GPSOppositeIpPort); + this.GPSUDP.send({ + data: '111111', + address: { + address: this.OppositeIp, + port: parseInt(this.PLCOppositeIpPort), + } + }) } // 接受消息 public receiveMsg(callback) { - console.log('heartMsg','getCallback') + console.log('heartMsg', 'getCallback') this.sendPLCMsg('1111') - this.PLCUDP.on("message", (message2, remoteInfo) => { - console.log('heartMsg','getPlc') - - this.PLCMsg = message2; - // 组合数据 - let newMessage = this.handleMsg() - callback(newMessage) - }) - return - this.GPSUDP.on("message", (message1, remoteInfo) => { - console.log('heartMsg','GPSUDP') - this.GPSMsg = message1; - this.PLCUDP.on("message", (message2, remoteInfo) => { - this.PLCMsg = message2; + this.sendGPSMsg('1111') + // this.PLCUDP.on("message", (res, remoteInfo) => { + // console.log('heartMsg', 'getPlc') + // + // this.PLCMsg = res.message; + // // 组合数据 + // let newMessage = this.handleMsg() + // callback(newMessage) + // }) + // return + this.GPSUDP.on("message", (res1, remoteInfo) => { + console.log('heartMsg', 'GPSUDP') + this.GPSMsg = res1.message; + this.PLCUDP.on("message", (res2, remoteInfo) => { + this.PLCMsg = res2.message; // 组合数据 let newMessage = this.handleMsg() callback(newMessage) @@ -85,15 +93,11 @@ export default class UdpByOne { // 处理消息 public handleMsg() { - // this.GPSMsg = `$GPGGA,021126.00,2955.5885178,N,11953.8931034,E,5,12,0.8,13.191,M,6.838,M,2.000,0000*49 - // $GPRMC,021126.00,A,2955.5885178,N,11953.8931034,E,4.881,318.3,170623,0.0,E,F*37 - // $GPGST,021126.00,3.30,1.77,3.30,0.0000,1.77,1.25,3.30*67 - // $PTNL,AVR,021126.00,+47.3119,Yaw,+0.4832,Tilt,,,0.817,3,1.7,25*09` let newMessage = PLCGPSData; - console.log('heartMsg000',PLCGPSData) + console.log('heartMsg000', PLCGPSData) // 海拔高度 // 原始GPS消息字符串 - this.GPSMsg = `$GPGGA,021126.00,2955.5885178,N,11953.8931034,E,5,12,0.8,13.191,M,6.838,M,2.000,0000*49$GPRMC,021126.00,A,2955.5885178,N,11953.8931034,E,4.881,318.3,170623,0.0,E,F*37$GPGST,021126.00,3.30,1.77,3.30,0.0000,1.77,1.25,3.30*67$PTNL,AVR,021126.00,+47.3119,Yaw,+0.4832,Tilt,,,0.817,3,1.7,25*09`; + // this.GPSMsg = `$GPGGA,021126.00,2955.5885178,N,11953.8931034,E,5,12,0.8,13.191,M,6.838,M,2.000,0000*49$GPRMC,021126.00,A,2955.5885178,N,11953.8931034,E,4.881,318.3,170623,0.0,E,F*37$GPGST,021126.00,3.30,1.77,3.30,0.0000,1.77,1.25,3.30*67$PTNL,AVR,021126.00,+47.3119,Yaw,+0.4832,Tilt,,,0.817,3,1.7,25*09`; // 使用正则表达式提取$GPGGA消息 let GPGGAMsg = this.GPSMsg.match(/\$GPGGA[^$]*/)[0]; let GPGGAMsgArr = GPGGAMsg.split(",").slice(0, 15); @@ -240,7 +244,7 @@ export default class UdpByOne { // GPS错误次数 60 // 已工作时长/设定的工作时长 61 // 改正数数据长度*数据长度-基准站RTCM改正数类型 62 - console.log('heartMsgend',newMessage.join(",")) + console.log('heartMsgend', newMessage.join(",")) return newMessage.join(",") }