ZXCar.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const common = require('../common.js');
  2. const FMBMS = require('./FMBMS.js');
  3. const readServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  4. const readID = '000036F6-0000-1000-8000-00805F9B34FB';
  5. const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB';
  6. const writeID = '000036F5-0000-1000-8000-00805F9B34FB';
  7. const MTU = 115;
  8. const app = getApp();
  9. function acceptDevice(device) {
  10. return device.btid ? true : false;
  11. }
  12. function isSingleBt() {
  13. console.log('是单蓝牙');
  14. return true;
  15. }
  16. function haveBms() {
  17. console.log('是单蓝牙并且带bms');
  18. return true;
  19. }
  20. function isDevice(device, data) {
  21. // console.log(device,data,'device111');
  22. const advertisData = new Uint8Array(data.advertisData);
  23. // console.log(advertisData.slice(4, 10).toString(),'device2222');
  24. const mac = device.btid
  25. .split('')
  26. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  27. .filter((p, i) => i % 2 == 0);
  28. // if ( advertisData.slice(4, 10).toString() == "095A5832") {
  29. // return true;
  30. // }
  31. if(data.name === "ZX2202220000000"){
  32. return true
  33. }
  34. return false;
  35. }
  36. function alterConnect(device, deviceId) {
  37. //登录 crc[0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00]
  38. //登录 [[0x22,0x22,0x01,0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00,crc_data,0xAA,0xAA]]
  39. //时间戳 0x67AC53E7 16:00:05
  40. //切换正常,工厂下发指令 [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  41. //OTA 升级指令 [[0x22,0x22,]]
  42. const data=[0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00]
  43. const crc_data=crc8Ieee8023(data)
  44. console.log(crc_data,111);
  45. return [[0x22,0x22,0x01,0x1F,0x0F,0x5A,0x58,0x32,0x32,0x30,0x32,0x32,0x32,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x08,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x67,0xAC,0x53,0xE7,0x08,0x00,crc_data,0xAA,0xAA]]
  46. //return [sendCommand(0x01, [0xAB, 0xCD, 0xAB, 0xCD])]
  47. }
  48. function crc8Ieee8023(data) {
  49. let crc = 0x00; // 初始化CRC为0
  50. const polynomial = 0x07; // CRC-8 IEEE 802.3 多项式
  51. // 遍历数据数组中的每个字节
  52. for (let byte of data) {
  53. crc ^= byte; // 将当前字节与CRC寄存器进行异或操作
  54. // 对CRC寄存器的每一位进行8次迭代
  55. for (let i = 0; i < 8; i++) {
  56. if (crc & 0x80) { // 检查最高位是否为1
  57. crc = (crc << 1) ^ polynomial; // 左移一位并与多项式异或
  58. } else {
  59. crc <<= 1; // 仅左移一位
  60. }
  61. }
  62. // 截断CRC到8位(在JavaScript中这一步是多余的,因为位运算会自动处理溢出)
  63. crc &= 0xFF;
  64. }
  65. // 返回CRC校验码的最低8位,并转换为十六进制字符串
  66. return '0x'+ crc.toString(16).toUpperCase().padStart(2, '0'); // 使用padStart确保结果始终是两位十六进制数
  67. }
  68. function readData(device, value, data) {
  69. console.log(value,'test1111');
  70. var value = new Uint8Array(value);
  71. console.log(value,'value***************************');
  72. // switch (value[0]) {
  73. // case 0x01:
  74. // //登陆成功准备
  75. // switchFactory(device)
  76. // break;
  77. // case 0x42:
  78. // //data = FMBMS.BMSReply(value.slice(2, 2 + value[1]), data)
  79. // packBmsData(device, value, data);
  80. // break;
  81. // }
  82. return data;
  83. }
  84. function sendCommand(cmd, data = []) {
  85. const startCmd=[0x02,0x02]
  86. const endCmd=[0xAA,0xAA]
  87. data=startCmd.concat(cmd).concat(endCmd)
  88. return data
  89. }
  90. function stateUpdate(device, deviceId) {
  91. return [sendCommand(0x04), sendCommand(0x02, FMBMS.BMSRead())]; //return [[0x02,0x00,0x15,0x15,0x00,0x4e,0x57,0x00,0x13,0x00,0x00,0x00,0x00,0x06,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x68,0x00,0x00,0x01,0x27,0x7b]]
  92. }
  93. function turnOn(device, deviceId) {
  94. return [sendCommand(0x02, FMBMS.BMSTurnOn())];
  95. }
  96. function turnOff(device, deviceId) {
  97. return [sendCommand(0x02, FMBMS.BMSTurnOff())];
  98. }
  99. function switchFactory(device, deviceId) {
  100. return [sendCommand([0x54,0x01,0x01,0x12])];
  101. }
  102. function otaUpgrade(device, deviceId) {
  103. return [sendCommand([0x54,0x01,0x01,0x12])];
  104. }
  105. module.exports = {
  106. readServiceID: readServiceID,
  107. readID: readID,
  108. writeServiceID: writeServiceID,
  109. writeID: writeID,
  110. MTU: MTU,
  111. acceptDevice: acceptDevice,
  112. isDevice: isDevice,
  113. alterConnect: alterConnect,
  114. readData: readData,
  115. stateUpdate: stateUpdate,
  116. turnOn: turnOn,
  117. turnOff: turnOff,
  118. isSingleBt: isSingleBt,
  119. haveBms: haveBms,
  120. };