串口更新

This commit is contained in:
lvyuankang 2024-05-27 17:25:20 +08:00
parent 63145aec13
commit 424dc1ab23
11 changed files with 135 additions and 81 deletions

View File

@ -261,44 +261,60 @@ let fd
const devPath="/dev/ttyS3" const devPath="/dev/ttyS3"
function openChuankouFn() { function openChuankouFn() {
fd = testNapi.SerialOpen(devPath); fd = testNapi.SerialOpen(devPath);
globalThis.num=0
globalThis.fd=fd globalThis.fd=fd
console.log('fdfd1111',globalThis.fd) console.log('fdfd1111',globalThis.fd)
let parity = 0x4e; // 'N' let parity = 0x4e; // 'N'
let ret = testNapi.SerialSet(globalThis.fd, 115200, 0, 8, 1, parity); let ret = testNapi.SerialSet(globalThis.fd, 115200, 0, 8, 1, parity);
} }
function getChuankouFnMsg() { function getChuankouFnMsg() {
let timeout = 50000; // 2秒超时 let timeout = 50000; // 2秒超时
let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE
console.log('fdfd',globalThis.fd) console.log('fdfd',globalThis.fd)
let ret = testNapi.SerialSend(globalThis.fd, databuff); let ret = testNapi.SerialSend(globalThis.fd, databuff);
console.log('mmmm',ret) console.log('mmmm',ret)
let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout); // let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout);
testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => {
console.log('revTestInfo',revTestInfo.recevedBuf.length)
console.log('revTestInfo',revTestInfo.recevedBuf.length) 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());
});
const message = revTestInfo?.recevedBuf?.toString()
console.log('chuankou',message)
if (message == '') {
return
}
const msg = message?.split(',')
if(!msg?.length){
return
}
if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
return
}
globalThis.chuankoMsg=msg[9]
} }
let chuankou
export async function getChuankouFn(){ export async function getChuankouFn(){
return
if(globalThis.fd){ if(globalThis.fd){
return return
} }
openChuankouFn() openChuankouFn()
setInterval(()=>{ clearInterval(chuankou)
chuankou=setInterval(()=>{
if(globalThis.num==5){
testNapi.SerialClose(globalThis.fd);
globalThis.fd=null
getChuankouFn()
}
getChuankouFnMsg() getChuankouFnMsg()
},1000) },1000)

View File

@ -40,6 +40,7 @@ export async function getCarInfo() {
// }, router.RouterMode.Single); // }, router.RouterMode.Single);
}else { }else {
globalThis.carInfo=res.obtainCarExamInfoRsp.body globalThis.carInfo=res.obtainCarExamInfoRsp.body
console.log('globalThis.carInfo',JSON.stringify(globalThis.carInfo));
// globalThis.carInfo.carId=res.obtainCarExamInfoRsp.body.carId // globalThis.carInfo.carId=res.obtainCarExamInfoRsp.body.carId
globalThis.carInfo.plateNo = decodeURIComponent(globalThis.carInfo.plateNo) globalThis.carInfo.plateNo = decodeURIComponent(globalThis.carInfo.plateNo)
} }

View File

@ -132,10 +132,10 @@ export async function takePhoto(param, context,dir,flag=1,callback?) {
} }
//type:1 是图片 2是视频
export async function deleteAllFileByPiC(dirName) { export async function deleteAllFileByPiC(dirName,type=1) {
// const fileHelper = new FileHelper(); // const fileHelper = new FileHelper();
fileHelper.deleteFileOfAlbum(dirName,1); fileHelper.deleteFileOfAlbum(dirName,type);
} }
export async function deleteAllVideos(context,type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<void> { export async function deleteAllVideos(context,type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<void> {

View File

@ -13,21 +13,39 @@ export async function getTCP() {
await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort) await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)
await globalThis.TcpClient.connectTcp() await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg('1002')//1002 await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002
globalThis.TcpClient.onError()
await globalThis.TcpClient.onMessage((val)=>{ await globalThis.TcpClient.onMessage((val)=>{
hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val)); hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val));
// console.log('valvalval',val) // console.log('valvalval',val)
setTimeout(()=>{ setTimeout(()=>{
globalThis.TcpClient.sendMsg('1002')//1002 globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002
if(val){ if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){
// const msg=val.substring(5,val.length-1) // const msg=val.substring(5,val.length-1)
globalThis.udpClient?.sendMsg(val) 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)
})
})
}) })
}else{ }else{
@ -39,23 +57,39 @@ export async function getTCP() {
globalThis.TcpClient = tcpClient globalThis.TcpClient = tcpClient
await globalThis.TcpClient.bindTcp() await globalThis.TcpClient.bindTcp()
await globalThis.TcpClient.connectTcp() await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg('1002')//1002 await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002
// setInterval(()=>{
// globalThis.TcpClient.sendMsg('1002')//1002
//
// },2000)
globalThis.TcpClient.onError()
await globalThis.TcpClient.onMessage((val)=>{ await globalThis.TcpClient.onMessage((val)=>{
hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val)); hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val));
setTimeout(()=>{ setTimeout(()=>{
globalThis.TcpClient.sendMsg('1002')//1002 globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002
if(val){ if(val&&globalThis.udpClient&&globalThis.udpClient.sendMsg){
// const msg=val.substring(5,val.length-1) // const msg=val.substring(5,val.length-1)
globalThis.udpClient?.sendMsg(val) 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 = tcpClient
await globalThis.TcpClient.bindTcp()
await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo||'402')//1002
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){
// const msg=val.substring(5,val.length-1)
globalThis.udpClient?.sendMsg(val)
}
},1000)
})
})
} }
// globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort) // globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort)

