条件
1、我要写入的是65k左右的数据包
2、如果将数据包分段发生,每段需有校验头和校验尾(14个字节)
3、设备校验时间间隔为
200> arraybuffer.length*2 :200 ?arraybuffer.length*2
(发生字节长度的两倍小于200时按200ms划分,若大于200则按字节长度的两倍划分)
当超过这个时间间隔就直接返回指令错误,要求重发这条指令。
描述
1、当我将每段按26个字节拆开,总共发40个字节,每次按照20个字节进行分包发送,时间间隔0.2ms(设置长了就超过发送时间),数据发送偶尔会超过规定时间,但总体能够写入设备。当全部数据写入设备后耗时约1个多小时,太长。
2、由于新版本微信支持设置mtu,故将每段按设置的最大mtu进行划分。
设置最大mtu为160(再高接收不到返回数据),数据包按每段136个字节划分,总共发150个字节,时间间隔为(160*0.05)ms。发送过程中不是很稳定,有时会断开和设备的连接,但总体能够写入设备。当全部数据写入设备后耗时约7分钟,时间还是有点长,而且有的设备不支持设置mtu。
问题
1、在APP中发送相同的数据包,每段以2048个字节划分,按20个字节分包,时间间隔为10ms。发送能稳定在1分钟以内。
2、微信版本号在7.0.15之前(不能使用mtu),小程序按每段1024个字节划分,按20个字节分包,时间间隔为0.2ms。发送整个包在2分钟以内(非调调试状态)。而在升级之后就面临发送时间超过规定时间的问题。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
求助求助,请问各位有没有什么解决办法
附上发送代码
使用promise来进行时间间隔
writeBulData: function(arrayBuffer,oldData) {
const self = this;
if(oldData!=true){
if(!arrayBuffer==false){
console.time('send_time');
dataContainer.push(arrayBuffer);
logUtil.log('数据存储器',dataContainer.length)
if(dataContainer.length>3){
dataContainer.shift();
self.writeBulData();
}
if(dataContainer.length>1){
return;
}
}
}
let buffer =dataContainer[0];
// let buffer = arrayBuffer;
let pos = 0;
let bytes = buffer.byteLength;
// logUtil.log("bytes1==", btHelper.bytesToStr(buffer));
if (bytes > 0) {
let tmpBuffer;
let tmpBuffer2;
if (bytes > app.globalData.mtu) {
// if(self.data.commListener){
self.setData({
commListener: false
})
writeHelper.delay(app.globalData.delay).then(() => {
tmpBuffer = buffer.slice(pos, pos + app.globalData.mtu);
pos += app.globalData.mtu;
bytes -= app.globalData.mtu;
// logUtil.log("bytes2==", btHelper.bytesToStr(tmpBuffer));
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
value: tmpBuffer,
success(res) {
updateHelper.setIsWork(true);
logUtil.log('第一次发送', tmpBuffer)
},
fail: function(res) {
// self.setData({
// connectFail: true
// })
dataContainer=[];
app.globalData.connected = false;
updateHelper.clear();
if (self.data.loadingIsShow) {
wx.hideLoading();
self.setData({
loadingIsShow: false
})
}
if (res.errCode == 10006) {
logUtil.showToast('连接已断开');
self.setData({
connectFail: true
})
}
// logUtil.showToast('发送失败');
logUtil.log('发送失败', res)
}
})
tmpBuffer2 = buffer.slice(pos, pos + bytes);
// logUtil.log('buffer1', tmpBuffer2)
dataContainer[0]=tmpBuffer2;
self.writeBulData(tmpBuffer2,true)
})
// }
} else {
writeHelper.delay(app.globalData.delay).then(() => {
self.setData({
commListener: true
})
tmpBuffer = buffer.slice(pos, pos + bytes);
pos += bytes;
bytes -= bytes;
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
value: tmpBuffer,
success(res) {
updateHelper.setIsWork(false);
logUtil.log('第二次发送', tmpBuffer)
// console.warn('去1数据',dataContainer)
dataContainer.shift();
if(dataContainer.length>0){
return self.writeBulData();
}
if(clearDataTimer!=null){
clearTimeout(clearDataTimer);
}
clearDataTimer=setTimeout(()=>{
dataContainer=[];
console.info('20s已到数据清空')
},20000)
},
fail: function(res) {
app.globalData.connected = false;
updateHelper.clear();
if (self.data.loadingIsShow) {
wx.hideLoading();
self.setData({
loadingIsShow: false
})
}
dataContainer=[];
// logUtil.showToast('发送失败');
console.log('发送失败', res)
if (res.errCode == 10006) {
logUtil.showToast('连接已断开');
self.setData({
connectFail: true
})
}
},complete(){
console.timeEnd('send_time');
}
})
})
}
}
},
什么建议都可以,现在完全没思路解决,公司产品也不可能因为一个小程序把原来代码给改了