眼科检查信息系统V3.0安卓版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

190 lines
5.4 KiB

var UsbSerial = uni.requireNativePlugin('Zhimi-UsbSerial');
export default {
data() {
return {
setInterTimeUSB: null,
vendorId: '',
port: '',
usbDevicesComPortsList: [] // 串口名称数组
}
},
destroyed() {
console.log('销毁页面');
clearInterval(this.setInterTimeUSB)
},
methods: {
// 检测USB是否连接状态
usbStatus() {
clearInterval(this.setInterTimeUSB)
this.setInterTimeUSB = setInterval(() => {
this.connectUSB('open')
}, 5000)
},
// 设置USB数据回调
connectUSB(flag, callback) {
console.log('UsbSerial', UsbSerial)
//设置数据回调
UsbSerial.setDataCallback((res) => {
console.log('设置USB数据回调-res', res)
if (res.type == "onUsbDeviceAttached") { //usb插上
console.log('USB已插入');
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USB已插入'
}
}
this.webViewToH5(data)
} else if (res.type == "onUsbDeviceDetached") { //usb断开
console.log('USB已拔出');
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USB已拔出'
}
}
this.webViewToH5(data)
} else if (res.type === 'startUpdateValue') {
console.log('正在接受数据中,请稍等...');
} else if (res.type == "onNewData") { //数据回调
var data = res.data.data; //字节数组
var str = res.data.string; //字符串
var hex = res.data.hex; //十六进制
// console.log('字节数组',data)
// console.log('字符串',str)
console.log('hex', hex)
// 68656c6c6f20776f726c64---hello world
console.log('this.hexCharCodeToStr(hex)', JSON.stringify(this.hexCharCodeToStr(hex)));
var data = {
command: 'examineData',
commandType: 'USB',
data: {
state: 0,
message: JSON.stringify(this.hexCharCodeToStr(hex))
}
}
this.webViewToH5(data)
} else if (res.type == "onRunError") {
//点击关闭USB
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USBRunError'
}
}
this.webViewToH5(data)
}
});
//UsbSerial.setReceiveTime(type,time) 参数:
// type (0 1 2)0直接返回,字节太长会被分包; 1固定间隔time返回拼接的数据; 2拼接数据,没接收到数据间隔time后返回
// time (毫秒)
UsbSerial.setReceiveTime(2, 500)
//获取usb列表
UsbSerial.getUsbDevices((usbDevices) => {
console.log('获取usb列表-usbDevices', usbDevices)
//选择usb列表
if (usbDevices.length > 0) {
this.usbDevicesComPortsList = usbDevices.map(item => item.device.vendorId)
if (flag === 'NoOpen') {
callback()
} else {
// console.log('this.cgConfigInfo.data.portName',this.cgConfigInfo.data.portName);
// console.log('usbDevices[0].device.vendorId',usbDevices[0].device.vendorId);
let usbDevice = usbDevices.filter(item => item.device.vendorId == this.cgConfigInfo.data.portName)
console.log('usbDevice',usbDevice);
if (usbDevice.length > 0) {
this.vendorId = usbDevice[0].device.vendorId
this.port = usbDevice[0].ports[0]
this.openUSB()
}
}
} else {
this.usbDevicesComPortsList = []
flag === 'NoOpen' ? callback() : ''
}
});
},
// 打开USB
openUSB() {
let vueDetailData = this.cgConfigInfo.data
console.log('打开USB', this.vendorId)
//检查授权Usb权限
UsbSerial.grantUsbPermission(this.vendorId, (res) => {
console.log('检查授权Usb权限', res)
if (res == true) {
//打开usb连接
UsbSerial.open(this.vendorId, this.port, (ret) => {
console.log('打开usb连接', ret)
if (ret == true) {
//设置参数
UsbSerial.setParameters(vueDetailData.baudRate, vueDetailData.dataBits, vueDetailData.stopBits, vueDetailData.parity)
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USB已打开'
}
}
this.webViewToH5(data)
}
});
} else {
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USB未授权'
}
}
this.webViewToH5(data)
}
});
},
//关闭usb
closeUSB() {
console.log('关闭usb')
UsbSerial.close()
clearInterval(this.setInterTimeUSB)
var data = {
command: 'comStatusChanged',
data: {
state: 0,
message: 'USB已关闭'
}
}
this.webViewToH5(data)
// uni.showToast({
// title: "usb已断开"
// })
},
// 将16进制的内容转成我们看得懂的字符串内容
hexCharCodeToStr(hexCharCodeStr) {
var trimedStr = hexCharCodeStr.trim()
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr
var len = rawStr.length;
// console.log(len)
if (len % 2 !== 0) {
alert("存在非法字符!")
return ""
}
var curCharCode
var resultStr = []
for (var i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16)
resultStr.push(String.fromCharCode(curCharCode))
}
return resultStr.join("")
},
// uniapp 传参给 vue页面---刘灿测试
webViewToH5(res) {
let currentWebview = this.$scope.$getAppWebview();
let wv = currentWebview.children()[0];
console.log('uniapp 传参给 vue页面');
wv.evalJS(`getVueMessage(${JSON.stringify(res)})`);
},
}
}