View File

@ -16,6 +16,8 @@ export async function getUDP() {
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort)
globalThis.udpClient.sendMsg('111') globalThis.udpClient.sendMsg('111')
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}`
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
}) })
}else{ }else{
@ -24,6 +26,8 @@ export async function getUDP() {
udpClient.sendMsg('111') udpClient.sendMsg('111')
globalThis.udpClient = udpClient globalThis.udpClient = udpClient
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}`
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
} }
}else{ }else{
@ -42,6 +46,7 @@ export async function getUDP2() {
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort)
globalThis.udpClient2.onMessage((val)=>{ globalThis.udpClient2.onMessage((val)=>{
}) })
globalThis.udpClient2.onError()
globalThis.udpClient2.setMsgCallBack=()=>{} globalThis.udpClient2.setMsgCallBack=()=>{}
}) })
@ -49,6 +54,7 @@ export async function getUDP2() {
const udpClient: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) const udpClient: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort)
globalThis.udpClient2 = udpClient globalThis.udpClient2 = udpClient
globalThis.udpClient2.bindUdp() globalThis.udpClient2.bindUdp()
globalThis.udpClient2.onError()
globalThis.udpClient2.onMessage((val)=>{ globalThis.udpClient2.onMessage((val)=>{
if(val.id=='32'){ if(val.id=='32'){
globalThis.signNum=val.body[1] globalThis.signNum=val.body[1]

View File

@ -24,8 +24,7 @@ export default class TcpClient {
onError(callback?){ onError(callback?){
this.tcp.on('error', err => { this.tcp.on('error', err => {
hilog.info(0x0000, 'testTag', "on error, err:" + JSON.stringify(err)); hilog.info(0x0000, 'testTag', "on error, err:" + JSON.stringify(err));
this.bindTcp() callback()
this.connectTcp()
// this.closeUdp(()=>{ // this.closeUdp(()=>{
// this.bindUdp() // this.bindUdp()
// }) // })
@ -56,13 +55,17 @@ export default class TcpClient {
} }
bindTcp() { bindTcp() {
console.log('tcpoppositeIp',this.oppositeIp,'localIp',this.localIp)
return new Promise((resolve,reject)=>{ return new Promise((resolve,reject)=>{
let promise=this.tcp.bind({ address: this.localIp, port:parseInt(this.localIpPort), family: 1 }, err => { let promise=this.tcp.bind({ address: this.localIp, port:parseInt(this.localIpPort), family: 1 }, err => {
if (err) { if (err) {
console.log('testTag tcp bind faile');
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err)); hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
resolve(true) resolve(true)
return
} }
console.log('bind success'); console.log('testTag tcp bind success');
resolve(false) resolve(false)
}) })
}) })
@ -77,10 +80,13 @@ export default class TcpClient {
} }
connectTcp(){ connectTcp(){
console.log('testTagtcpoppositeIp',this.oppositeIp,'localIp',this.localIp)
console.log('22323') console.log('22323')
return new Promise((resolve,reject)=>{ return new Promise((resolve,reject)=>{
let promise = this.tcp.connect({ address: {address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1} , timeout: 6000}); let promise = this.tcp.connect({ address: {address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1} , timeout: 6000});
promise.then(() => { promise.then(() => {
console.log('testTagbindSuccess')
this.tcp.setExtraOptions({ this.tcp.setExtraOptions({
keepAlive: true, keepAlive: true,
// OOBInline: true, // OOBInline: true,
@ -97,13 +103,15 @@ export default class TcpClient {
}); });
resolve(true) resolve(true)
}).catch(err => { }).catch(err => {
console.log('testTagconnect,error')
resolve(false) resolve(false)
}); });
}) })
} }
sendMsg(msg: string) {console.log('oppositeIp',this.oppositeIp) sendMsg(msg: string) {
console.log('oppositeIp',this.oppositeIp)
return new Promise((reslove,reject)=>{ return new Promise((reslove,reject)=>{
let promise = this.tcp.send({ let promise = this.tcp.send({
data:msg data:msg
@ -135,19 +143,19 @@ export default class TcpClient {
// }); // });
} }
onMessage(callback) { onMessage(callback?) {
this.tcp.on('message', value => { this.tcp.on('message', value => {
// console.log('messageLengt',,value.message.length)) // console.log('messageLengt',,value.message.length))
console.log('tcpmsg')
// console.log("on message, message:" + value.message+ ", remoteInfo:" ) // console.log("on message, message:" + value.message+ ", remoteInfo:" )
if (value) { if (value) {
let dataView = new DataView(value.message) let dataView = new DataView(value.message)
// const Arraybuffer=buffer.from(value.message, 5, dataView?.byteLength); // const Arraybuffer=buffer.from(value.message, 5, dataView?.byteLength);
const Arraybuffer=value.message.slice(5, dataView?.byteLength); const Arraybuffer=value.message.slice(5, dataView?.byteLength);
callback(Arraybuffer) callback&&callback(Arraybuffer)
}else{ }else{
callback('') callback&&callback('')
} }
// callback(value.message) // callback(value.message)
}); });

View File

@ -64,9 +64,9 @@ export default class UdpClient {
hilog.info(0x0000, 'udpCLient', 'bind'+ this.localIp+this.localIpPort); hilog.info(0x0000, 'udpCLient', 'bind'+ this.localIp+this.localIpPort);
let promise = this.udp.bind({ let promise = this.udp.bind({
// address: '192.168.7.170', port: 20122, family: 1 address: '192.168.7.170', port: 20122, family: 1
// address: '192.168.7.170', port: 31013, family: 1 // address: '192.168.7.170', port: 31013, family: 1
address: this.localIp, port: parseInt(this.localIpPort), family: 1 // address: this.localIp, port: parseInt(this.localIpPort), family: 1
}); });
promise.then(() => { promise.then(() => {
console.log(`${TAG}udpCLient udp bind success`); console.log(`${TAG}udpCLient udp bind success`);
@ -78,9 +78,10 @@ export default class UdpClient {
setMsgCallBack(callback){ setMsgCallBack(callback){
// this.stashFn=callback?callback:()=>{} // this.stashFn=callback?callback:()=>{}
} }
sendMsg(msg: string) { sendMsg(msg) {
console.log('sendsnd01') console.log('sendsnd01')
hilog.info(0x0000, 'udpCLient', 'send'); hilog.info(0x0000, 'udpCLient', 'send');
console.log('udpdiushisend')
let promise = this.udp.send({ let promise = this.udp.send({
data: msg, data: msg,
@ -101,18 +102,25 @@ export default class UdpClient {
}); });
} }
onError(callback?){ onError(callback?){
this.udp.on('error', err => { this.udp.on('error',async err => {
hilog.info(0x0000, 'udpCLient', 'error'+ JSON.stringify(err)); console.log('udpdiushierroring')
hilog.info(0x0000, 'udpCLientonerror', 'error'+ JSON.stringify(err));
console.log("on error, err:" + JSON.stringify(err)) console.log("on error, err:" + JSON.stringify(err))
await this.bindUdp()
await this.sendMsg('111')
await this.onMessage(callback)
// callback&&callback()
// this.closeUdp(()=>{ // this.closeUdp(()=>{
// this.bindUdp() // this.bindUdp()
// }) // })
}); });
} }
onMessage(callback) { onMessage(callback?) {
this.udp.on('message', value => { this.udp.on('message', value => {
console.log('udpdiushimsg')
// 收到的是ArrayBuffer 需要进行转换解析 // 收到的是ArrayBuffer 需要进行转换解析
hilog.info(0x0000, 'udpCLient', 'onmessage'); hilog.info(0x0000, 'udpCLient', 'onmessage');
@ -134,15 +142,16 @@ export default class UdpClient {
if (strachArr[0] != '#DN_GD') { if (strachArr[0] != '#DN_GD') {
return return
} }
console.log(`${TAG} udp222 on message array buffer:${str}`);
strachArr[28]=globalThis.chuankoMsg strachArr[28]=globalThis.chuankoMsg
// this.stashFn(str) // this.stashFn(str)
const newArr=JSON.parse(JSON.stringify(strachArr)) const newArr=JSON.parse(JSON.stringify(strachArr))
console.log('22222',strachArr[28],newArr)
// this.stashFn=()=>{} // this.stashFn=()=>{}
callback(newArr.toString()) callback&&callback(newArr.toString())
} else { } else {
callback('') callback&&callback('')
} }
console.log('messageTimeEnd') console.log('messageTimeEnd')

View File

@ -68,7 +68,6 @@ export default class UdpClientByCenter {
}); });
} }
initHeartSendMsg(param,context){ initHeartSendMsg(param,context){
console.log('1111param',JSON.stringify(param))
this.initParam=param this.initParam=param
this.context=context this.context=context
} }
@ -199,7 +198,16 @@ export default class UdpClientByCenter {
callback({id,length,body:list,sendId:this.sendId}) callback({id,length,body:list,sendId:this.sendId})
}); });
} }
onError(callback?){
this.udp.on('error', err => {
console.log('udpbycenterdiushierroring')
hilog.info(0x0000, 'udpCLientBycenteronerror', 'error'+ JSON.stringify(err));
console.log("on error, err:" + JSON.stringify(err))
// this.closeUdp(()=>{
// this.bindUdp()
// })
});
}
closeUdp(callback) { closeUdp(callback) {
this.udp.close(err=>{ this.udp.close(err=>{
if(err){ if(err){

View File

@ -249,7 +249,7 @@ struct Index {
getliushuiNum() getliushuiNum()
this.heartMsg() this.heartMsg()
}, 1000) }, 1000)
takePhotoFn(this.context) // takePhotoFn(this.context)
// //
getTCP() getTCP()

View File

@ -172,7 +172,6 @@ struct Index {
for (let i = 1; i <= 4; i++) { for (let i = 1; i <= 4; i++) {
console.log('rocord_handle',i,JSON.stringify(this.rocordHandleObj)) console.log('rocord_handle',i,JSON.stringify(this.rocordHandleObj))
if (this.rocordHandleObj['rocord_handle'+i]) { if (this.rocordHandleObj['rocord_handle'+i]) {
console.log('ttttt',i)
endRecordVideo(this.rocordHandleObj['rocord_handle'+i]) endRecordVideo(this.rocordHandleObj['rocord_handle'+i])
this.rocordHandleObj['rocord_handle'+i] = 0 this.rocordHandleObj['rocord_handle'+i] = 0
} }

View File

@ -3,7 +3,6 @@ import UdpClient from '../../common/utils/UdpClient';
import FileLog from '../judgeSDK/utils/file-log' import FileLog from '../judgeSDK/utils/file-log'
import { voiceService } from '../../common/service/voiceService' import { voiceService } from '../../common/service/voiceService'
import prompt from '@ohos.prompt' import prompt from '@ohos.prompt'
import SerialPortClient from '../../common/utils/SerialPortClient'
import RealTime from '../compontents/judge/real-time' import RealTime from '../compontents/judge/real-time'
@Component @Component
struct SignDisplayCom { struct SignDisplayCom {
@ -397,20 +396,7 @@ struct SignDisplayCom {
const that = this const that = this
const {showBack,getSignal} = this const {showBack,getSignal} = this
const serialPortClient = new SerialPortClient()
serialPortClient.openSerial()
serialPortClient.SerialSet()
this.interval=setInterval(() => {
serialPortClient.SerialSend()
const data = serialPortClient.SerialRecv()
// if (data?.recevedLen) {
// const msg = data.msg.split(',')
// if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
// return
// }
// this.sjxhColum[18].value = msg[9]
// }
}, 1000)
if(showBack){ if(showBack){
globalThis.udpClient.onMessage((msg) => { globalThis.udpClient.onMessage((msg) => {
console.log('msgmsg',msg) console.log('msgmsg',msg)
@ -441,21 +427,8 @@ struct SignDisplayCom {
// } // }
// } // }
// }); // });
const serialPortClient = new SerialPortClient()
serialPortClient.openSerial()
serialPortClient.SerialSet()
const getSignal = this.getSignal; const getSignal = this.getSignal;
this.interval=setInterval(() => {
serialPortClient.SerialSend()
const data = serialPortClient.SerialRecv()
// if (data.recevedLen) {
// const msg = data.msg.split(',')
// if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
// return
// }
// this.sjxhColum[18].value = msg[9]
// }
}, 1000)
const that = this const that = this
const showBack = this.showBack; const showBack = this.showBack;
if(showBack){ if(showBack){