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.

cguBle.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. const TAG_FUNCTION = 'function'
  2. const CGU_SERVICE_UUID = "0000FEE7-0000-1000-8000-00805F9B34FB";//目标服务
  3. const CGU_WRTIE_UUID = "0000FFE1-0000-1000-8000-00805F9B34FB";//目标写特征
  4. const CGU_READ_UUID = "0000FFE4-0000-1000-8000-00805F9B34FB";//目标读特征
  5. const SUCCESS_CODE=0;
  6. const FAILL_CODE=-1;
  7. module.exports = {
  8. ScanDevice: ScanDevice,
  9. StopScanDevice: StopScanDevice,
  10. connectDevice: connectDevice,
  11. disconnectDevice: disconnectDevice,
  12. transCmd: transCmd,
  13. noSleepCmd: noSleepCmd
  14. }
  15. let foundDevices = []
  16. var cguDeviceId = '';
  17. var cguServiceId = '';
  18. var cguWriteId = "";
  19. var cguReadId = "";
  20. var correctPacketIndex = 1;//理应包数
  21. var receivePacketSize = 0;//接收总包数
  22. var sendCallback;
  23. var timeOutId;
  24. var recevResendCount = 3;
  25. var recevSuccessFlag = false;
  26. var flashFlag = false;
  27. var valueArray = new Array();//完整数据保存
  28. var cguSendData = new Array();//送法的数据
  29. var cguSendIndex = 0;
  30. var cguResendCount = 3;
  31. var IntervalTimer = 0;//搜搜蓝牙定时器
  32. var SetoutTimer = 0;
  33. //打开蓝牙适配器
  34. function openBleAdapter(callback) {
  35. let DevResult = {};
  36. if (!wx.openBluetoothAdapter) {
  37. DevResult.code = 10001;
  38. DevResult.msg = "当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试";
  39. DevResult.data = null;
  40. typeof callback == TAG_FUNCTION && callback(DevResult)
  41. } else {
  42. wx.openBluetoothAdapter({
  43. success: function (res) {
  44. DevResult.code = 0;
  45. DevResult.msg = "初始化蓝牙成功";
  46. DevResult.data = res;
  47. typeof callback == TAG_FUNCTION && callback(DevResult)
  48. },
  49. fail: function (res) {
  50. DevResult.code = 10001;
  51. DevResult.msg = "初始化蓝牙失败";
  52. DevResult.data = res;
  53. typeof callback == TAG_FUNCTION && callback(DevResult)
  54. }
  55. })
  56. }
  57. }
  58. //搜索蓝牙 flag 是过滤标识
  59. function ScanDevice(flag, callback) {
  60. openBleAdapter(function (res) {
  61. if (res.code == 0) {
  62. let DevResult = {};
  63. foundDevices = [];
  64. wx.startBluetoothDevicesDiscovery({
  65. services: [],
  66. success: function (res) {
  67. fn();
  68. SetoutTimer = setTimeout(function () {
  69. StopScanDevice(function (res) {
  70. DevResult.code = 10002;
  71. DevResult.msg = "搜索超时,未发现设备";
  72. DevResult.data = null;
  73. typeof callback == TAG_FUNCTION && callback(DevResult);
  74. });
  75. }, 30000);
  76. function fn() {
  77. wx.onBluetoothDeviceFound(function (res) {
  78. for (let i = 0; i < res.devices.length; i++) {
  79. let isHave = false
  80. for (let j = 0; j < foundDevices.length; j++) {
  81. if (res.devices[i].deviceId == foundDevices[j].deviceId) {
  82. isHave = true
  83. break
  84. }
  85. }
  86. var blueName = res.devices[i]['name'];
  87. if (typeof (blueName) == undefined || blueName ==''){
  88. }else{
  89. console.log("蓝牙名称:", res.devices[i]['name']);
  90. if (isHave == false) {
  91. foundDevices.push(res.devices[i]);
  92. var deviceName = res.devices[i]['name'];
  93. //过滤器 name
  94. if (deviceName.indexOf(flag) != -1) {
  95. var deviceId = res.devices[i]['deviceId'];
  96. //停止搜搜
  97. StopScanDevice(function (res) {
  98. if (res.code == 0) {
  99. clearTimeout(SetoutTimer);
  100. SetoutTimer = 0;
  101. let device = new Object();
  102. device.deviceId = deviceId;
  103. device.name = deviceName;
  104. DevResult.code = 0;
  105. DevResult.msg = "搜索成功";
  106. DevResult.data = device;
  107. typeof callback == TAG_FUNCTION && callback(DevResult)
  108. } else {
  109. DevResult.code = 10002;
  110. DevResult.msg = "搜索失败";
  111. DevResult.data = res;
  112. typeof callback == TAG_FUNCTION && callback(DevResult)
  113. }
  114. })
  115. break
  116. } else {
  117. }
  118. }
  119. }
  120. }
  121. })
  122. }
  123. },
  124. fail: function (res) {
  125. DevResult.code = 10001;
  126. DevResult.msg = "搜索失败";
  127. DevResult.data = res;
  128. typeof callback == TAG_FUNCTION && callback(DevResult)
  129. }
  130. })
  131. } else {
  132. typeof callback == TAG_FUNCTION && callback(res)
  133. }
  134. });
  135. }
  136. //停止搜索
  137. function StopScanDevice(callback) {
  138. if (SetoutTimer != 0) {
  139. clearTimeout(SetoutTimer);
  140. SetoutTimer = 0;
  141. }
  142. let DevResult = {};
  143. wx.stopBluetoothDevicesDiscovery({
  144. success: function (res) {
  145. DevResult.code = 0;
  146. DevResult.data = res;
  147. DevResult.msg = "操作成功";
  148. typeof callback == TAG_FUNCTION && callback(DevResult)
  149. },
  150. fail: function (res) {
  151. DevResult.code = 10030;
  152. DevResult.data = res;
  153. DevResult.msg = "蓝牙已关闭";
  154. typeof callback == TAG_FUNCTION && callback(DevResult)
  155. }
  156. })
  157. }
  158. //连接蓝牙
  159. function connectDevice(device, callback, callback_2) {
  160. console.log("连接蓝牙==================");
  161. console.log(device);
  162. var DevResult = {};
  163. if (device != null) {
  164. cguWriteId = '';
  165. cguReadId = '';
  166. cguDeviceId = '';
  167. let deviceId = device.deviceId;
  168. cguDeviceId = deviceId;
  169. wx.createBLEConnection({
  170. deviceId: deviceId,
  171. success: function (data) {
  172. console.log("连接成功设备,开始使能服务");
  173. wx.onBLEConnectionStateChange(function (res) {
  174. if (res.connected == false) {
  175. DevResult.code=1;
  176. DevResult.data=null;
  177. DevResult.msg = "监听到断开连接";
  178. console.log("蓝牙异常断开");
  179. typeof callback_2 == TAG_FUNCTION && callback_2(DevResult);
  180. }else{
  181. DevResult.code = 0;
  182. DevResult.data = null;
  183. DevResult.msg = "监听连接成功";
  184. typeof callback_2 == TAG_FUNCTION && callback_2(DevResult);
  185. }
  186. })
  187. wx.getBLEDeviceServices({
  188. deviceId: deviceId,
  189. success: function (res) {
  190. for (let i = 0; i < res.services.length; i++) {
  191. let serviceuuid = res.services[i].uuid.toUpperCase();
  192. if (CGU_SERVICE_UUID.indexOf(serviceuuid) != -1) {
  193. cguServiceId = serviceuuid;
  194. //获取特征
  195. wx.getBLEDeviceCharacteristics({
  196. deviceId: cguDeviceId,
  197. serviceId: cguServiceId,
  198. success: function (res) {
  199. for (let i = 0; i < res.characteristics.length; i++) {
  200. let chauuid = res.characteristics[i].uuid.toUpperCase();
  201. if (CGU_READ_UUID.indexOf(chauuid) != -1) {
  202. cguReadId = chauuid;
  203. }
  204. else if (CGU_WRTIE_UUID.indexOf(chauuid) != -1) {
  205. cguWriteId = chauuid;
  206. }
  207. }
  208. if (cguWriteId.length == 0 || cguReadId.length == 0) {
  209. DevResult.code = -3;
  210. DevResult.data = res;
  211. DevResult.msg = "获取特征值失败";
  212. typeof callback == TAG_FUNCTION && callback(DevResult);
  213. }
  214. else {
  215. openReceiveData(function (res) {
  216. typeof callback == TAG_FUNCTION && callback(res);
  217. });
  218. }
  219. },
  220. fail: function (res) {
  221. DevResult.code = -2;
  222. DevResult.data = res;
  223. DevResult.msg = "获取特征值失败";
  224. typeof callback == TAG_FUNCTION && callback(DevResult);
  225. }
  226. })
  227. }
  228. }
  229. }, fail: function (res) {
  230. DevResult.code = -1;
  231. DevResult.data = res;
  232. DevResult.msg = "获取特征值失败";
  233. typeof callback == TAG_FUNCTION && callback(DevResult);
  234. }
  235. })
  236. },
  237. fail: function (err) {
  238. disconnectDevice(function (res) {
  239. DevResult.code = 10001;
  240. DevResult.data = res;
  241. DevResult.msg = "获取特征值失败";
  242. typeof callback == TAG_FUNCTION && callback(DevResult);
  243. });
  244. }
  245. })
  246. } else {
  247. DevResult.code = 10002;
  248. DevResult.data = device;
  249. DevResult.msg = "蓝牙名称为空";
  250. typeof callback == TAG_FUNCTION && callback(DevResult)
  251. }
  252. }
  253. //数据接收
  254. function openReceiveData(callback) {
  255. var DevResult = {};
  256. wx.notifyBLECharacteristicValueChange({
  257. deviceId: cguDeviceId,
  258. serviceId: cguServiceId,
  259. characteristicId: cguReadId,
  260. state: true,
  261. success: function (res) {
  262. DevResult.code = 0;
  263. DevResult.data = null;
  264. DevResult.msg = "连接蓝牙成功";
  265. sendData(noSleepCmd(), function (res) {
  266. });
  267. setTimeout(function(){
  268. typeof callback == TAG_FUNCTION && callback(DevResult);
  269. },500)
  270. }, fail: function (res) {
  271. DevResult.code = -1;
  272. DevResult.data = res;
  273. DevResult.msg = "打开notify失败";
  274. typeof callback == TAG_FUNCTION && callback(DevResult);
  275. }
  276. })
  277. wx.onBLECharacteristicValueChange(function (characteristic) {
  278. var DevResult = {};
  279. if (CGU_READ_UUID.indexOf(characteristic.characteristicId.toUpperCase()) != -1) {
  280. let hex = Array.prototype.map.call(new Uint8Array(characteristic.value), x => ('00' + x.toString(16)).slice(-2)).join('');
  281. console.log("原始数据:" + hex);
  282. if (hex.length > 6) {
  283. let receivePacketIndex = parseInt(hex.substring(4, 6), 16);
  284. if (parseInt(hex.substring(2, 4), 16) == 0x80) {
  285. receivePacketSize = parseInt(hex.substring(4, 6), 16);
  286. }
  287. if (receivePacketSize == 1) {
  288. if ((parseInt(hex.substring(6, 8), 16) != 0xc7) && (parseInt(hex.substring(6, 8), 16) != 0xc0)) {
  289. console.log("进入只有一包数据的回调");
  290. recevSuccessFlag = true;
  291. recevResendCount = 3;
  292. clearInterval(timeOutId);
  293. hex = hex.substring(6, hex.length - 2);
  294. typeof sendCallback == TAG_FUNCTION && sendCallback(SUCCESS_CODE,hex);
  295. }
  296. } else {
  297. valueArray.push(hex.substring(6, hex.length - 2));
  298. if (correctPacketIndex == 1 && parseInt(hex.substring(2, 4), 16) != 0x80) {
  299. recevSuccessFlag = false
  300. console.log("第一帧数据丢包");
  301. correctPacketIndex++;
  302. }
  303. else if (correctPacketIndex != receivePacketIndex && correctPacketIndex != 1) {
  304. recevSuccessFlag = false
  305. console.log("数据丢包");
  306. correctPacketIndex++;
  307. } else if (correctPacketIndex == receivePacketSize) {
  308. correctPacketIndex = 1;
  309. var data = "";
  310. for (var i = 0; i < receivePacketSize; i++) {
  311. data = data + valueArray[i];
  312. }
  313. console.log("组包后:" + data);
  314. if ((parseInt(data.substring(0, 2), 16) != 0xc7)) {
  315. recevSuccessFlag = true;
  316. recevResendCount = 3;
  317. clearInterval(timeOutId);
  318. typeof sendCallback == TAG_FUNCTION && sendCallback(SUCCESS_CODE,data);
  319. }
  320. } else {
  321. correctPacketIndex++;
  322. }
  323. }
  324. }
  325. }
  326. });
  327. }
  328. //断开连接
  329. function disconnectDevice(callback) {
  330. let deviceId = cguDeviceId;
  331. wx.closeBLEConnection({
  332. deviceId: deviceId,
  333. success: function (res) {
  334. if (res.errCode==0){
  335. wx.closeBluetoothAdapter({
  336. success: function (data) {
  337. let DevResult = {};
  338. DevResult.code = 0;
  339. DevResult.msg = "断开成功";
  340. DevResult.data = data;
  341. typeof callback == TAG_FUNCTION && callback(DevResult);
  342. }
  343. })
  344. }else{
  345. let DevResult = {};
  346. DevResult.code = 10001;
  347. DevResult.msg = "断开失败";
  348. DevResult.data = res;
  349. typeof callback == TAG_FUNCTION && callback(DevResult);
  350. }
  351. }, fail: function (err) {
  352. let DevResult = {};
  353. DevResult.code = 10002;
  354. DevResult.msg = "断开失败";
  355. DevResult.data = err;
  356. typeof callback == TAG_FUNCTION && callback(DevResult);
  357. }
  358. })
  359. }
  360. //发送数据
  361. //bufferArray:封好数据包并拆分20字节一帧的数组
  362. function sendData(bufferArray, callback) {
  363. var that = this;
  364. cguSendData = bufferArray;
  365. cguSendIndex = 0;
  366. sendCallback = callback;
  367. valueArray = [];//重置记录数据
  368. if (flashFlag) {
  369. flashFlag = false;
  370. recevSuccessFlag = true;
  371. } else {
  372. recevSuccessFlag = false;
  373. }
  374. correctPacketIndex = 1;
  375. startSendData();
  376. }
  377. function startSendData() {
  378. let DevResult = {};
  379. var that = this;
  380. let buffer = cguSendData[cguSendIndex];
  381. console.log("开始发送第" + cguSendIndex + "包数据: " + Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('') + ",总包数: " + cguSendData.length)
  382. wx.writeBLECharacteristicValue({
  383. deviceId: cguDeviceId,
  384. serviceId: cguServiceId,
  385. characteristicId: cguWriteId,
  386. value: buffer,
  387. success: function (res) {
  388. console.log("发送第" + cguSendIndex + "包数据成功");
  389. cguResendCount = 3;
  390. cguSendIndex++;
  391. if (cguSendIndex < cguSendData.length) {
  392. startSendData();//继续发下一个
  393. } else {
  394. // 设置超时
  395. clearInterval(timeOutId);
  396. timeOutId = null;
  397. timeOutId = setInterval(resendData, 10000);
  398. }
  399. },
  400. fail: function () {
  401. clearInterval(timeOutId);
  402. timeOutId = null;
  403. console.log("发送第" + cguSendIndex + "包数据失败");
  404. typeof sendCallback == TAG_FUNCTION && sendCallback(FAILL_CODE,"数据发送失败")
  405. }
  406. })
  407. }
  408. function resendData() {
  409. recevResendCount = 3;
  410. clearInterval(timeOutId);
  411. timeOutId = null;
  412. console.log("数据接收失败,数据接收超时");
  413. typeof sendCallback == TAG_FUNCTION && sendCallback(FAILL_CODE, "数据接收失败");
  414. }
  415. /**
  416. * cos通道接口
  417. * cosArr:指令数组;channelType:访问通道10访问IC卡,20访问ESAM;其他默认透传纯指令数据
  418. * callback(回调),返回响应指令+状态码数组,
  419. */
  420. function transCmd(cosArr, channelType, callback) {
  421. var CmdArrayData = new Array();
  422. if (cosArr.length>3){
  423. let DevResult={};
  424. var test = Group(cosArr, 3);
  425. if (channelType == "20"){
  426. let cmd = getEsamCmdByArray(test[0]);
  427. sendData(cmd, function (code, res) {
  428. if (code == 0 && parseInt(res.substring(0, 2), 16) == 0xB4) {
  429. var resultA = getArrayByReturnCmd(res);
  430. cmd = getEsamCmdByArray(test[1]);
  431. sendData(cmd, function (code2, res2) {
  432. if (code2 == 0 && parseInt(res2.substring(0, 2), 16) == 0xB4) {
  433. var resultB = getArrayByReturnCmd(res2);
  434. var dataRarray = resultA.concat(resultB);
  435. DevResult.code = 0;
  436. DevResult.msg = "透传ESAM指令成功";
  437. DevResult.data = dataRarray;
  438. typeof callback == TAG_FUNCTION && callback(DevResult);
  439. } else {
  440. DevResult.code = 1;
  441. DevResult.msg = "透传ESAM指令失败" + res2;
  442. DevResult.data = res2;
  443. typeof callback == TAG_FUNCTION && callback(DevResult);
  444. }
  445. })
  446. } else {
  447. DevResult.code = 1;
  448. DevResult.msg = "透传ESAM指令失败" + res;
  449. DevResult.data = res;
  450. typeof callback == TAG_FUNCTION && callback(DevResult);
  451. }
  452. })
  453. } else if (channelType=="10"){
  454. let cmd = getICCardCmdByArray(test[0]);
  455. sendData(cmd, function (code, res) {
  456. if (code == 0 && parseInt(res.substring(0, 2), 16) == 0xB3) {
  457. var resultA = getArrayByReturnCmd(res);
  458. cmd = getICCardCmdByArray(test[1]);
  459. sendData(cmd, function (code2, res2) {
  460. if (code2 == 0 && parseInt(res2.substring(0, 2), 16) == 0xB3) {
  461. var resultB = getArrayByReturnCmd(res2);
  462. var dataRarray = resultA.concat(resultB);
  463. DevResult.code = 0;
  464. DevResult.msg = "透传IC卡指令成功";
  465. DevResult.data = dataRarray;
  466. typeof callback == TAG_FUNCTION && callback(DevResult);
  467. } else {
  468. DevResult.code = 1;
  469. DevResult.msg = "透传IC卡指令失败" + res2;
  470. DevResult.data = res2;
  471. typeof callback == TAG_FUNCTION && callback(DevResult);
  472. }
  473. })
  474. } else {
  475. DevResult.code = 1;
  476. DevResult.msg = "透传IC卡指令失败" + res;
  477. DevResult.data = res;
  478. typeof callback == TAG_FUNCTION && callback(DevResult);
  479. }
  480. })
  481. }
  482. }else{
  483. let DevResult = {};
  484. if (channelType == "10") {
  485. sendData(getICCardCmdByArray(cosArr), function (code, res) {
  486. if (code == 0) {
  487. if (parseInt(res.substring(0, 2), 16) == 0xB3) {
  488. DevResult.code = 0;
  489. DevResult.data = getArrayByReturnCmd(res);
  490. DevResult.msg = "透传IC卡指令成功";
  491. typeof callback == TAG_FUNCTION && callback(DevResult);
  492. } else {
  493. DevResult.code = -1;
  494. DevResult.data = res;
  495. DevResult.msg = "透传IC卡指令失败:" + res;
  496. typeof callback == TAG_FUNCTION && callback(DevResult);
  497. }
  498. } else {
  499. DevResult.code = -2;
  500. DevResult.data = res;
  501. DevResult.msg = "指令透传失败:" + res;
  502. typeof callback == TAG_FUNCTION && callback(DevResult);
  503. }
  504. })
  505. } else if (channelType == "20") {
  506. sendData(getEsamCmdByArray(cosArr), function (code, res) {
  507. if (code == 0) {
  508. if (parseInt(res.substring(0, 2), 16) == 0xB4) {
  509. DevResult.code = 0;
  510. DevResult.data = getArrayByReturnCmd(res);
  511. DevResult.msg = "透传ESAM指令成功";
  512. typeof callback == TAG_FUNCTION && callback(DevResult);
  513. } else {
  514. DevResult.code = -2;
  515. DevResult.data = res;
  516. DevResult.msg = "透传ESAM指令失败:" + res;
  517. typeof callback == TAG_FUNCTION && callback(DevResult);
  518. }
  519. }
  520. })
  521. } else {
  522. if (typeof (cosArr) == "object") {
  523. } else if (typeof (cosArr) == "string") {
  524. sendData(packetData(cosArr), function (code, res) {
  525. if (code == 0) {
  526. DevResult.code = 0;
  527. DevResult.data = res;
  528. DevResult.msg = "透传其他指令成功";
  529. typeof callback == TAG_FUNCTION && callback(DevResult);
  530. } else {
  531. DevResult.code = 1;
  532. DevResult.data = res;
  533. DevResult.msg = "透传其他指令失败";
  534. typeof callback == TAG_FUNCTION && callback(DevResult);
  535. }
  536. })
  537. }
  538. }
  539. }
  540. }
  541. //将数组拆分为小数组
  542. function Group(array, subGroupLength) {
  543. let index = 0;
  544. let newArray = [];
  545. while (index < array.length) {
  546. newArray.push(array.slice(index, index += subGroupLength));
  547. }
  548. return newArray;
  549. }
  550. function getICCardCmdByArray(cosArr) {
  551. let arrLen = cosArr.length;
  552. let cmd = "A3" + intToHexString(arrLen, 1, false);
  553. for (let i = 0; i < cosArr.length; i++) {
  554. cmd = cmd + intToHexString(cosArr[i].length / 2, 1, false) + cosArr[i];
  555. }
  556. return packetData(cmd);
  557. }
  558. function getEsamCmdByArray(cosArr) {
  559. let arrLen = cosArr.length;
  560. let cmd = "A4" + intToHexString(arrLen, 1, false);
  561. for (let i = 0; i < cosArr.length; i++) {
  562. cmd = cmd + intToHexString(cosArr[i].length / 2, 1, false) + cosArr[i];
  563. }
  564. return packetData(cmd);
  565. }
  566. function getArrayByReturnCmd(cmd) {
  567. let cmdCount = parseInt(cmd.substring(2, 6), 16);
  568. let returnCosArr = new Array();
  569. let startIndex = 6;
  570. let cmdLength = 0;
  571. for (let i = 0; i < cmdCount; i++) {
  572. cmdLength = parseInt(cmd.substring(startIndex, startIndex + 2), 16);
  573. let cmdStr = cmd.substring(startIndex + 2, startIndex + 2 + 2 * cmdLength);
  574. startIndex += 2 + cmdLength * 2;
  575. returnCosArr[i] = cmdStr;
  576. }
  577. return returnCosArr;
  578. }
  579. /**
  580. * 不休眠60s
  581. *
  582. */
  583. function noSleepCmd() {
  584. let cmd = "D13C";//60s
  585. return packetData(cmd);
  586. }
  587. /**
  588. * 不休眠30s
  589. *
  590. */
  591. function getNoSleepCmd() {
  592. let cmd = "D11E";
  593. return packetData(cmd);
  594. }
  595. function packetData(content) {
  596. console.log("开始拆包:" + content);
  597. let bufferArr = new Array();
  598. let sendPacketSize;
  599. let ST, CTL, DATA, BCC;
  600. let contentLength = content.length;
  601. if (content.length % 15 == 0) {
  602. sendPacketSize = parseInt(content.length / (15 * 2));
  603. } else {
  604. sendPacketSize = parseInt(content.length / (15 * 2)) + 1;
  605. }
  606. for (let i = 0; i < sendPacketSize; i++) {
  607. if (i == 0) {
  608. CTL = "80" + intToHexString(sendPacketSize, 1, false);
  609. } else {
  610. CTL = "00" + intToHexString(i + 1, 1, false);
  611. }
  612. if (contentLength >= 15 * 2) {
  613. DATA = content.substring(i * 15 * 2, (i + 1) * 15 * 2);
  614. contentLength = contentLength - 15 * 2;
  615. } else {
  616. DATA = content.substring(content.length - contentLength, content.length);
  617. }
  618. ST = "5" + parseInt(DATA.length / 2, 10).toString(16);
  619. BCC = getBcc(ST + CTL + DATA);
  620. console.log("组帧:" + "ST:" + ST + " CTL:" + CTL + " DATA:" + DATA + " BCC:" + BCC);
  621. bufferArr.push(str2array(ST + CTL + DATA + BCC).buffer);
  622. }
  623. return bufferArr;
  624. }
  625. //num:要转换的数(number) bitNum:转换后的字节数(number) isBig:是不是大端模式(boolean)
  626. function intToHexString(num, bitNum, isBig) {
  627. //转大端hex并补足
  628. let hex = num.toString(16);
  629. for (let i = hex.length; i < bitNum * 2; i++) {
  630. hex = "0" + hex;
  631. }
  632. //多位截取
  633. if (hex.length > bitNum * 2) {
  634. hex = hex.substring(hex.length - bitNum * 2);
  635. }
  636. //转小端
  637. if (isBig == false) {
  638. let temp = "";
  639. for (let i = hex.length - 2; i >= 0; i -= 2) {
  640. temp = temp + hex.substring(i, i + 2);
  641. }
  642. hex = temp;
  643. }
  644. return hex;
  645. }
  646. //array 转str
  647. function array2Str(array) {
  648. let data = "";
  649. for (let i = 0; i < array.length; i++) {
  650. data = data + array[i];
  651. }
  652. return data;
  653. }
  654. //str 转 array
  655. function str2array(str) {
  656. let array = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function (h) {
  657. return parseInt(h, 16)
  658. }))
  659. return array;
  660. }
  661. //计算BCC
  662. function getBcc(data) {
  663. var bcc = 0x00;
  664. var bcc1;
  665. for (var i = 0; i < data.length / 2; i++) {
  666. bcc1 = parseInt(data.substring(i * 2, i * 2 + 2), 16);
  667. bcc = bcc1 ^ bcc;
  668. }
  669. let str = bcc.toString(16);
  670. if (str.length == 1) {
  671. str = "0" + str;
  672. }
  673. return str;
  674. }
  675. function arraycopy(/* array*/ from, /* index */ from_start,
  676. /* array */ to, /* index */ to_start,
  677. /* integer */ length) {
  678. //逻辑代码;
  679. for (var i = 0; i < length; i++) {
  680. to[to_start + i] = from[from_start + i];
  681. }
  682. }