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.
222 lines
4.9 KiB
222 lines
4.9 KiB
// #ifdef APP
|
|
import ecUI from '@/utils/ecUI.js'
|
|
import ecBLE from '@/utils/ecBLE/ecBLE.js'
|
|
// #endif
|
|
// #ifdef MP
|
|
const ecUI = require('@/utils/ecUI.js')
|
|
const ecBLE = require('@/utils/ecBLE/ecBLE.js')
|
|
// #endif
|
|
|
|
export function hexToString(hex)
|
|
{
|
|
var tmp = '';
|
|
if(hex < 0)
|
|
{
|
|
hex=hex+256;
|
|
}
|
|
tmp = hex.toString(16)
|
|
tmp = tmp.padStart(2, '0')
|
|
return tmp
|
|
}
|
|
|
|
export function send_uart(id,cc,icode,len,msg)
|
|
{
|
|
var code=''
|
|
var send_data=[],send_stringdata='',send_length=0;
|
|
if(cc == '08')
|
|
{
|
|
send_length = 18
|
|
}
|
|
else if(cc == '8f')
|
|
{
|
|
send_length = len+12;
|
|
}
|
|
else{
|
|
send_length = len+14;
|
|
}
|
|
|
|
send_data[0]='68';
|
|
send_data[7]='68';
|
|
for(var i=0 ; i<6; i ++)
|
|
{
|
|
send_data[6-i] = id.substr(2*i,2);
|
|
}
|
|
|
|
//获取控制码
|
|
send_data[8]=cc;
|
|
|
|
var i1,i2,i3,i4
|
|
|
|
if(cc == '08'||cc == '8f')
|
|
{
|
|
//获取长度
|
|
send_data[9]=hexToString(len);
|
|
//获取时钟 ss mm hh dd mm yy+33H
|
|
for(var i1=0 ; i1<len; i1 ++)
|
|
{
|
|
send_data[10+i1] = hexToString((parseInt(msg.substr(2*i1,2),16)+51)%256);
|
|
}
|
|
}
|
|
else{
|
|
//获取长度
|
|
send_data[9]=hexToString(len+2);
|
|
|
|
//获取 icode ,同时+33H
|
|
if(cc=="0f"){
|
|
for(var i=0 ; i<2; i++)
|
|
{
|
|
send_data[10+i] = icode.substr(2*i,2);
|
|
}
|
|
}else{
|
|
for(var i=0 ; i<2; i++)
|
|
{
|
|
send_data[11-i] = hexToString((parseInt(icode.substr(2*i,2),16)+51)%256);
|
|
}
|
|
}
|
|
|
|
//获取数据内容 +33H
|
|
if(icode=='b200'){
|
|
//文件校验和
|
|
for(var b1=0;b1<4;b1++){
|
|
send_data[15-b1] = hexToString((parseInt(msg.textChecks.substr(2*b1,2),16)+51)%256);
|
|
}
|
|
//数据长度
|
|
for(var b2=0;b2<4;b2++){
|
|
send_data[19-b2] = hexToString((parseInt(msg.lenChecks.substr(2*b2,2),16)+51)%256);
|
|
}
|
|
}else if(cc=="0f"){
|
|
for(var i=0;i<len;i++){
|
|
send_data[12+i] = hexToString((msg[i]+51)%256);
|
|
}
|
|
}else{
|
|
for(var i1=0 ; i1<len; i1++)
|
|
{
|
|
send_data[12+i1] = hexToString((parseInt(msg.substr(2*i1,2),16)+51)%256);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// if(cc=='0f'){
|
|
// let sum2=0
|
|
// for(var j2 = 0;j2 <send_length-2;j2++)
|
|
// {
|
|
// sum2= sum2+parseInt(send_data[j2],16);
|
|
// }
|
|
// sum2 = sum2.toString().slice(-2);
|
|
// send_data[send_length-2]= hexToString(sum2);
|
|
// }else{
|
|
let sum = 0;
|
|
//计算645累加校验和
|
|
for(var j = 0;j < send_length-2;j++)
|
|
{
|
|
sum =sum + parseInt(send_data[j],16);
|
|
}
|
|
sum = sum%256;
|
|
send_data[send_length-2]= hexToString(sum);
|
|
// }
|
|
send_data[send_length-1]='16'
|
|
|
|
//发送数据转为字符串
|
|
for(var k = 0; k < send_length; k++)
|
|
{
|
|
send_stringdata=send_stringdata+send_data[k]
|
|
}
|
|
console.log('发送命令:'+send_stringdata);
|
|
ecBLE.writeBLECharacteristicValue(send_stringdata, true)
|
|
if(cc == '8f')
|
|
{
|
|
return
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
ecBLE.onBLECharacteristicValueChange((str, strHex) => {
|
|
recv_uart(strHex)
|
|
console.log('设备回复:'+strHex);
|
|
resolve(getApp().globalData.uart_645datalink)
|
|
})
|
|
})
|
|
}
|
|
|
|
//串口接收解析
|
|
export function recv_uart(rec)
|
|
{
|
|
var uart_645datalink={
|
|
id:'',
|
|
cc:'',
|
|
len:'',
|
|
icode:'',
|
|
data:[],
|
|
uart_recvflag: 0,//接收正确645协议 0:未收到正确报文 1:收到正确报文
|
|
}
|
|
var recv_data=[],recv_length=0
|
|
//去除fe,获得完整645协议报文
|
|
var start = rec.indexOf('68')
|
|
recv_length = rec.length/2;
|
|
rec = rec.slice(start,rec.length)
|
|
recv_length = rec.length/2;
|
|
uart_645datalink.text = rec
|
|
if(recv_length > 0)
|
|
{
|
|
//获取645报文,按照字节存储到数组
|
|
for(var i=0 ; i<recv_length; i ++)
|
|
{
|
|
recv_data[i] = rec.substr(2*i,2);
|
|
}
|
|
|
|
let sum = 0;
|
|
if(recv_data[0] == '68'&& recv_data[7] == '68')
|
|
{
|
|
//计算645累加校验和
|
|
for(var j = 0;j < recv_length-2;j++)
|
|
{
|
|
sum =sum + parseInt(recv_data[j],16);
|
|
}
|
|
sum = sum%256;
|
|
|
|
//验证校验和
|
|
if(sum == parseInt(recv_data[recv_length-2],16))
|
|
{
|
|
//获取设备ID
|
|
uart_645datalink.id= recv_data[6]+recv_data[5]+recv_data[4]+recv_data[3]+recv_data[2]+recv_data[1];
|
|
|
|
//获取控制码
|
|
uart_645datalink.cc = recv_data[8];
|
|
|
|
//获取数据长度
|
|
uart_645datalink.len = parseInt(recv_data[9],16)
|
|
|
|
if(uart_645datalink.len >=2)
|
|
{
|
|
//获取标识编码
|
|
uart_645datalink.icode = hexToString(parseInt(recv_data[11],16)-51)+hexToString(parseInt(recv_data[10],16)-51);
|
|
//获取数据
|
|
for(var k=0;k<uart_645datalink.len-2;k++)
|
|
{
|
|
uart_645datalink.data[k] = hexToString(parseInt(recv_data[12+k],16)-51);
|
|
}
|
|
}
|
|
else{
|
|
uart_645datalink.icode = ''
|
|
//获取数据
|
|
for(var k=0;k<uart_645datalink.len;k++)
|
|
{
|
|
uart_645datalink.data[k] = hexToString(parseInt(recv_data[10+k],16)-51);
|
|
}
|
|
}
|
|
uart_645datalink.uart_recvflag = 1;
|
|
}
|
|
else{
|
|
uart_645datalink.uart_recvflag = 0;
|
|
}
|
|
|
|
}
|
|
else{
|
|
uart_645datalink.uart_recvflag = 0;
|
|
}
|
|
}
|
|
else{
|
|
uart_645datalink.uart_recvflag = 0;
|
|
}
|
|
getApp().globalData.uart_645datalink = uart_645datalink
|
|
} |