@jiabaida/tools 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/core/BleApiManager.js +1 -1
- package/dist/cjs/core/BleCmdAnalysis/BaseParamProtocol.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/BleCmdAnalysis.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/BleCmdDD.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/BleCmdFFAA.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/BleCmdHVES.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/ESHostProtocol.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis/readAndSetParam.js +1 -0
- package/dist/cjs/core/BleCmdAnalysis.js +1 -0
- package/dist/cjs/core/BleDataProcess.js +1 -0
- package/dist/cjs/core/OtaUpgrade.js +1 -0
- package/dist/cjs/core/TelinkApi.js +1 -0
- package/dist/cjs/core/Transfer.js +1 -0
- package/dist/cjs/core/commonfun.js +1 -1
- package/dist/cjs/core/dataJson/baseParamsJson.js +1 -0
- package/dist/cjs/core/keyAndPwdManager.js +1 -0
- package/dist/cjs/index.js +1 -1
- package/dist/esm/core/BleApiManager.js +1 -1
- package/dist/esm/core/BleCmdAnalysis/BaseParamProtocol.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/BleCmdAnalysis.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/BleCmdDD.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/BleCmdFFAA.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/BleCmdHVES.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/ESHostProtocol.js +1 -0
- package/dist/esm/core/BleCmdAnalysis/readAndSetParam.js +1 -0
- package/dist/esm/core/BleCmdAnalysis.js +1 -0
- package/dist/esm/core/BleDataProcess.js +1 -0
- package/dist/esm/core/OtaUpgrade.js +1 -0
- package/dist/esm/core/TelinkApi.js +1 -0
- package/dist/esm/core/Transfer.js +1 -0
- package/dist/esm/core/commonfun.js +1 -1
- package/dist/esm/core/dataJson/baseParamsJson.js +1 -0
- package/dist/esm/core/keyAndPwdManager.js +1 -0
- package/dist/esm/index.js +1 -1
- package/package.json +13 -3
- package/src/core/BleApiManager.js +487 -0
- package/src/core/BleCmdAnalysis/BaseParamProtocol.js +651 -0
- package/src/core/BleCmdAnalysis/BleCmdAnalysis.js +220 -0
- package/src/core/BleCmdAnalysis/BleCmdDD.js +1214 -0
- package/src/core/BleCmdAnalysis/BleCmdDDA4.js +762 -0
- package/src/core/BleCmdAnalysis/BleCmdFFAA.js +407 -0
- package/src/core/BleCmdAnalysis/BleCmdHVES.js +1222 -0
- package/src/core/BleCmdAnalysis/ESHostProtocol.js +829 -0
- package/src/core/BleCmdAnalysis/index.js +7 -0
- package/src/core/BleCmdAnalysis/readAndSetParam.js +288 -0
- package/src/core/BleDataProcess.js +355 -0
- package/src/core/OtaUpgrade.js +338 -0
- package/src/core/TelinkApi.js +73 -0
- package/src/core/Transfer.js +516 -0
- package/src/core/array.js +10 -0
- package/src/core/commonfun.js +84 -0
- package/src/core/dataJson/baseParamsJson.js +754 -0
- package/src/core/dataJson/index.js +1 -0
- package/src/core/keyAndPwdManager.js +346 -0
- package/src/core/mqttServer.js +296 -0
- package/src/core/rsaEncrypt.js +45 -0
- package/src/index.js +11 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { decimalToTwoByteHexArray, decimalToHex, getParamBaseValue } from '../BleDataProcess.js'
|
|
2
|
+
import { readExistFactory, getDDA5FAAsync, enterFactory, getDDA5OldAsync, existFactory, setDDA5FAAsync, setDDA5OldAsync } from './BleCmdDD.js'
|
|
3
|
+
|
|
4
|
+
// #region 发指令读取参数
|
|
5
|
+
/**
|
|
6
|
+
* 发指令读取参数
|
|
7
|
+
* @param {*} chipType 芯片类型
|
|
8
|
+
* @param {*} deviceId 设备deviceId
|
|
9
|
+
* @param {*} paramInfo 参数信息
|
|
10
|
+
* @param {*} paramInfo.address 新协议开始读取地址(十进制)如 31
|
|
11
|
+
* @param {*} paramInfo.length 新协议读参数内容长度(需要读取的所有参数的字节数之和/2)(十进制)如 1
|
|
12
|
+
* @param {*} paramInfo.oldAddress 旧协议读取地址 默认读一个(十六进制字符串)如 '0x1f'
|
|
13
|
+
* @param {*} needFactoryMode 是否需要进入工厂模式 默认true
|
|
14
|
+
* @returns data 读取到的参数值(只返回内容的十进制数组)
|
|
15
|
+
*/
|
|
16
|
+
export const readParamCmd = (chipType, deviceId, paramInfo, needFactoryMode = true) => {
|
|
17
|
+
console.log('chipType: ', chipType);
|
|
18
|
+
return new Promise(async (resolve, reject) => {
|
|
19
|
+
const { address = 0, length = 1, oldAddress = "0x1f" } = paramInfo;
|
|
20
|
+
/** 地址转换为16进制 */
|
|
21
|
+
const hexAddress = decimalToTwoByteHexArray(address);
|
|
22
|
+
// 长度转换为16进制
|
|
23
|
+
const hexLength = decimalToHex(length);
|
|
24
|
+
const handleRead = async (readFunction, index) => {
|
|
25
|
+
try {
|
|
26
|
+
const hex = await readFunction();
|
|
27
|
+
if (index == 4 && needFactoryMode) await readExistFactory(deviceId);
|
|
28
|
+
if (!hex) {
|
|
29
|
+
resolve(null);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const data = hex.slice(index, -3);
|
|
33
|
+
console.log("data: 读参数", data);
|
|
34
|
+
if (data) {
|
|
35
|
+
resolve(data);
|
|
36
|
+
} else {
|
|
37
|
+
resolve(null);
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error("读取参数出错: ", error);
|
|
41
|
+
reject(error);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
try {
|
|
45
|
+
if (chipType) {
|
|
46
|
+
await handleRead(async () => await getDDA5FAAsync(deviceId, hexAddress, hexLength), 7);
|
|
47
|
+
} else {
|
|
48
|
+
if (!oldAddress) return resolve(null);
|
|
49
|
+
if (needFactoryMode) await enterFactory(deviceId);
|
|
50
|
+
await handleRead(async () => await getDDA5OldAsync(deviceId, oldAddress), 4);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error("读取参数出错---: ", error);
|
|
55
|
+
reject(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
// #region 系统读参数处理
|
|
60
|
+
/**读参数处理
|
|
61
|
+
* @param {*} chipType 芯片类型
|
|
62
|
+
* @param {*} deviceId 设备deviceId
|
|
63
|
+
* @param {Object} paramInfo 开始读取的参数信息
|
|
64
|
+
* @param {*} paramInfo.paramNo 新协议开始读取地址(十进制)如 31
|
|
65
|
+
* @param {*} paramInfo.paramLength 新协议读参数内容长度(内容字节数)(十进制)如 2字节,实际读取一个参数
|
|
66
|
+
* @param {*} paramInfo.oldParamNo 旧协议读取地址 默认读一个(十六进制字符串)如 '0x1f'
|
|
67
|
+
* @returns — data 读取到的参数值(只返回内容的十进制数组如[1, 2])
|
|
68
|
+
*/
|
|
69
|
+
export const getSysParamCmd = async (chipType, deviceId, paramInfo, needFactoryMode = true) => {
|
|
70
|
+
try {
|
|
71
|
+
console.log('paramInfo: ', paramInfo);
|
|
72
|
+
const paramObj = {
|
|
73
|
+
address: paramInfo.paramNo,
|
|
74
|
+
length: paramInfo.paramLength / 2,
|
|
75
|
+
oldAddress: paramInfo.oldParamNo,
|
|
76
|
+
}
|
|
77
|
+
let data = await readParamCmd(chipType, deviceId, paramObj, needFactoryMode);
|
|
78
|
+
if (!data) return null;
|
|
79
|
+
// data = data.map((el)=>{
|
|
80
|
+
// const hexValue = decimalToHex(el)
|
|
81
|
+
// console.log('el: ------------', hexValue);
|
|
82
|
+
// return hexValue;
|
|
83
|
+
// })
|
|
84
|
+
// console.log('data: ', data);
|
|
85
|
+
return data
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('error: 读系统参数报错 getSysParamCmd', error);
|
|
88
|
+
throw error
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// #region 系统写参数处理
|
|
93
|
+
/**
|
|
94
|
+
* @param {*} chipType 芯片类型
|
|
95
|
+
* @param {*} deviceId 设备deviceId
|
|
96
|
+
* @param {Array|Object} paramInfoArray 包含多个参数信息的数组或单个参数信息对象
|
|
97
|
+
* @param {*} paramInfoArray[].newValue 新设置的参数输入值十进制值
|
|
98
|
+
* @param {*} paramInfoArray[].paramValue 新设置的参数值十进制值 部分参数输入的值需单独处理
|
|
99
|
+
* @param {*} paramInfoArray[].oldParamNo 旧协议写入地址 默认一个(十六进制字符串)如 '0x1f'
|
|
100
|
+
* @param {*} paramInfoArray[].paramNo 新协议写入地址 十进制
|
|
101
|
+
* @param {*} paramInfoArray[].paramLength 新协议写参数内容长度(内容字节数)(十进制)如 2字节,实际写入一个参数
|
|
102
|
+
* @param {*} paramInfoArray[].divisor 缩放系数(十进制)如 10
|
|
103
|
+
* @param {*} paramInfoArray[].isTemp 是否是温度参数
|
|
104
|
+
*/
|
|
105
|
+
export const setSysParamCmd = async (chipType, deviceId, paramInfoArray) => {
|
|
106
|
+
try {
|
|
107
|
+
console.log('paramInfoArray: ', paramInfoArray);
|
|
108
|
+
|
|
109
|
+
// 确保paramInfoArray是数组
|
|
110
|
+
const paramsToProcess = Array.isArray(paramInfoArray) ? paramInfoArray : [paramInfoArray];
|
|
111
|
+
|
|
112
|
+
// 收集所有处理后的参数对象到一个数组中
|
|
113
|
+
const paramObjs = [];
|
|
114
|
+
|
|
115
|
+
for (const paramInfo of paramsToProcess) {
|
|
116
|
+
let value = Math.round(Number(paramInfo?.paramValue || paramInfo.newValue) / Number(paramInfo?.divisor || 1));
|
|
117
|
+
console.log('value: ', value);
|
|
118
|
+
if (paramInfo.isTemp) {
|
|
119
|
+
value = 2731 + (Number(paramInfo.newValue) * 10)
|
|
120
|
+
}
|
|
121
|
+
const valuesHexs = decimalToTwoByteHexArray(value);
|
|
122
|
+
console.log('valuesHexs: ', valuesHexs);
|
|
123
|
+
|
|
124
|
+
// 将处理后的参数对象添加到数组中
|
|
125
|
+
paramObjs.push({
|
|
126
|
+
address: paramInfo.paramNo,
|
|
127
|
+
length: paramInfo.paramLength / 2,
|
|
128
|
+
values: valuesHexs,
|
|
129
|
+
oldAddress: paramInfo.oldParamNo,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 一次性将所有参数对象数组传递给setParamCmd函数
|
|
134
|
+
await setParamCmd(chipType, deviceId, paramObjs);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error('error: 写系统参数报错 setSysParamCmd', error);
|
|
137
|
+
throw error
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// #region 发指令设置参数
|
|
142
|
+
/**
|
|
143
|
+
* 发指令设置参数
|
|
144
|
+
* @param {*} chipType 芯片类型
|
|
145
|
+
* @param {*} deviceId 设备deviceId
|
|
146
|
+
* @param {Array} paramInfoArray 参数信息数组 或单个对象
|
|
147
|
+
* @param {*} paramInfoArray[].address 新协议开始写入地址(十进制)如 31
|
|
148
|
+
* @param {*} paramInfoArray[].length 新协议写参数内容长度(写几个)(十进制)如 1
|
|
149
|
+
* @param {*} paramInfoArray[].values 新设置的参数值(十六进制数组)如 ['0x00','0x10']
|
|
150
|
+
* @param {*} paramInfoArray[].oldAddress 旧协议写入地址 默认一个(十六进制字符串)如 '0x1f'
|
|
151
|
+
* @returns {Promise<Array>} 包含所有参数设置结果的数组
|
|
152
|
+
*/
|
|
153
|
+
export const setParamCmd = (chipType, deviceId, paramInfoArray) => {
|
|
154
|
+
console.log("chipType: 芯片", chipType);
|
|
155
|
+
|
|
156
|
+
// 确保paramInfoArray是数组
|
|
157
|
+
const paramsToProcess = Array.isArray(paramInfoArray) ? paramInfoArray : [paramInfoArray];
|
|
158
|
+
|
|
159
|
+
return new Promise(async (resolve, reject) => {
|
|
160
|
+
try {
|
|
161
|
+
// 只进入工厂一次
|
|
162
|
+
await enterFactory(deviceId);
|
|
163
|
+
|
|
164
|
+
const results = [];
|
|
165
|
+
|
|
166
|
+
// 处理所有参数
|
|
167
|
+
for (let i = 0; i < paramsToProcess.length; i++) {
|
|
168
|
+
const paramInfo = paramsToProcess[i];
|
|
169
|
+
const { address, length, values, oldAddress } = paramInfo;
|
|
170
|
+
|
|
171
|
+
/** 地址转换为16进制 */
|
|
172
|
+
const hexAddress = decimalToTwoByteHexArray(address);
|
|
173
|
+
|
|
174
|
+
// 长度转换为16进制
|
|
175
|
+
const hexLength = decimalToHex(length);
|
|
176
|
+
|
|
177
|
+
// 根据芯片类型选择不同的设置函数
|
|
178
|
+
let hex;
|
|
179
|
+
if (chipType) {
|
|
180
|
+
hex = await setDDA5FAAsync(deviceId, hexAddress, hexLength, values);
|
|
181
|
+
} else {
|
|
182
|
+
hex = await setDDA5OldAsync(deviceId, oldAddress, values);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
console.log(`data: 写入第${i + 1}个参数结果`, hex);
|
|
186
|
+
|
|
187
|
+
if (!hex) {
|
|
188
|
+
throw new Error(`设置第${i + 1}个参数失败`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
results.push(hex);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 所有参数都执行完后才退出工厂
|
|
195
|
+
await existFactory(deviceId);
|
|
196
|
+
|
|
197
|
+
// 返回所有结果
|
|
198
|
+
resolve(results);
|
|
199
|
+
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error("设置参数出错: ", error);
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
// 发生错误时也要尝试退出工厂
|
|
205
|
+
await existFactory(deviceId);
|
|
206
|
+
} catch (exitError) {
|
|
207
|
+
console.error("退出工厂模式失败: ", exitError);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
reject(error);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// #region 写入标称容量
|
|
216
|
+
/**写入标称容量 同步写入满充及循环
|
|
217
|
+
* @param {*} chipType 芯片类型
|
|
218
|
+
* @param {*} deviceId 设备deviceId
|
|
219
|
+
* @param {*} paramInfo 参数信息
|
|
220
|
+
* @param {*} paramInfo.values 新设置的参数值(十进制)如 10000
|
|
221
|
+
* @param {*} paramInfo.address 新协议写入地址(十进制)如 1
|
|
222
|
+
* @param {*} paramInfo.oldAddress 旧协议写入地址 默认一个(十六进制字符串)如 '0x1f'
|
|
223
|
+
* @param {*} paramInfo.length 参数写入长度(十进制)如 1
|
|
224
|
+
* @returns
|
|
225
|
+
*/
|
|
226
|
+
export const setCapacityParamCmd = (chipType, deviceId, paramInfo) => {
|
|
227
|
+
console.log("chipType: 芯片", chipType);
|
|
228
|
+
|
|
229
|
+
return new Promise(async (resolve, reject) => {
|
|
230
|
+
const { values } = paramInfo;
|
|
231
|
+
// 循环 循环是标称的80%
|
|
232
|
+
const circular = {
|
|
233
|
+
address: 1,
|
|
234
|
+
oldAddress: "0x11",
|
|
235
|
+
values: values * 0.8,
|
|
236
|
+
length: 1,
|
|
237
|
+
};
|
|
238
|
+
// 满充 满充等于标称 旧协议没有满充
|
|
239
|
+
const full = {
|
|
240
|
+
address: 112,
|
|
241
|
+
oldAddress: "0x10",
|
|
242
|
+
values: values,
|
|
243
|
+
length: 1,
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const handleSet = async (setFunction, index) => {
|
|
247
|
+
try {
|
|
248
|
+
const hex = await setFunction();
|
|
249
|
+
console.log("data: 写入参数结果", hex);
|
|
250
|
+
if (!hex) {
|
|
251
|
+
reject("设置参数失败");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
return hex;
|
|
255
|
+
} catch (error) {
|
|
256
|
+
console.error("设置参数出错: ", error);
|
|
257
|
+
reject(error);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const handleParam = async (paramObj) => {
|
|
261
|
+
const { address, oldAddress, length } = paramObj;
|
|
262
|
+
const hexValues = decimalToTwoByteHexArray(Number(paramObj.values) * 100);
|
|
263
|
+
/** 地址转换为16进制 */
|
|
264
|
+
const hexAddress = decimalToTwoByteHexArray(address);
|
|
265
|
+
// 长度转换为16进制
|
|
266
|
+
const hexLength = decimalToHex(length);
|
|
267
|
+
if (chipType) {
|
|
268
|
+
return await handleSet(() => setDDA5FAAsync(deviceId, hexAddress, hexLength, hexValues), 7);
|
|
269
|
+
} else {
|
|
270
|
+
return await handleSet(() => setDDA5OldAsync(deviceId, oldAddress, hexValues), 4);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
try {
|
|
274
|
+
await enterFactory(deviceId);
|
|
275
|
+
const paramInfoHex = await handleParam(paramInfo);
|
|
276
|
+
const circularHex = await handleParam(circular);
|
|
277
|
+
if (chipType) {
|
|
278
|
+
const fullHex = await handleParam(full);
|
|
279
|
+
}
|
|
280
|
+
await existFactory(deviceId);
|
|
281
|
+
if (paramInfoHex) {
|
|
282
|
+
resolve(paramInfoHex);
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
reject(error);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
};
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// #region 校验----------------------------------------
|
|
2
|
+
/** 标准modbus_crc16
|
|
3
|
+
*
|
|
4
|
+
* @param {number[]} data 十六进制的数据
|
|
5
|
+
* @param {boolean} swapNeed 是否高低位交换, 默认高位在前-低位在后
|
|
6
|
+
* @returns 十进制校验位
|
|
7
|
+
*/
|
|
8
|
+
export const crc16modbus = (data, swapNeed = false) => {
|
|
9
|
+
let crc = 0xffff;
|
|
10
|
+
for (let i = 0; i < data.length; i++) {
|
|
11
|
+
crc ^= data[i];
|
|
12
|
+
for (let j = 0; j < 8; j++) {
|
|
13
|
+
if (crc & 0x0001) {
|
|
14
|
+
crc = (crc >> 1) ^ 0xa001;
|
|
15
|
+
} else {
|
|
16
|
+
crc = crc >> 1;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// swap crc
|
|
21
|
+
if (swapNeed) crc = ((crc & 0xff) << 8) | ((crc >> 8) & 0xff);
|
|
22
|
+
return crc & 0xffff;
|
|
23
|
+
}
|
|
24
|
+
/** 生成十六进制校验位(适用于3B3C、储能协议)
|
|
25
|
+
*
|
|
26
|
+
* @param {number[]} data 十六进制的数据
|
|
27
|
+
* @param {boolean} swapNeed 是否高低位交换, 默认高位在前-低位在后
|
|
28
|
+
* @returns string[] 十六进制校验位
|
|
29
|
+
*/
|
|
30
|
+
export const generateCrc16modbusCheck = (data, swapNeed = false) => {
|
|
31
|
+
const hex = crc16modbus(data, swapNeed);
|
|
32
|
+
const hexStr = decimalToHex(hex, 4);
|
|
33
|
+
return [`0x${hexStr.slice(0, 2)}`, `0x${hexStr.slice(2, 4)}`];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** crc校验和算法
|
|
37
|
+
* 数据段内容+长度字节+状态码字节的校验和然后在取反加1
|
|
38
|
+
* @param {number[]} data 十六进制的数据
|
|
39
|
+
* @param {*} offset 求和后的偏移量
|
|
40
|
+
* @returns 十进制校验位
|
|
41
|
+
*/
|
|
42
|
+
export const crcCheckSum = (data, offset = 0x01) => {
|
|
43
|
+
console.log('data: --------', data);
|
|
44
|
+
let hex = 0x00;
|
|
45
|
+
for (let i = 0; i < data.length; i++) {
|
|
46
|
+
hex = hex + (data[i] & 0x0ff);
|
|
47
|
+
}
|
|
48
|
+
hex = hex ^ 0xffff;
|
|
49
|
+
hex = hex + offset;
|
|
50
|
+
console.log('hex: -------------', hex);
|
|
51
|
+
return hex;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 生成校验和校验位(适用于通用板协议)
|
|
55
|
+
*
|
|
56
|
+
* @param {number[]} data 十六进制的数据 命令码、长度、内容
|
|
57
|
+
* @returns string[] 十六进制校验位
|
|
58
|
+
*/
|
|
59
|
+
export const generateCrcCheckSum = (data) => {
|
|
60
|
+
const hex = crcCheckSum(data);
|
|
61
|
+
const hexStr = decimalToHex(hex, 4);
|
|
62
|
+
return [`0x${hexStr.slice(0, 2)}`, `0x${hexStr.slice(2, 4)}`];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** FFAA协议校验算法
|
|
66
|
+
*
|
|
67
|
+
* @param {number[]} data 十六进制的数据
|
|
68
|
+
* @returns 十进制校验位
|
|
69
|
+
*/
|
|
70
|
+
export const checkSum = (data = []) => {
|
|
71
|
+
const sum = data.map((o) => parseInt(o, 16)).reduce((a, c) => a + c, 0);
|
|
72
|
+
let sumHex = '0x' + decimalToHex(sum, 2);
|
|
73
|
+
// 取低8位
|
|
74
|
+
sumHex = sumHex & 0xff;
|
|
75
|
+
return sumHex;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**生成校验和校验位(适用于FFAA指令协议)
|
|
79
|
+
*
|
|
80
|
+
* @param {number[]} data 十六进制的数据
|
|
81
|
+
* @returns string[] 十六进制校验位
|
|
82
|
+
*/
|
|
83
|
+
export const generateCheckSum = (data) => {
|
|
84
|
+
const hex = checkSum(data);
|
|
85
|
+
const hexStr = decimalToHex(hex, 2);
|
|
86
|
+
return [`0x${hexStr}`];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// #region Buffer 数据转换-------------------------------------------
|
|
90
|
+
|
|
91
|
+
/** ArrayBuffer转换为十进制数字数组
|
|
92
|
+
*
|
|
93
|
+
* @param {ArrayBuffer} buffer
|
|
94
|
+
* @returns number[] 十进制数字数组
|
|
95
|
+
*/
|
|
96
|
+
export const ab2decimalArr = (buffer) => {
|
|
97
|
+
return Array.prototype.map.call(new Uint8Array(buffer), (bit) => bit);
|
|
98
|
+
}
|
|
99
|
+
/** ArrayBuffer转换为十六进制字符串
|
|
100
|
+
*
|
|
101
|
+
* @param {ArrayBuffer} buffer
|
|
102
|
+
* @returns string 十六进制字符串
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
export const ab2hex = (buffer) => {
|
|
106
|
+
const hexArr = Array.prototype.map.call(
|
|
107
|
+
new Uint8Array(buffer),
|
|
108
|
+
function (bit) {
|
|
109
|
+
return ('00' + bit.toString(16)).slice(-2)
|
|
110
|
+
}
|
|
111
|
+
)
|
|
112
|
+
return hexArr.join('')
|
|
113
|
+
}
|
|
114
|
+
/** ArrayBuffer转16进制数组
|
|
115
|
+
*
|
|
116
|
+
* @param {ArrayBuffer} arrayBuffer
|
|
117
|
+
* @param {boolean} withPrefix
|
|
118
|
+
* @returns string[] 十六进制数组
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
export const arrayBufferToHexArray = (arrayBuffer, withPrefix = true) => {
|
|
122
|
+
return Array.prototype.map.call(new Uint8Array(arrayBuffer), (x) => {
|
|
123
|
+
let hex = ('00' + x.toString(16)).slice(-2);
|
|
124
|
+
return withPrefix ? `0x${hex}` : hex;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/** Base64转16进制数组
|
|
128
|
+
*
|
|
129
|
+
* @param {string} base64
|
|
130
|
+
* @param {boolean} withPrefix
|
|
131
|
+
* @returns string[] 十六进制数组
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
export const base64ToHexArray = (base64, withPrefix = true) => {
|
|
135
|
+
const arrayBuffer = uni.base64ToArrayBuffer(base64);
|
|
136
|
+
return arrayBufferToHexArray(arrayBuffer, withPrefix);
|
|
137
|
+
}
|
|
138
|
+
// 将HexArray转成Module指定格式的ArrayBuffer
|
|
139
|
+
export const hexArrayToModuleArrayBuffer = (fields, hexArray, withCheck = true, withPrefix = true) => {
|
|
140
|
+
const fieldsLength = fields.length; //e.g [0xff, 0xaa, 0x80]: 包头(2Byte) 指令码(1Byte)
|
|
141
|
+
let offset = fieldsLength + 1; // +1 for data length
|
|
142
|
+
if (withCheck) offset++; // check
|
|
143
|
+
|
|
144
|
+
let bufferLength = hexArray.length + offset;
|
|
145
|
+
const buffer = new ArrayBuffer(bufferLength);
|
|
146
|
+
const dataView = new DataView(buffer);
|
|
147
|
+
const lengthCode = decimalToHex(hexArray.length);
|
|
148
|
+
|
|
149
|
+
// 包头&指令码
|
|
150
|
+
fields.forEach((field, i) => {
|
|
151
|
+
dataView.setUint8(i, field);
|
|
152
|
+
});
|
|
153
|
+
// 数据位长度
|
|
154
|
+
dataView.setUint8(fieldsLength, '0x' + lengthCode);
|
|
155
|
+
// 数据位
|
|
156
|
+
let indexNum = fieldsLength + 1;
|
|
157
|
+
hexArray.forEach((el, index) => {
|
|
158
|
+
dataView.setUint8(indexNum, withPrefix ? '0x' + el : el);
|
|
159
|
+
indexNum += 1;
|
|
160
|
+
});
|
|
161
|
+
// 校验位
|
|
162
|
+
if (withCheck) {
|
|
163
|
+
const code = fields[2].toString(16); //
|
|
164
|
+
let check = generateCheckSum([code, lengthCode, ...hexArray]);
|
|
165
|
+
dataView.setUint8(bufferLength - 1, check[0]);
|
|
166
|
+
}
|
|
167
|
+
return buffer;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// #region 进制转换------------------------------------------
|
|
171
|
+
/**
|
|
172
|
+
* 获取参数的基础值
|
|
173
|
+
* 将参数十六进制内容数组转换为十进制数值
|
|
174
|
+
* @param {*} arr
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
export const getParamBaseValue = (arr) => {
|
|
178
|
+
// 将有效的字节转换为十六进制字符串
|
|
179
|
+
const hexString = arr.map(el => decimalToHex(el)).join('');
|
|
180
|
+
let value = parseInt(hexString, 16); // 转为十进制
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
/** 十进制转十六进制
|
|
184
|
+
*
|
|
185
|
+
* @param {number} decimal 十进制数字
|
|
186
|
+
* @param {*} padding 补零长度
|
|
187
|
+
* @returns 十六进制字符串
|
|
188
|
+
*/
|
|
189
|
+
export const decimalToHex = (decimal, padding = 2) => {
|
|
190
|
+
const hex = Number(decimal).toString(16).padStart(padding, '0');
|
|
191
|
+
return hex;
|
|
192
|
+
}
|
|
193
|
+
/** 十进制转十六进制
|
|
194
|
+
* *
|
|
195
|
+
* @param {number} decimal 十进制数字
|
|
196
|
+
* @param {*} padding 补零长度
|
|
197
|
+
* @returns 0x + 十六进制字符串
|
|
198
|
+
*/
|
|
199
|
+
export const decimalToHex0x = (decimal, padding = 2) => {
|
|
200
|
+
const hex = Number(decimal).toString(16).padStart(padding, "0");
|
|
201
|
+
return "0x" + hex;
|
|
202
|
+
}
|
|
203
|
+
/** 十六进制转十进制 */
|
|
204
|
+
export const hexToDecimal = (hex) => {
|
|
205
|
+
const decimal = parseInt(hex, 16)
|
|
206
|
+
return decimal;
|
|
207
|
+
}
|
|
208
|
+
/** 十六进制数据的数组转为ArrayBuffer
|
|
209
|
+
*
|
|
210
|
+
* @param {(string|number)[]} hexArr 十六进制数据的数组
|
|
211
|
+
* @returns ArrayBuffer
|
|
212
|
+
*/
|
|
213
|
+
export const hexArr2ab = (hexArr) => {
|
|
214
|
+
const buffer = new ArrayBuffer(hexArr.length);
|
|
215
|
+
const dataView = new DataView(buffer);
|
|
216
|
+
hexArr.forEach((hex, i) => dataView.setUint8(i, hex));
|
|
217
|
+
return buffer;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 十六进制转十六进制字符串
|
|
221
|
+
*
|
|
222
|
+
* @param {string|number} hex 十六进制数据
|
|
223
|
+
* @returns string 十六进制数据对应的字符串
|
|
224
|
+
*/
|
|
225
|
+
export const hex2string = (hex) => {
|
|
226
|
+
return ('00' + hex.toString(16)).slice(-2).toUpperCase();
|
|
227
|
+
}
|
|
228
|
+
/** 字符串转十六进制
|
|
229
|
+
*
|
|
230
|
+
* @param {string} str
|
|
231
|
+
* @returns 十六进制数组
|
|
232
|
+
*/
|
|
233
|
+
export const string2hexArr = (str) => {
|
|
234
|
+
let hexArray = [];
|
|
235
|
+
for (let i = 0; i < str.length; i++) {
|
|
236
|
+
const charCode = str.charCodeAt(i).toString(16).padStart(2, '0');
|
|
237
|
+
hexArray.push(charCode);
|
|
238
|
+
}
|
|
239
|
+
return hexArray;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** 十六进制转ASSIC
|
|
243
|
+
*
|
|
244
|
+
* @param {(string|number)[]} hexArr
|
|
245
|
+
* @returns string
|
|
246
|
+
*/
|
|
247
|
+
export const hexArr2Assic = (hexArr = []) => {
|
|
248
|
+
return String.fromCharCode(...hexArr).replace(/\u0000/g, '');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 十六进制字符串转ASSIC
|
|
252
|
+
*
|
|
253
|
+
* @param {string} hexString
|
|
254
|
+
* @returns string
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
export const hexStrAscii = (hexString) => {
|
|
258
|
+
let asciiString = '';
|
|
259
|
+
for (let i = 0; i < hexString.length; i += 2) {
|
|
260
|
+
const hexPair = hexString.substr(i, 2);
|
|
261
|
+
const decimalValue = parseInt(hexPair, 16);
|
|
262
|
+
asciiString += String.fromCharCode(decimalValue);
|
|
263
|
+
}
|
|
264
|
+
const result = asciiString.replace(/\u0000/g, '');
|
|
265
|
+
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
/** 十六进制转ASSIC
|
|
269
|
+
*
|
|
270
|
+
* @param {string} hexArr
|
|
271
|
+
* @returns string
|
|
272
|
+
*/
|
|
273
|
+
export const hex2Ascii = (value = []) => {
|
|
274
|
+
const hexString = value.join('');
|
|
275
|
+
return hexStrAscii(hexString)
|
|
276
|
+
}
|
|
277
|
+
/** 十六进制数据的数组转为字符串
|
|
278
|
+
*
|
|
279
|
+
* @param {(string|number)[]} hexArr 十六进制数据的数组
|
|
280
|
+
* @returns string 十六进制字符串 e.g:3B3C000000000000000001160104000201000200030A444830342E312E342E370400775A0D
|
|
281
|
+
*/
|
|
282
|
+
export const hexArr2string = (hexArr = [], connector = '') => {
|
|
283
|
+
return hexArr.map((o) => hex2string(o)).join(connector);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 十六进制转二进制
|
|
287
|
+
*
|
|
288
|
+
* @param {*} hex
|
|
289
|
+
* @returns
|
|
290
|
+
*/
|
|
291
|
+
export const hex2Binary = (hex, len = 0) => {
|
|
292
|
+
return parseInt(hex, 16).toString(2).padStart(len, '0');
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 计算Bit位区间值(包头包尾)
|
|
296
|
+
* 例如:计算Bit7-Bit11的值
|
|
297
|
+
* @param number 十进制数值
|
|
298
|
+
* @param start 开始位(从0开始)
|
|
299
|
+
* @param end 结束位
|
|
300
|
+
* @examples extractBits(a, 7, 11)
|
|
301
|
+
*/
|
|
302
|
+
export const extractBits = (number, start, end) => {
|
|
303
|
+
// 创建掩码,使用(1<<n)-1来创建n位的掩码
|
|
304
|
+
let mask = (1 << (end - start + 1)) - 1;
|
|
305
|
+
// 右移去掉低位,并应用掩码以提取位
|
|
306
|
+
return (number >> start) & mask;
|
|
307
|
+
}
|
|
308
|
+
/**计时器 等待n毫秒
|
|
309
|
+
* @param {number} n 毫秒
|
|
310
|
+
*/
|
|
311
|
+
export const sleep = (n = 500) => {
|
|
312
|
+
return new Promise((r) => setTimeout(() => r(true), n));
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 十进制数转换为两个字节的十六进制数组
|
|
316
|
+
* @param {number} decimal 十进制数,范围 0 - 65535
|
|
317
|
+
* @returns {Array} 包含两个字节十六进制字符串的数组
|
|
318
|
+
*/
|
|
319
|
+
export const decimalToTwoByteHexArray = (decimal) => {
|
|
320
|
+
// 添加类型和整数检查
|
|
321
|
+
if (typeof decimal !== 'number' || !Number.isInteger(decimal)) {
|
|
322
|
+
throw new Error('输入必须为整数');
|
|
323
|
+
}
|
|
324
|
+
if (decimal < 0 || decimal > 65535) {
|
|
325
|
+
throw new Error('输入的十进制数超出 16 位无符号整数范围(0 - 65535)');
|
|
326
|
+
}
|
|
327
|
+
let f = `00${(decimal >> 8).toString(16)}`.slice(-2);
|
|
328
|
+
let e = `00${(decimal & 0xff).toString(16)}`.slice(-2);
|
|
329
|
+
return [`0x${f}`, `0x${e}`];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 将十六进制字符串转换为两个十六进制的数组
|
|
334
|
+
* @param {string} str 要转换的字符串 '1881'
|
|
335
|
+
* @returns {string[]} 转换后的十六进制数组 ['0x18', '0x81']
|
|
336
|
+
*/
|
|
337
|
+
export const stringToTwoHexArray = (str) => {
|
|
338
|
+
// 确保字符串长度为 4
|
|
339
|
+
const paddedStr = str.padStart(4, '0');
|
|
340
|
+
return [
|
|
341
|
+
`0x${paddedStr.slice(0, 2)}`,
|
|
342
|
+
`0x${paddedStr.slice(2, 4)}`
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* 将十六进制字符串转换为二进制字符串
|
|
347
|
+
* @param {string} hexString 要转换的十六进制字符串
|
|
348
|
+
* @returns {string} 转换后的二进制字符串
|
|
349
|
+
*/
|
|
350
|
+
export const hexStringToBinary = (hexString) => {
|
|
351
|
+
const decimalNumber = parseInt(hexString, 16);
|
|
352
|
+
const binaryString = decimalNumber.toString(2);
|
|
353
|
+
return binaryString;
|
|
354
|
+
}
|
|
355
|
+
|