ZXCar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. let subIndex = -1
  10. let commands = []; // 升级包指令数组,每个指令是一个 ArrayBuffer
  11. let currentCommandIndex = 1;
  12. function acceptDevice(device) {
  13. return device.btid ? true : false;
  14. }
  15. function isSingleBt() {
  16. console.log('是单蓝牙');
  17. return true;
  18. }
  19. function haveBms() {
  20. console.log('是单蓝牙并且带bms');
  21. return true;
  22. }
  23. function isDevice(device, data) {
  24. // console.log(device,data,'device111');
  25. const advertisData = new Uint8Array(data.advertisData);
  26. // console.log(advertisData,'判断返回第几个字段是正常模式还是升级模式');
  27. //打印返回广播 判断返回字段是正常模式还是升级模式
  28. // console.log(advertisData.slice(4, 10).toString(),'device2222');
  29. const mac = device.btid
  30. .split('')
  31. .map((p, i) => parseInt(p + device.btid[i + 1], 16))
  32. .filter((p, i) => i % 2 == 0);
  33. // if ( advertisData.slice(4, 10).toString() == "095A5832") {
  34. // return true;
  35. // }
  36. //判断是否是智寻的蓝牙
  37. const result = data.name.startsWith("ZX");
  38. // if (data.name === "865416038002201") {
  39. // if (data.name === "ZX2503140000000") {
  40. if (result) {
  41. return true
  42. }
  43. return false;
  44. }
  45. function alterConnect(device, deviceId) {
  46. // console.log(device,app.globalData.connectionState[deviceId],'deviceliuwei');
  47. //登录 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]
  48. //登录 [[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]]
  49. //时间戳 0x67AC53E7 0x65,0xB3,0xED,0x6F 16:00:05
  50. //切换正常,工厂下发指令 [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  51. //OTA 升级指令 [[0x22,0x22,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,n,0xCB,0xAA,0xAA]]
  52. // const data = [0x1F, 0x0F, ,0x5A,0x58, 0x35, 0x32, 0x30, 0x32, 0x32, 0x32, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  53. let data=[ 0x1F, 0x0F]
  54. const device_name=app.globalData.connectionState[deviceId].deviceName
  55. const device_sn=stringToHexArray(device_name)
  56. data.push(...device_sn)
  57. data.push(0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00)
  58. // const data=[ 0x1F, 0x0F,0x38, 0x36, 0x35, 0x34, 0x31, 0x36, 0x30, 0x33, 0x38, 0x30, 0x30, 0x32, 0x32, 0x30, 0x31,0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00]
  59. // console.log(data,'datatest');
  60. const crc_data = '0x'+crc8IEEE8023(data).toString(16).toUpperCase()
  61. data.push(crc_data,0xAA,0xAA)
  62. data.unshift(0x22,0x22,0x01)
  63. return [data]
  64. }
  65. function crc8IEEE8023(data) {
  66. const poly = 0x07; // IEEE 802.3 polynomial x^8 + x^2 + x + 1
  67. let crc = 0x00;
  68. for (let i = 0; i < data.length; i++) {
  69. crc ^= data[i];
  70. for (let j = 0; j < 8; j++) {
  71. if (crc & 0x80) {
  72. crc = ((crc << 1) ^ poly) & 0xFF;
  73. } else {
  74. crc = (crc << 1) & 0xFF;
  75. }
  76. }
  77. }
  78. return crc;
  79. }
  80. function stringToHexArray(str) {
  81. const result = [];
  82. for (let i = 0; i < str.length; i++) {
  83. const charCode = str.charCodeAt(i); // 获取字符的 ASCII 码值
  84. const hexValue=`0x${charCode.toString(16).toUpperCase()}`
  85. const hexDataValue=parseInt(hexValue,16)
  86. result.push(hexDataValue);
  87. }
  88. return result;
  89. }
  90. //收到硬件方向软件方发送的数据
  91. function readData(device, value, data) {
  92. // console.log(value, 'test1111');
  93. var value = new Uint8Array(value);
  94. const normalArray = Array.from(value); // 或 [...uint8Array]
  95. console.log(normalArray,currentCommandIndex-1,commands[currentCommandIndex-1], 'value**************************');
  96. switch (normalArray[1]) {
  97. case 0x01:
  98. uni.showModal({
  99. title: '提示',
  100. confirmText: '开始升级',
  101. content: '登陆成功,当前处于升级模式是否开始升级',
  102. success: function(res) {
  103. if (res.confirm) {
  104. currentCommandIndex=0
  105. otaUpgrade().then(()=>{
  106. //登陆成功 且当前为升级模式可以发送升级文件
  107. sendNextCommand(device.mac_id)
  108. })
  109. } else {}
  110. }
  111. });
  112. break;
  113. }
  114. switch(normalArray[3]){
  115. case 0x00:
  116. sendNextCommand(device.mac_id)
  117. break;
  118. case 0x01:
  119. uni.hideLoading();
  120. common.simpleToast('升级文件回复失败', 2000)
  121. // test() 升级文件回复失败
  122. break;
  123. }
  124. return data;
  125. }
  126. //发送指令封装
  127. function sendCommand(cmd, data = []) {
  128. let cmdData = [0x02, 0x02]
  129. // cmdData.push(cmd)
  130. //协议号 cmd
  131. //包长度 data.length
  132. const dataLen=ensureInteger(data.length)
  133. //信息内容 cmd
  134. //crc校验
  135. const pData=[dataLen,cmd]
  136. const crcData='0x'+crc8IEEE8023(pData).toString(16).toUpperCase()
  137. cmdData.push(cmd,dataLen,crcData,0xAA, 0xAA)
  138. return cmdData
  139. }
  140. //转成16进制
  141. function ensureInteger(cmd){
  142. return parseInt((cmd).toString(16).toUpperCase(), 16)
  143. }
  144. function turnOnCar(device, deviceId) {
  145. return [sendCommand(0x02,[0x02])];
  146. }
  147. function turnOffCar(device, deviceId) {
  148. return [sendCommand(0x03,[0x03])];
  149. }
  150. function defenseCmd(device, deviceId) {
  151. return [sendCommand(0x04,[0x04])];
  152. }
  153. function switchFactory(device, deviceId) {
  154. //切换正常,工厂模式指令
  155. //切换工厂模式 [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  156. //切换正常模式 [[0x22,0x22,0x54,0x01,0x00,0x15,0xAA,0xAA]]
  157. return [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
  158. // return [sendCommand([0x54, 0x01, 0x01, 0x12])];
  159. }
  160. //升级按钮
  161. function otaUpgrade(device){
  162. const data = readBinBinarayToCommand()
  163. data.then(result=>{commands=result})
  164. // console.log(data,'datatest111');
  165. return data
  166. }
  167. async function readBinBinarayToCommand() {
  168. let res =await uni.request({
  169. url: 'https://opt.bms16.com/ota.BIN', // 文件的网络地址
  170. method: 'GET',
  171. responseType: 'arraybuffer', // 指定响应类型为 arraybuffer
  172. });
  173. const arrayBuffer=res[1].data
  174. const uint8Array = new Uint8Array(arrayBuffer); // 转换为 Uint8Array
  175. const test = splitArrayIntoChunks(uint8Array, 80); // 分割成长度为 80 的数组
  176. const data =makeArr(test)
  177. // const tests=data.slice(data.length-3)
  178. // console.log(data.length,tests[0],'testtest数组');
  179. // console.log(tests,'tests');
  180. return data
  181. }
  182. function sendNextCommand(mac_id) {
  183. // console.log(commands[currentCommandIndex],'command');
  184. if (currentCommandIndex >= commands.length) {
  185. uni.hideLoading();
  186. common.simpleToast('所有指令发送完成', 2000)
  187. return;
  188. }
  189. const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == mac_id);
  190. if (deviceId == undefined) {
  191. console.log('deviceId == undefined');
  192. return ;
  193. }
  194. const command = [commands[currentCommandIndex]];
  195. console.log(command,'commandtest');
  196. if(command.length==0){
  197. return
  198. }
  199. // 将数据转换为ArrayBuffer类型
  200. var buffer;
  201. buffer = common.toArrayBuffer(command.shift());
  202. // console.log(command,'command');
  203. uni.writeBLECharacteristicValue({
  204. deviceId: deviceId, // 替换为实际的设备 ID
  205. serviceId: writeServiceID, // 替换为实际的服务 ID
  206. characteristicId: writeID, // 替换为可写入的特征值 ID
  207. value: buffer,
  208. success() {
  209. // console.log('指令发送成功,等待设备响应');
  210. },
  211. fail(err) {
  212. console.error('指令发送失败', err);
  213. }
  214. });
  215. currentCommandIndex++;
  216. }
  217. function splitArrayIntoChunks(array, chunkSize) {
  218. const numChunks = Math.ceil(array.length / chunkSize);
  219. return Array.from({ length: numChunks }, (_, i) => {
  220. return array.slice(i * chunkSize, (i + 1) * chunkSize);
  221. });
  222. }
  223. // 处理分割后的数组
  224. function makeArr(array) {
  225. const result = [];
  226. let endPackage;
  227. for (let i = 0; i < array.length; i ++) {
  228. endPackage = 0x00
  229. if((i + 1) >= array.length){
  230. //当前是最后一次循环
  231. endPackage = 0x01
  232. }
  233. //协议号 30 包长度未固定array.length+8 转16进制 固件类型0x00 升级包大小(总包大小) 125396字节-》0x00,0x01,0xE9,0xD4
  234. let startArray = []
  235. const packageLen=parseInt((array[i].length+8).toString(16).toUpperCase(), 16)
  236. // const totalPackageSize=[0x00,0x00,0x01,0xE9,0xD4]
  237. const totalPackageSize=[0x00,0x00,0x01,0xe8,0x24]
  238. startArray.push(packageLen,...totalPackageSize)
  239. //软件包id
  240. const ids=splitNumber(i) // 0x00,0x01
  241. startArray.push(...ids)
  242. //是否为最后一个包
  243. startArray.push(endPackage)
  244. //软件数据包
  245. const formattedChunk = array[i].map(byte => `0x${byte.toString(16).toUpperCase()}`); // 转换为 0xXX 格式
  246. startArray.push(...formattedChunk)
  247. // if(i>95&& i<105){
  248. // console.log(startArray,'startArray-------');
  249. // }
  250. //crc检验
  251. const crcStr='0x'+crc8IEEE8023(startArray).toString(16).toUpperCase()
  252. const testCrc=parseInt(crcStr,16)
  253. // if(i>95&& i<105){
  254. // console.log(testCrc,crcStr,'startArray*******');
  255. // }
  256. startArray.push(crcStr)
  257. startArray.push(0xAA,0xAA)
  258. startArray.unshift(0x22,0x22,0x30)
  259. result.push(startArray);
  260. }
  261. // result.push([0x22,0x22,0x30,0x58,0x00,0x00,0x00,0xE9,0xD4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2C,0xAA,0xAA])
  262. return result;
  263. }
  264. function splitNumber(num) {
  265. // 将数字转换为字符串
  266. const numStr = num.toString();
  267. // 使用正则表达式分割字符串为两位一组
  268. const parts = numStr.match(/.{1,2}/g) || [];
  269. // 将字符串数组转换为数字数组
  270. let result = parts.map(part => parseInt(part, 10));
  271. // 如果数组长度为 1,前面补一个 0
  272. if (result.length === 1) {
  273. result = [0, ...result];
  274. }
  275. return result;
  276. }
  277. module.exports = {
  278. readServiceID: readServiceID,
  279. readID: readID,
  280. writeServiceID: writeServiceID,
  281. writeID: writeID,
  282. MTU: MTU,
  283. acceptDevice: acceptDevice,
  284. isDevice: isDevice,
  285. alterConnect: alterConnect,
  286. readData: readData,
  287. turnOnCar: turnOnCar,
  288. turnOffCar: turnOffCar,
  289. isSingleBt: isSingleBt,
  290. haveBms: haveBms,
  291. switchFactory: switchFactory,
  292. otaUpgrade
  293. };