diff --git a/entry/src/main/ets/common/service/indexService.ts b/entry/src/main/ets/common/service/indexService.ts index 3923e965..5846eb3c 100644 --- a/entry/src/main/ets/common/service/indexService.ts +++ b/entry/src/main/ets/common/service/indexService.ts @@ -260,6 +260,7 @@ export async function takePhotoFn(context) { let fd const devPath="/dev/ttyS3" function openChuankouFn() { + console.log('SerialOpen in indexservice, path=' + devPath) fd = testNapi.SerialOpen(devPath); globalThis.num=0 globalThis.fd=fd @@ -273,49 +274,63 @@ function getChuankouFnMsg() { let timeout = 50000; // 2秒超时 let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE console.log('fdfd',globalThis.fd) - let ret = testNapi.SerialSend(globalThis.fd, databuff); - console.log('mmmm',ret) - // let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout); - testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => { - console.log('revTestInfo',revTestInfo.recevedBuf.length) + console.log('zzc 1 try send msg') + testNapi.SerialSendAsync(globalThis.fd, databuff,(ret)=>{ + console.log('zzc 2 send finished') + console.log('zzc 3 try receive msg') + testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => { + console.log('zzc 4 received msg') + console.log('revTestInfo',revTestInfo.recevedBuf.length) + + const message = revTestInfo?.recevedBuf?.toString() + console.log('chuankou',message) + if (message == '') { + console.log('zzc error msg is emptry') + globalThis.num=1 + console.log('zzc 9 num=3 close serial') + // clearInterval(chuankou) + testNapi.SerialClose(globalThis.fd); + globalThis.fd=null + getChuankouFn() + return + } + const msg = message?.split(',') + if(!msg?.length){ + + } else if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) { + + } else if(msg.length<12){ + + } else { + globalThis.chuankoMsg=msg[9] + } + + console.log('zzc 5 sleep 1s') + setTimeout(()=>{ + console.log(`x=${globalThis.num}`) + + console.log('zzc 7 send next msg') + getChuankouFnMsg() + },1000) + + // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync callback in'); + // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedLen = %{public}d', revTestInfo.recevedLen); + // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedBuf = %{public}s', revTestInfo.recevedBuf.toString()); + }); - const message = revTestInfo?.recevedBuf?.toString() - console.log('chuankou',message) - if (message == '') { - globalThis.num++ - return - } - const msg = message?.split(',') - if(!msg?.length){ - return - } - if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) { - return - } - globalThis.chuankoMsg=msg[9] - // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync callback in'); - // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedLen = %{public}d', revTestInfo.recevedLen); - // hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedBuf = %{public}s', revTestInfo.recevedBuf.toString()); }); - - + // let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout); } let chuankou export async function getChuankouFn(){ - return if(globalThis.fd){ return } openChuankouFn() - clearInterval(chuankou) - chuankou=setInterval(()=>{ - if(globalThis.num==5){ - testNapi.SerialClose(globalThis.fd); - globalThis.fd=null - getChuankouFn() - } - getChuankouFnMsg() - },1000) + // clearInterval(chuankou) + // chuankou=setInterval(()=>{ + 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 d3d61867..d23dd7d6 100644 --- a/entry/src/main/ets/common/utils/GlobleTcp.ts +++ b/entry/src/main/ets/common/utils/GlobleTcp.ts @@ -1,12 +1,15 @@ import TcpClient from './TcpClient'; import { getSyncData} from '../service/initable' import hilog from '@ohos.hilog'; -export async function getTCP() { +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 @@ -48,7 +51,8 @@ export async function getTCP() { }) - }else{ + } + else{ // prompt.showToast({ // message:'初始化localIp'+ result[0].localIp+ result[0].localIpPort+'oppositeIp'+result[0].oppositeIp+result[0].oppositeIpPort, // duration: 1000 diff --git a/entry/src/main/ets/common/utils/GlobleUdp.ts b/entry/src/main/ets/common/utils/GlobleUdp.ts index 49c6cc18..e802a649 100644 --- a/entry/src/main/ets/common/utils/GlobleUdp.ts +++ b/entry/src/main/ets/common/utils/GlobleUdp.ts @@ -8,9 +8,12 @@ import {string2Bytes} from '../../common/utils/tools' export async function sendMsg(val){ // globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val) } -export async function getUDP() { +export async function getUDP(flag:Boolean=false) { getSyncData('IpConfigTable').then((result:Array)=>{ if(result.length){ + if(!flag){ + return + } if(globalThis.udpClient&&globalThis.udpClient.closeUdp){ globalThis.udpClient.closeUdp(()=>{ globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) @@ -38,10 +41,13 @@ export async function getUDP() { }) } -export async function getUDP2() { +export async function getUDP2(flag:Boolean=false) { getSyncData('IpConfigTable').then(async (result:Array)=>{ if(result.length){ if(globalThis.udpClient2&&globalThis.udpClient2.closeUdp){ + if(!flag){ + return + } globalThis.udpClient2.closeUdp(async ()=>{ globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) globalThis.udpClient2.onMessage((val)=>{ diff --git a/entry/src/main/ets/common/utils/UdpClient.ts b/entry/src/main/ets/common/utils/UdpClient.ts index 41133b53..fee4c756 100644 --- a/entry/src/main/ets/common/utils/UdpClient.ts +++ b/entry/src/main/ets/common/utils/UdpClient.ts @@ -88,10 +88,10 @@ export default class UdpClient { address: { // address: '192.168.7.124', // port: 30013, - // address: '192.168.7.124', - // port: 20022, - address: this.oppositeIp, - port: parseInt(this.oppositeIpPort), + address: '192.168.7.124', + port: 20022, + // address: this.oppositeIp, + // port: parseInt(this.oppositeIpPort), family: 1 } }); @@ -144,7 +144,7 @@ export default class UdpClient { } console.log(`${TAG} udp222 on message array buffer:${str}`); - strachArr[28]=globalThis.chuankoMsg + strachArr[28]=globalThis.chuankoMsg||'0' // this.stashFn(str) const newArr=JSON.parse(JSON.stringify(strachArr)) // this.stashFn=()=>{} diff --git a/entry/src/main/ets/pages/CarCheck.ets b/entry/src/main/ets/pages/CarCheck.ets index 8a8f551b..8b056419 100644 --- a/entry/src/main/ets/pages/CarCheck.ets +++ b/entry/src/main/ets/pages/CarCheck.ets @@ -226,6 +226,8 @@ struct Index { } openChuankouFn() { + console.log('SerialOpen in carcheck, path=' + this.devPath) + this.fd = testNapi.SerialOpen(this.devPath); let parity = 0x4e; // 'N' let ret = testNapi.SerialSet(this.fd, 115200, 0, 8, 1, parity); diff --git a/entry/src/main/ets/pages/TerminalInfos.ets b/entry/src/main/ets/pages/TerminalInfos.ets index eff4ba59..7ee8b004 100644 --- a/entry/src/main/ets/pages/TerminalInfos.ets +++ b/entry/src/main/ets/pages/TerminalInfos.ets @@ -1,3 +1,4 @@ + import router from '@ohos.router' import TopLogo from './compontents/topLogo' import ethernet from '@ohos.net.ethernet'; @@ -6,6 +7,8 @@ import { upDateTableByArray} from '../common/service/initable' import { getSyncData} from '../common/service/initable' import { voiceService } from '../common/service/voiceService' import { GlobalConfig } from '../config/index' +import { getUDP, getUDP2 } from '../common/utils/GlobleUdp'; +import { getTCP } from '../common/utils/GlobleTcp'; @Entry @Component struct Index { @@ -102,6 +105,9 @@ struct Index { }); } }); + getUDP(true) + getUDP2(true) + getTCP(true) }) } .backgroundColor('#CCC4B8')