120 lines
3.6 KiB
Plaintext
120 lines
3.6 KiB
Plaintext
// 导入USB接口api包。
|
|
import usb from '@ohos.usbManager';
|
|
import { UsbTag } from '../config';
|
|
import { StringToBytes } from './Common';
|
|
|
|
|
|
//plc数据转成无锡科研所usb字节数据
|
|
function plcStrToWXCodeArr(wuXiDataStr: string) {
|
|
// const arr = stringToArr(wuXiDataStr);
|
|
const arr = StringToBytes(wuXiDataStr);
|
|
// 数据包总长度和有效长度
|
|
const maxDataSize: number = 63;
|
|
|
|
// 存储所有数据包的数组
|
|
const packetArr: number[][] = [];
|
|
|
|
// 计算需要的数据包数量
|
|
const loop: number = Math.ceil(arr.length / maxDataSize);
|
|
|
|
for (let i = 0; i < loop; i++) {
|
|
// 截取当前数据包的数据
|
|
const thisPacket: number[] = Array.from(arr.slice(i * maxDataSize, (i + 1) * maxDataSize));
|
|
|
|
// 计算需要补齐的长度
|
|
const oSize: number = maxDataSize - thisPacket.length;
|
|
|
|
// 创建当前数据包
|
|
const packet: number[] = [thisPacket.length, ...thisPacket];
|
|
|
|
// 如果需要补齐,则填充 0x00
|
|
if (oSize > 0) {
|
|
packet.push(...new Array(oSize).fill(0x00));
|
|
}
|
|
|
|
// 将当前数据包添加到结果数组中
|
|
packetArr.push(packet);
|
|
}
|
|
|
|
return packetArr;
|
|
}
|
|
|
|
export default class UsbService {
|
|
public isWXUSBDevice: Boolean
|
|
private devicepipe?: usb.USBDevicePipe
|
|
private outEndpoint?: usb.USBEndpoint
|
|
sendUSB = async (wuXiDataStr: string) => {
|
|
console.info(UsbTag, '正在发送数据')
|
|
console.info(UsbTag, '正在发送数据')
|
|
if (this.isWXUSBDevice) {
|
|
console.info(UsbTag, wuXiDataStr)
|
|
const codeArr = plcStrToWXCodeArr(wuXiDataStr);
|
|
for (let i = 0; i < codeArr.length; i++) {
|
|
try {
|
|
console.info(UsbTag, '正在发送数据')
|
|
const f = await usb.bulkTransfer(this.devicepipe, this.outEndpoint, new Uint8Array(codeArr[i]))
|
|
console.info(UsbTag, '发送成功数据长度为:' + f)
|
|
} catch (e) {
|
|
console.info(UsbTag, JSON.stringify(e))
|
|
}
|
|
}
|
|
} else {
|
|
console.info(UsbTag, 'usb设备初始化失败')
|
|
}
|
|
|
|
}
|
|
init = async () => {
|
|
// 获取设备列表。
|
|
let deviceList: Array<usb.USBDevice> = usb.getDevices();
|
|
|
|
deviceList.forEach(async (device: usb.USBDevice) => {
|
|
console.log(UsbTag, JSON.stringify(device));
|
|
//无锡所检测设备接入
|
|
if (device.vendorId === 6790 && device.productId === 58409) {
|
|
// if(vendorId === 2385 && productId === 5734){
|
|
// 申请操作指定的device的操作权限。
|
|
try {
|
|
let bool = usb.hasRight(device.name);
|
|
console.info(UsbTag, 'bool =>' + bool)
|
|
const isExit = await usb.requestRight(device.name);
|
|
console.info(UsbTag, '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(UsbTag, 'ret =>' + ret);
|
|
|
|
if (ret === 0) {
|
|
let outEndpoint: usb.USBEndpoint = interfaces.endpoints[1];
|
|
this.isWXUSBDevice = true;
|
|
this.devicepipe = devicepipe
|
|
this.outEndpoint = outEndpoint
|
|
|
|
} else {
|
|
console.info(UsbTag, 'usb claimInterface failed')
|
|
}
|
|
|
|
} else {
|
|
console.info(UsbTag, 'isExit =>' + 'false')
|
|
}
|
|
|
|
} catch (e) {
|
|
console.info(UsbTag, 'e=>' + JSON.stringify(e))
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
constructor() {
|
|
//是否是无锡检测设备
|
|
this.isWXUSBDevice = false
|
|
// this.devicepipe
|
|
this.init()
|
|
}
|
|
}
|
|
|
|
|
|
// export default initUsbServicesFn
|