diff --git a/entry/src/main/ets/common/service/usbService.ts b/entry/src/main/ets/common/service/usbService.ts new file mode 100644 index 00000000..f3691e19 --- /dev/null +++ b/entry/src/main/ets/common/service/usbService.ts @@ -0,0 +1,116 @@ +// 导入USB接口api包。 +import usb from '@ohos.usbManager'; +const LOGTAG = 'USBSERVICES' + + +//字符串转字节 +function stringToArr(str){ + var arr = []; + for (var i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + return arr +} + +//plc数据转成无锡科研所usb字节数据 +function plcStrToWXCodeArr(wuXiDataStr){ + const arr = stringToArr(wuXiDataStr); + //数据包总长度 有效长度 + let packetSize = 65,maxDataSize = 63; + let packetArr = [] + const loop = Math.ceil(arr.length / maxDataSize) + for(let i = 0; i< loop; i++){ + const thisPacket = arr.slice(i * maxDataSize,(i + 1) * maxDataSize) + const oSize = maxDataSize - thisPacket.length; + //补齐0x00 + if(oSize > 0){ + let oSizeArr = [] + for(let j = 0;j < oSize;j++){oSizeArr.push(0x00)} + packetArr.push([thisPacket.length].concat(thisPacket).concat(oSizeArr)); + }else{ + packetArr.push([thisPacket.length].concat(thisPacket)); + } + } + return packetArr +} + +export default class UsbService{ + private devicepipe : usb.USBDevicePipe + private outEndpoint: usb.USBEndpoint + public isWXUSBDevice:Boolean + + constructor() { + //是否是无锡检测设备 + this.isWXUSBDevice = false + this.devicepipe + this.init() + } + + init = async()=>{ + // 获取设备列表。 + let deviceList : Array = usb.getDevices(); + + deviceList.forEach(async (device:usb.USBDevice) => { + const {vendorId,productId} = device; + console.log(LOGTAG,JSON.stringify(device)); + //无锡所检测设备接入 + if(vendorId === 6790 && productId === 58409){ + // if(vendorId === 2385 && productId === 5734){ + // 申请操作指定的device的操作权限。 + try { + let bool = usb.hasRight(device.name); + console.info(LOGTAG,'bool =>' + bool) + const isExit = await usb.requestRight(device.name); + console.info(LOGTAG,'isExit =>' + isExit) + if(isExit){ + let devicepipe : usb.USBDevicePipe = usb.connectDevice(device); + let interfaces = device.configs[0].interfaces[0]; + let ret = usb.claimInterface(devicepipe, interfaces,true); + console.info(LOGTAG,'ret =>' + ret); + + if(ret === 0 ){ + let outEndpoint : usb.USBEndpoint = interfaces.endpoints[1]; + let inEndpoint : usb.USBEndpoint = interfaces.endpoints[0]; + this.isWXUSBDevice = true; + this.devicepipe = devicepipe + this.outEndpoint = outEndpoint + + }else{ + console.info(LOGTAG,'usb claimInterface failed') + } + + }else{ + console.info(LOGTAG,'isExit =>' +'false') + } + + } catch (e) { + console.info(LOGTAG,'e=>' + JSON.stringify(e)) + } + + } + }); + + } + + sendUSB =async (wuXiDataStr) => { + const {devicepipe,isWXUSBDevice,outEndpoint} = this; + if(isWXUSBDevice){ + const codeArr = plcStrToWXCodeArr(wuXiDataStr); + for(let i = 0; i < codeArr.length;i++){ + try { + const f = await usb.bulkTransfer(devicepipe, outEndpoint, new Uint8Array(codeArr[i])) + console.info(LOGTAG,'发送成功数据长度为:' + f) + } catch (e) { + console.info(LOGTAG,JSON.stringify(e)) + } + } + }else{ + console.info(LOGTAG,'usb设备初始化失败') + } + + } +} + + + +// export default initUsbServicesFn diff --git a/entry/src/main/ets/pages/judgeSDK/judge.ts b/entry/src/main/ets/pages/judgeSDK/judge.ts index 20797019..37d58a3c 100644 --- a/entry/src/main/ets/pages/judgeSDK/judge.ts +++ b/entry/src/main/ets/pages/judgeSDK/judge.ts @@ -20,8 +20,10 @@ import {writeObjectOut,uploadExamProgressData} from '../../api/judge' import {deepClone,getCurrentTime,stringToASC,string2Bytes,fillZero,Array2Byte,convertGpsCoord2} from '../../common/utils/tools' import {getTranslateSignals,getCarStatus,getCarStatusType,getCenterProjectStatus,plcStrToJson,plcStrToWXJson,promptWxCode,getKmProjectVoice} from './utils//judge-common' import {examJudgeSetLogCallback,examJudgeBeginExam,examJudgeInit,examJudgeRealExam,examJudgeSetRealExamCallback,examJudgeSetPerformCallback,examJudgeEndExam,examJudgeArtificialMark,examJudgeArtificialItem} from './api/index' +import UsbService from '../../common/service/usbService' import prompt from '@ohos.prompt'; +import usbManager from '@ohos.usbManager'; const judgeTag = 'SURENJUN_JUDGE' @@ -40,6 +42,8 @@ export default class Judge{ this.fileUtil = new FileUtil(judgeUI.context) this.judgeTask = new JudgeTask() const mediaTest= new FilePhoto(judgeUI.context); + const usbService = new UsbService(); + this.usbService = usbService this.filePhoto = mediaTest this.kfArr = judgeUI.kfArr this.xmmcStr = '';this.xmmcCode = '';this.carztStr = ''; @@ -1001,6 +1005,7 @@ export default class Judge{ private xmxh:string private fileModel:FileModel private filePhoto:FilePhoto + private usbService:UsbService //是否是考试模式 private isExam:boolean //考试是否结束了