串口更新

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"
function openChuankouFn() {
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);
}
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);
// 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(){
return
if(globalThis.fd){
return
}
openChuankouFn()
setInterval(()=>{
clearInterval(chuankou)
chuankou=setInterval(()=>{
if(globalThis.num==5){
testNapi.SerialClose(globalThis.fd);
globalThis.fd=null
getChuankouFn()
}
getChuankouFnMsg()
},1000)

View File

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

View File

@ -132,10 +132,10 @@ export async function takePhoto(param, context,dir,flag=1,callback?) {
}
export async function deleteAllFileByPiC(dirName) {
//type:1 是图片 2是视频
export async function deleteAllFileByPiC(dirName,type=1) {
// 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> {

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.connectTcp()
await globalThis.TcpClient.sendMsg('1002')//1002
globalThis.TcpClient.onError()
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('1002')//1002
if(val){
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)
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{
@ -39,23 +57,39 @@ export async function getTCP() {
globalThis.TcpClient = tcpClient
await globalThis.TcpClient.bindTcp()
await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg('1002')//1002
// setInterval(()=>{
// globalThis.TcpClient.sendMsg('1002')//1002
//
// },2000)
globalThis.TcpClient.onError()
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('1002')//1002
if(val){
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()=>{
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)

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.sendMsg('111')
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}`
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
})
}else{
@ -24,6 +26,8 @@ export async function getUDP() {
udpClient.sendMsg('111')
globalThis.udpClient = udpClient
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}`
globalThis.udpClient.onError(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
}
}else{
@ -42,6 +46,7 @@ export async function getUDP2() {
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort)
globalThis.udpClient2.onMessage((val)=>{
})
globalThis.udpClient2.onError()
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)
globalThis.udpClient2 = udpClient
globalThis.udpClient2.bindUdp()
globalThis.udpClient2.onError()
globalThis.udpClient2.onMessage((val)=>{
if(val.id=='32'){
globalThis.signNum=val.body[1]

View File

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

View File

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

View File

@ -68,7 +68,6 @@ export default class UdpClientByCenter {
});
}
initHeartSendMsg(param,context){
console.log('1111param',JSON.stringify(param))
this.initParam=param
this.context=context
}
@ -199,7 +198,16 @@ export default class UdpClientByCenter {
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) {
this.udp.close(err=>{
if(err){

View File

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

View File

@ -172,7 +172,6 @@ struct Index {
for (let i = 1; i <= 4; i++) {
console.log('rocord_handle',i,JSON.stringify(this.rocordHandleObj))
if (this.rocordHandleObj['rocord_handle'+i]) {
console.log('ttttt',i)
endRecordVideo(this.rocordHandleObj['rocord_handle'+i])
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 { voiceService } from '../../common/service/voiceService'
import prompt from '@ohos.prompt'
import SerialPortClient from '../../common/utils/SerialPortClient'
import RealTime from '../compontents/judge/real-time'
@Component
struct SignDisplayCom {
@ -397,20 +396,7 @@ struct SignDisplayCom {
const that = 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){
globalThis.udpClient.onMessage((msg) => {
console.log('msgmsg',msg)
@ -441,21 +427,8 @@ struct SignDisplayCom {
// }
// }
// });
const serialPortClient = new SerialPortClient()
serialPortClient.openSerial()
serialPortClient.SerialSet()
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 showBack = this.showBack;
if(showBack){