Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cguBle.js 23KB

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