From 600dfbe397e1f02432e71a7be1a4a44657bf1827 Mon Sep 17 00:00:00 2001 From: lvyuankang <1344032923@qq.com> Date: Thu, 30 May 2024 15:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=A6=E5=92=8C=E4=B8=B2=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/common/service/indexService.ts | 28 +++-- entry/src/main/ets/common/utils/GlobleTcp.ts | 112 +++++++++--------- entry/src/main/ets/common/utils/GlobleUdp.ts | 32 ++--- 3 files changed, 93 insertions(+), 79 deletions(-) diff --git a/entry/src/main/ets/common/service/indexService.ts b/entry/src/main/ets/common/service/indexService.ts index 5846eb3c..cc801a80 100644 --- a/entry/src/main/ets/common/service/indexService.ts +++ b/entry/src/main/ets/common/service/indexService.ts @@ -259,15 +259,25 @@ export async function takePhotoFn(context) { } let fd const devPath="/dev/ttyS3" -function openChuankouFn() { +function openChuankouFn(callback) { console.log('SerialOpen in indexservice, path=' + devPath) - fd = testNapi.SerialOpen(devPath); - globalThis.num=0 - globalThis.fd=fd - console.log('fdfd1111',globalThis.fd) + // fd = testNapi.SerialOpen(devPath); + // globalThis.num=0 + // globalThis.fd=fd + // console.log('fdfd1111',globalThis.fd) + // + // let parity = 0x4e; // 'N' + // let ret = testNapi.SerialSet(globalThis.fd, 115200, 0, 8, 1, parity); - let parity = 0x4e; // 'N' - let ret = testNapi.SerialSet(globalThis.fd, 115200, 0, 8, 1, parity); + + testNapi.SerialOpenAsync(devPath, (fd)=> { + globalThis.fd = fd; + globalThis.num=0 + let parity = 0x4e; // 'N' + let ret = testNapi.SerialSetAsync(globalThis.fd, 115200, 0, 8, 1, parity, (ret) => { + callback() + }); + }); } function getChuankouFnMsg() { @@ -327,10 +337,10 @@ export async function getChuankouFn(){ if(globalThis.fd){ return } - openChuankouFn() + openChuankouFn(getChuankouFnMsg) // clearInterval(chuankou) // chuankou=setInterval(()=>{ - getChuankouFnMsg() + // getChuankouFnMsg() // },1000) } \ No newline at end of file diff --git a/entry/src/main/ets/common/utils/GlobleTcp.ts b/entry/src/main/ets/common/utils/GlobleTcp.ts index d23dd7d6..e8239e87 100644 --- a/entry/src/main/ets/common/utils/GlobleTcp.ts +++ b/entry/src/main/ets/common/utils/GlobleTcp.ts @@ -1,95 +1,94 @@ import TcpClient from './TcpClient'; -import { getSyncData} from '../service/initable' +import { getSyncData } from '../service/initable' import hilog from '@ohos.hilog'; -export async function getTCP(flag=false) { - getSyncData('IpConfigTable').then(async (result:Array)=>{ - console.log('result222',JSON.stringify(result)) - if(result.length){ - if(globalThis.TcpClient&&globalThis.TcpClient.closeTcp){ - if(!flag){ - return - } - globalThis.TcpClient.closeTcp(async ()=>{ - // const tcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) - // globalThis.TcpClient = tcpClient +export async function getTCP(flag?) { - await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) - await globalThis.TcpClient.connectTcp() - await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - await globalThis.TcpClient.onMessage((val)=>{ - hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val)); - - // console.log('valvalval',val) - setTimeout(()=>{ - globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){ - // const msg=val.substring(5,val.length-1) - globalThis.udpClient?.sendMsg(val) - } - },1000) - - }) - globalThis.TcpClient.onError(async()=>{ - await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) + getSyncData('IpConfigTable').then(async (result: Array) => { + console.log('result222', JSON.stringify(result)) + if (result.length) { + if (globalThis.TcpClient && globalThis.TcpClient.closeTcp) { + globalThis.TcpClient.closeTcp(async () => { + if(!flag){ + return + } + setTimeout(async () => { + await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort) await globalThis.TcpClient.connectTcp() - await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - await globalThis.TcpClient.onMessage((val)=>{ + await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + await globalThis.TcpClient.onMessage((val) => { hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val)); // console.log('valvalval',val) - setTimeout(()=>{ - globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){ + setTimeout(() => { + globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) { // const msg=val.substring(5,val.length-1) globalThis.udpClient?.sendMsg(val) } - },1000) + }, 1000) }) - }) + globalThis.TcpClient.onError(async () => { + await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort) + await globalThis.TcpClient.connectTcp() + await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + await globalThis.TcpClient.onMessage((val) => { + hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val)); + // console.log('valvalval',val) + setTimeout(() => { + globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) { + // const msg=val.substring(5,val.length-1) + globalThis.udpClient?.sendMsg(val) + } + }, 1000) + + }) + }) + },1000) }) } - else{ + else { // prompt.showToast({ // message:'初始化localIp'+ result[0].localIp+ result[0].localIpPort+'oppositeIp'+result[0].oppositeIp+result[0].oppositeIpPort, // duration: 1000 // }); - const tcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) + const tcpClient: TcpClient = new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort) globalThis.TcpClient = tcpClient await globalThis.TcpClient.bindTcp() await globalThis.TcpClient.connectTcp() - await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 + await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 - await globalThis.TcpClient.onMessage((val)=>{ + await globalThis.TcpClient.onMessage((val) => { hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val)); - setTimeout(()=>{ - globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){ + setTimeout(() => { + globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) { // const msg=val.substring(5,val.length-1) globalThis.udpClient?.sendMsg(val) } - },1000) + }, 1000) }) - globalThis.TcpClient.onError(async()=>{ - const tcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) + globalThis.TcpClient.onError(async () => { + const tcpClient: TcpClient = new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort) globalThis.TcpClient = tcpClient await globalThis.TcpClient.bindTcp() await globalThis.TcpClient.connectTcp() - await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 + await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 - await globalThis.TcpClient.onMessage((val)=>{ + await globalThis.TcpClient.onMessage((val) => { hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val)); - setTimeout(()=>{ - globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002 - if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){ + setTimeout(() => { + globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002 + if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) { // const msg=val.substring(5,val.length-1) globalThis.udpClient?.sendMsg(val) } - },1000) + }, 1000) }) }) @@ -97,9 +96,10 @@ export async function getTCP(flag=false) { } // globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort) - }else{ - globalThis.TcpClient={} - globalThis.TcpClient.onMessage=()=>{} + } else { + globalThis.TcpClient = {} + globalThis.TcpClient.onMessage = () => { + } } }) } diff --git a/entry/src/main/ets/common/utils/GlobleUdp.ts b/entry/src/main/ets/common/utils/GlobleUdp.ts index e802a649..f01a9c41 100644 --- a/entry/src/main/ets/common/utils/GlobleUdp.ts +++ b/entry/src/main/ets/common/utils/GlobleUdp.ts @@ -8,19 +8,21 @@ import {string2Bytes} from '../../common/utils/tools' export async function sendMsg(val){ // globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val) } -export async function getUDP(flag:Boolean=false) { +export async function getUDP(flag?) { getSyncData('IpConfigTable').then((result:Array)=>{ if(result.length){ - if(!flag){ - return - } if(globalThis.udpClient&&globalThis.udpClient.closeUdp){ + if(!flag){ + return + } globalThis.udpClient.closeUdp(()=>{ - globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) - globalThis.udpClient.sendMsg('111') - globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` - globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{}) + setTimeout(()=>{ + globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) + globalThis.udpClient.sendMsg('111') + globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` + globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{}) + },1000) }) }else{ @@ -41,7 +43,7 @@ export async function getUDP(flag:Boolean=false) { }) } -export async function getUDP2(flag:Boolean=false) { +export async function getUDP2(flag?) { getSyncData('IpConfigTable').then(async (result:Array)=>{ if(result.length){ if(globalThis.udpClient2&&globalThis.udpClient2.closeUdp){ @@ -49,11 +51,13 @@ export async function getUDP2(flag:Boolean=false) { return } globalThis.udpClient2.closeUdp(async ()=>{ - globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) - globalThis.udpClient2.onMessage((val)=>{ - }) - globalThis.udpClient2.onError() - globalThis.udpClient2.setMsgCallBack=()=>{} + setTimeout(()=>{ + globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) + globalThis.udpClient2.onMessage((val)=>{ + }) + globalThis.udpClient2.onError() + globalThis.udpClient2.setMsgCallBack=()=>{} + },1000) }) }else{