parseDeviceMessage.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. #include "parseDeviceMessage.h"
  2. #include "cJson.h"
  3. #include "systick.h"
  4. #include "usart.h"
  5. #include "string.h"
  6. #include <math.h>
  7. #include "mqttRecv.h"
  8. #include "log.h"
  9. #include "ec800m.h"
  10. #include "main.h"
  11. #define MQTT_SUB_CMD_WRITE 0
  12. #define MQTT_SUB_CMD_READ 1
  13. ring_buffer mqttRecv;
  14. void processHTTPjson(cJSON *json);
  15. char *processStringData(cJSON *data_obj, const char *key);
  16. int processIntData(cJSON *data_obj, const char *key);
  17. void processHttp(const uint8_t *data);
  18. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  19. //static void extract_data_from_buffer232(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr);
  20. static uint16_t checksum(const char *str, uint16_t len);
  21. // json解析字符串
  22. char *processStringData(cJSON *data_obj, const char *key)
  23. {
  24. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  25. if (cJSON_IsString(json_data) && (json_data->valuestring != NULL))
  26. {
  27. return json_data->valuestring;
  28. }
  29. else
  30. {
  31. return NULL;
  32. }
  33. }
  34. // json解析int
  35. int processIntData(cJSON *data_obj, const char *key)
  36. {
  37. cJSON *json_data = cJSON_GetObjectItemCaseSensitive(data_obj, key);
  38. // if (cJSON_IsNumber(json_data) && (json_data->valueint != NULL)) 解决寄存器地址为0问题
  39. if (cJSON_IsNumber(json_data))
  40. {
  41. return json_data->valueint;
  42. }
  43. else
  44. {
  45. // gateway.data_valid_flag = 0; // 如果任意数据为NULL则此次数据为无效
  46. return -1;
  47. }
  48. }
  49. /*
  50. * 函数名:void WaitForUpData(void)
  51. * 输入参数:无
  52. * 输出参数:无
  53. * 返回值:无
  54. * 函数作用:等待更新本地flash信息
  55. */
  56. bool WaitForUpData(char *dmaBuffer)
  57. {
  58. if (UART0_RX_STAT > 0)
  59. {
  60. UART0_RX_STAT = 0;
  61. uint32_t len;
  62. uint16_t checkCode;
  63. extract_data_from_buffer(dmaBuffer, &len, &checkCode);
  64. uint16_t jsonCheck = checksum(dmaBuffer, len);
  65. if (checkCode == jsonCheck)
  66. {
  67. processHttp(dmaBuffer);
  68. }
  69. }
  70. }
  71. /*
  72. * 函数名:static void extract_data_from_buffer(const char* buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  73. * 输入参数:buffer字符串
  74. * 输出参数:json有效字符串长度len_ptr,checkCode_ptr校验码指针
  75. * 返回值:无
  76. * 函数作用:eg. QFDWL: 621,3e23 从json信息最后端取出这段json的有效长度和校验码
  77. */
  78. static void extract_data_from_buffer(const char *buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  79. {
  80. char *start = strstr(buffer, "+QFDWL:");
  81. if (start != NULL)
  82. {
  83. start += 8; // 跳过"+QFDWL:"
  84. uint32_t len = 0;
  85. sscanf(start, "%u,", &len); // 读取长度
  86. start = strchr(start, ',') + 1; // 跳过逗号
  87. uint16_t checkCode = 0;
  88. sscanf(start, "%hx", &checkCode); // 读取16进制数据
  89. // 将提取的数据存入形参
  90. *len_ptr = len;
  91. *checkCode_ptr = checkCode;
  92. }
  93. }
  94. /*
  95. * 函数名:void processHTTPjson(cJSON *json)
  96. * 输入参数:json从http get获取的json字符串
  97. * 输出参数:无
  98. * 返回值:无
  99. * 函数作用:解析json字符串将设备配置信息写到flash中 strcpy NULL指针导致的内存泄露问题
  100. */
  101. void processHTTPjson(cJSON *json)
  102. {
  103. CONFIG_PARAMS gateway;
  104. gateway.data_valid_flag = 0xF1; // 将数据标识位置为有效
  105. cJSON *readArrayItem = cJSON_GetObjectItem(json, "sensorData");
  106. gateway.device_read_data_num = cJSON_GetArraySize(readArrayItem);
  107. cJSON *writeArrayItem = cJSON_GetObjectItem(json, "commandData");
  108. gateway.device_write_data_num = cJSON_GetArraySize(writeArrayItem);
  109. strcpy(gateway.host, processStringData(json, "host"));
  110. gateway.port = processIntData(json, "port");
  111. //strcpy(gateway.deviceId, processStringData(json, "deviceId")); 这条数据将不在进行解析由芯片id决定
  112. strcpy(gateway.messageTopic, processStringData(json, "messageTopic"));
  113. strcpy(gateway.commandTopic, processStringData(json, "commandTopic"));
  114. gateway.dataSource = processIntData(json, "dataSource");
  115. gateway.baudrate = processIntData(json, "baudrate");
  116. gateway.dataBits = processIntData(json, "dataBit");
  117. gateway.stopBit = processIntData(json, "stopBit");
  118. gateway.checkBit = processIntData(json, "checkBit");
  119. gateway.flowControl = processIntData(json, "flowControl");
  120. gateway.dataType645 = processIntData(json, "version645");
  121. gateway.inboundTime= processIntData(json, "inboundTime");
  122. gateway.pollTime= processIntData(json, "pollTime");
  123. if (cJSON_IsArray(readArrayItem))
  124. {
  125. for (int i = 0; i < gateway.device_read_data_num; i++)
  126. {
  127. cJSON *read_data_obj = cJSON_GetArrayItem(readArrayItem, i);
  128. if (cJSON_IsObject(read_data_obj))
  129. {
  130. if (gateway.dataSource == MODBUS)
  131. {
  132. strcpy(gateway.device_read_data[i].deviceId, processStringData(read_data_obj, "deviceId"));
  133. strcpy(gateway.device_read_data[i].keyword, processStringData(read_data_obj, "identifier"));
  134. gateway.device_read_data[i].mdbSlave = processIntData(read_data_obj, "slaveAddress");
  135. gateway.device_read_data[i].mdbRegister = processIntData(read_data_obj, "registerAddress");
  136. gateway.device_read_data[i].mdbFunctionCode = processIntData(read_data_obj, "rFunctionCode");
  137. gateway.device_read_data[i].registerLength = processIntData(read_data_obj, "registerByteNum");
  138. gateway.device_read_data[i].decimalPoint = processIntData(read_data_obj, "precise");
  139. gateway.device_read_data[i].bigLittleFormat=processIntData(read_data_obj,"bigLittleFormat");
  140. }
  141. else if (gateway.dataSource == DLT645)
  142. {
  143. strcpy(gateway.device_read_data[i].deviceId, processStringData(read_data_obj, "deviceId"));
  144. strcpy(gateway.device_read_data[i].keyword, processStringData(read_data_obj, "identifier"));
  145. gateway.device_read_data[i].dataType645 = processIntData(read_data_obj, "identifier645");
  146. char idstring[13];
  147. strcpy(idstring, processStringData(read_data_obj, "deviceID645"));
  148. // 将deviceID645 由string变成0x
  149. for (int j = 0; j < 6; j++)
  150. {
  151. {
  152. uint8_t byte;
  153. sscanf((const char *)&idstring[j * 2], "%2hhx", &byte);
  154. gateway.device_read_data[i].deviceID645[j] = byte;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. //写数据预存数组
  161. for (int i = 0; i < gateway.device_write_data_num; i++)
  162. {
  163. cJSON *write_data_obj = cJSON_GetArrayItem(writeArrayItem, i);
  164. if (gateway.dataSource == MODBUS)
  165. {
  166. strcpy(gateway.device_write_data[i].deviceId, processStringData(write_data_obj, "deviceId"));
  167. strcpy(gateway.device_write_data[i].keyword, processStringData(write_data_obj, "identifier"));
  168. gateway.device_write_data[i].mdbSlave = processIntData(write_data_obj, "slaveAddress");
  169. gateway.device_write_data[i].mdbRegister = processIntData(write_data_obj, "registerAddress");
  170. gateway.device_write_data[i].mdbFunctionCode = processIntData(write_data_obj, "wFunctionCode");
  171. gateway.device_write_data[i].registerLength = processIntData(write_data_obj, "registerByteNum");
  172. gateway.device_write_data[i].decimalPoint = processIntData(write_data_obj, "precise");
  173. gateway.device_write_data[i].bigLittleFormat=processIntData(write_data_obj,"bigLittleFormat");
  174. }
  175. }
  176. }
  177. save_config_params(&gateway);
  178. }
  179. //提取int数据
  180. int parseIntField(const char* data, const char* field) {
  181. char* ptr = strstr(data, field) + strlen(field);
  182. int value;
  183. value = strtol(ptr, &ptr, 10);
  184. return value;
  185. }
  186. //提取string字符串
  187. void parseStringField(const char* data, const char* field, char* value) {
  188. char* ptr = strstr(data, field) + strlen(field) ;
  189. sscanf(ptr, "%[^\"],", value);
  190. }
  191. void parseCommandData();
  192. void processHttp(const uint8_t *data)
  193. {
  194. CONFIG_PARAMS *gateway;
  195. gateway=get_config_params();
  196. gateway->data_valid_flag = 0xF1;
  197. char* ptr = (char*)data;
  198. gateway->dataSource=parseIntField(ptr,"\"dataSource\":");
  199. gateway->baudrate=parseIntField(ptr,"\"baudrate\":");
  200. gateway->checkBit=parseIntField(ptr,"\"checkBit\":");
  201. gateway->stopBit=parseIntField(ptr,"\"stopBit\":");
  202. parseStringField(ptr,"\"commandTopic\":\"",(char *)&gateway->commandTopic);
  203. gateway->dataBits=parseIntField(ptr,"\"dataBit\":");
  204. // parseStringField(ptr,"\"deviceId\":\"",(char *)&gateway->deviceId);
  205. parseStringField(ptr,"\"host\":\"",(char *)&gateway->host);
  206. gateway->inboundTime=parseIntField(ptr,"\"inboundTime\":");
  207. parseStringField(ptr,"\"messageTopic\":\"",(char *)&gateway->messageTopic);
  208. gateway->pollTime=parseIntField(ptr,"\"pollTime\":");
  209. gateway->port=parseIntField(ptr,"\"port\":");
  210. if(gateway->dataSource==2) //为MODBUS
  211. {
  212. ptr = strstr(ptr, "\"commandData\":[");
  213. int i=0;
  214. while(1)
  215. {
  216. gateway->device_write_data[i].bigLittleFormat=parseIntField(ptr,"\"bigLittleFormat\":");
  217. parseStringField(ptr,"\"deviceId\":\"",(char *)&gateway->device_write_data[i].deviceId);
  218. parseStringField(ptr,"\"identifier\":\"",(char *)&gateway->device_write_data[i].keyword);
  219. gateway->device_write_data[i].registerLength =parseIntField(ptr,"\"registerByteNum\":\"");
  220. gateway->device_write_data[i].mdbRegister=parseIntField(ptr,"\"registerAddress\":");
  221. gateway->device_write_data[i].mdbSlave =parseIntField(ptr,"\"slaveAddress\":");
  222. gateway->device_write_data[i].mdbFunctionCode=parseIntField(ptr,"\"wFunctionCode\":");
  223. ptr=strstr(ptr,"}"); //移动位置到结束
  224. ptr++;
  225. if(ptr[0]==']')
  226. {
  227. i++;
  228. gateway->device_write_data_num=i;
  229. ptr = (char*)data;
  230. break;
  231. }
  232. ptr = strchr(ptr, ',');
  233. if (!ptr)
  234. {
  235. i++;
  236. gateway->device_write_data_num=i;
  237. ptr = (char*)data;
  238. break;
  239. }
  240. i++;
  241. }
  242. ptr=strstr(ptr,"\"sensorData\":[");
  243. i=0;
  244. while(*ptr != ']')
  245. {
  246. gateway->device_read_data[i].bigLittleFormat=parseIntField(ptr,"\"bigLittleFormat\":");
  247. parseStringField(ptr,"\"deviceId\":\"",(char *)&gateway->device_read_data[i].deviceId);
  248. parseStringField(ptr,"\"identifier\":\"",(char *)&gateway->device_read_data[i].keyword);
  249. gateway->device_read_data[i].mdbRegister=parseIntField(ptr,"\"registerAddress\":");
  250. gateway->device_read_data[i].mdbSlave =parseIntField(ptr,"\"slaveAddress\":");
  251. gateway->device_read_data[i].registerLength =parseIntField(ptr,"\"registerByteNum\":");
  252. gateway->device_read_data[i].mdbFunctionCode=parseIntField(ptr,"\"rFunctionCode\":");
  253. gateway->device_read_data[i].decimalPoint=parseIntField(ptr,"\"precise\":");
  254. ptr=strstr(ptr,"}"); //移动位置到结束
  255. ptr++;
  256. if(ptr[0]==']')
  257. {
  258. i++;
  259. gateway->device_read_data_num=i;
  260. ptr = (char*)data;
  261. break;
  262. }
  263. ptr = strchr(ptr, ',');
  264. if (!ptr)
  265. {
  266. i++;
  267. gateway->device_read_data_num=i;
  268. ptr = (char*)data;
  269. break;
  270. }
  271. i++;
  272. }
  273. }
  274. else if(gateway->dataSource==1) //为dlt645协议
  275. {
  276. gateway->dataType645=parseIntField(ptr,"\"version645\":");
  277. ptr=strstr(ptr,"\"sensorData\":[");
  278. int i=0;
  279. while(*ptr != ']')
  280. {
  281. char *string=malloc(13);
  282. parseStringField(ptr,"\"deviceID645\":\"",string);
  283. for (int j = 0; j < 6; j++)
  284. {
  285. {
  286. uint8_t byte;
  287. sscanf((const char *)&string[j * 2], "%2hhx", &byte);
  288. gateway->device_read_data[i].deviceID645[j] = byte;
  289. }
  290. }
  291. free(string);
  292. parseStringField(ptr, "\"deviceId\":\"",(char *)&gateway->device_read_data[i].deviceId);
  293. parseStringField(ptr, "\"identifier\":\"",(char *)&gateway->device_read_data[i].keyword);
  294. gateway->device_read_data[i].dataType645=parseIntField(ptr,"\"identifier645\":");
  295. ptr=strstr(ptr,"}");
  296. ptr++;
  297. if(ptr[0]==']')
  298. {
  299. i++;
  300. gateway->device_read_data_num=i;
  301. ptr = (char*)data;
  302. break;
  303. }
  304. ptr = strchr(ptr, ',');
  305. if (!ptr)
  306. {
  307. i++;
  308. gateway->device_read_data_num=i;
  309. ptr = (char*)data;
  310. break;
  311. }
  312. i++;
  313. }
  314. }
  315. ptr=NULL;
  316. }
  317. /*
  318. * 函数名: void mmodbusRead(CONFIG_PARAMS * gateway)
  319. * 输入参数:gateway网关存储的配置参数
  320. * 输出参数:网关结构体指针
  321. * 返回值:无
  322. * 函数作用:根据存储的设备信息发送modbus信息返回值存储到gateway->device_read_data[i].data中
  323. * modbusRead读数据只考虑读16位寄存器和32位寄存器这两种情况,但对其返回值进行处理后返回值一律为float型4个字节存储在内部
  324. */
  325. void modbusRead(CONFIG_PARAMS *gateway)
  326. {
  327. for (int i = 0; i < gateway->device_read_data_num; i++)
  328. {
  329. delay_1ms(100);
  330. uint16_t data[gateway->device_read_data[i].registerLength/2];
  331. mmodbus_set16bitOrder(gateway->device_read_data[i].bigLittleFormat);
  332. if (gateway->device_read_data[i].mdbFunctionCode == 0x03)
  333. {
  334. bool success = mmodbus_readHoldingRegisters16i(gateway->device_read_data[i].mdbSlave,
  335. gateway->device_read_data[i].mdbRegister,
  336. gateway->device_read_data[i].registerLength/2,
  337. data);
  338. if (success)
  339. {
  340. if (gateway->device_read_data[i].registerLength == 4)
  341. {
  342. uint32_t value = (uint32_t)data[0] << 16 | data[1];
  343. gateway->device_read_data[i].rxLen = 4;
  344. //增加小数点为0则gateway->device_read_data[i].data内存储为整形数据
  345. if(gateway->device_read_data[i].decimalPoint!=0) //小数点不为0的清空
  346. {
  347. float convertedValue = (float)value / pow(10, gateway->device_read_data[i].decimalPoint);
  348. memcpy(gateway->device_read_data[i].data, &convertedValue, 4);
  349. }
  350. else if(gateway->device_read_data[i].decimalPoint==0) //小数点为0
  351. {
  352. gateway->device_read_data[i].data[0]=value;
  353. gateway->device_read_data[i].data[1]=value<<8;
  354. gateway->device_read_data[i].data[2]=value<<16;
  355. gateway->device_read_data[i].data[3]=value<<24;
  356. }
  357. }
  358. else if (gateway->device_read_data[i].registerLength == 2)
  359. {
  360. uint32_t value = data[0];
  361. gateway->device_read_data[i].rxLen = 4;
  362. if(gateway->device_read_data[i].decimalPoint!=0) //小数点不为0的清空
  363. {
  364. float convertedValue = (float)value / pow(10, gateway->device_read_data[i].decimalPoint);
  365. memcpy(gateway->device_read_data[i].data, &convertedValue, 4);
  366. }
  367. else if(gateway->device_read_data[i].decimalPoint==0) //小数点为0
  368. {
  369. gateway->device_read_data[i].data[0]=value;
  370. gateway->device_read_data[i].data[1]=value<<8;
  371. gateway->device_read_data[i].data[2]=value<<16;
  372. gateway->device_read_data[i].data[3]=value<<24;
  373. }
  374. }
  375. }
  376. else
  377. {
  378. gateway->device_read_data[i].rxLen = 0;
  379. }
  380. }
  381. else if (gateway->device_read_data[i].mdbFunctionCode == 0x01)
  382. {
  383. uint8_t data[2];
  384. mmodbus_set16bitOrder(gateway->device_read_data[i].bigLittleFormat);
  385. bool success = mmodbus_readCoils(gateway->device_read_data[i].mdbSlave,
  386. gateway->device_read_data[i].mdbRegister,
  387. gateway->device_read_data[i].registerLength,
  388. data);
  389. if (success)
  390. {
  391. uint32_t value = (uint32_t)data[0] << 8 | data[1];
  392. gateway->device_read_data[i].rxLen = 4;
  393. }
  394. else
  395. {
  396. gateway->device_read_data[i].rxLen = 0;
  397. }
  398. }
  399. task_fwdgt_reload();
  400. }
  401. }
  402. /*
  403. * 函数名:void parseMQTTData(CONFIG_PARAMS *gateway)
  404. * 输入参数:device存储的各类产品信息
  405. * 输出参数:无
  406. * 返回值:无
  407. * 函数作用:从环形缓冲区中一个个把json信息读出,与存储的gateway内的子设备信息匹配后将对应指令匹配执行并返回相应的值
  408. * 查询逻辑现在有两套数组read数组和write数组,当接收中断产生时先判断是进行读还是写、之后在查询此网关支持的协议MODBUS还是
  409. * 645,后根据协议去不同数组查询不同的本地存储信息、组装发送给子设备,然后根据查询的值进行采集上传数据
  410. */
  411. void parseMQTTData(CONFIG_PARAMS *gateway)
  412. {
  413. while (UART0_RX_MQTT_SUB_STAT == 1)
  414. {
  415. UART0_RX_MQTT_SUB_STAT = 0;
  416. char payload_out[200];
  417. char mqtt_publish[200];
  418. char json[128];
  419. while (MQTT_BUFFER_READ(json))
  420. {
  421. LogPrint(LOG_INFO, __func__, __LINE__, "Received JSON: %s", json);
  422. cJSON *mqtt_sub_json = cJSON_Parse(json);
  423. if (mqtt_sub_json != NULL)
  424. {
  425. uint8_t action1[1];
  426. uint8_t action;
  427. strcpy(action1, processStringData(mqtt_sub_json, "action"));
  428. // 解决字符串为
  429. if (action1[0] == '0')
  430. {
  431. action = 0;
  432. }
  433. else if (action1[0] == '1')
  434. {
  435. action = 1;
  436. }
  437. uint8_t identifier[20];
  438. strcpy(identifier, processStringData(mqtt_sub_json, "identifier"));
  439. uint8_t deviceId[20];
  440. strcpy(deviceId, processStringData(mqtt_sub_json, "deviceId"));
  441. uint16_t value = processIntData(mqtt_sub_json, "parameter");
  442. uint8_t messageId[10];
  443. strcpy(messageId, processStringData(mqtt_sub_json, "messageId"));
  444. // 判断其指令过来是读还是写
  445. if (action == MQTT_SUB_CMD_WRITE)
  446. {
  447. for (int i = 0; i <= gateway->device_write_data_num; i++)
  448. {
  449. if (strcmp(gateway->device_write_data[i].deviceId, deviceId) == 0)
  450. {
  451. if (strcmp(gateway->device_write_data[i].keyword, identifier) == 0) // 寻找到对应存储的地址
  452. {
  453. if (gateway->dataSource == MODBUS)
  454. {
  455. if (gateway->device_write_data[i].mdbFunctionCode == 0x05)
  456. {
  457. mmodbus_set16bitOrder(gateway->device_write_data[i].bigLittleFormat);
  458. bool success = mmodbus_writeCoil(gateway->device_write_data[i].mdbSlave,
  459. gateway->device_write_data[i].mdbRegister,
  460. (uint8_t)value);
  461. // 成功
  462. if (success)
  463. {
  464. sprintf((char *)payload_out,
  465. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  466. action1,
  467. gateway->device_write_data[i].keyword,
  468. gateway->device_write_data[i].deviceId, messageId, success);
  469. }
  470. else
  471. sprintf((char *)payload_out,
  472. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  473. action1,
  474. gateway->device_write_data[i].keyword,
  475. gateway->device_write_data[i].deviceId, messageId, success);
  476. }
  477. else if (gateway->device_write_data[i].mdbFunctionCode == 0x06)
  478. {
  479. mmodbus_set16bitOrder(gateway->device_write_data[i].bigLittleFormat);
  480. bool success = mmodbus_writeHoldingRegister16i(gateway->device_write_data[i].mdbSlave,
  481. gateway->device_write_data[i].mdbRegister,
  482. (uint16_t)value);
  483. if (success)
  484. {
  485. sprintf((char *)payload_out,
  486. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  487. action1,
  488. gateway->device_write_data[i].keyword,
  489. gateway->device_write_data[i].deviceId, messageId, success);
  490. }
  491. else
  492. sprintf((char *)payload_out,
  493. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  494. action1,
  495. gateway->device_write_data[i].keyword,
  496. gateway->device_write_data[i].deviceId, messageId, success);
  497. }
  498. else
  499. {
  500. // 考虑后续兼容其他自定义协议
  501. }
  502. }
  503. // DLT645暂时不支持写入操作
  504. else if (gateway->dataSource == DLT645)
  505. {
  506. LogPrint(LOG_ERROR, __func__, __LINE__, "DLT645 does not support write instructions");
  507. }
  508. }
  509. }
  510. else
  511. LogPrint(LOG_ERROR, __func__, __LINE__, "No child device information matched to settings");
  512. }
  513. }
  514. // 指令是来读某属性值的
  515. else if (action == MQTT_SUB_CMD_READ)
  516. {
  517. for (int i = 0; i <= gateway->device_read_data_num; i++)
  518. {
  519. if (strcmp(gateway->device_read_data[i].deviceId, deviceId) == 0)
  520. {
  521. if (strcmp(gateway->device_read_data[i].keyword, identifier) == 0) // 寻找到对应存储的地址
  522. {
  523. if (gateway->dataSource == MODBUS)
  524. {
  525. if (gateway->device_read_data[i].mdbFunctionCode == 0x01)
  526. {
  527. uint8_t data[2];
  528. mmodbus_set16bitOrder(gateway->device_read_data[i].bigLittleFormat);
  529. bool success = mmodbus_readCoils(gateway->device_read_data[i].mdbSlave,
  530. gateway->device_read_data[i].mdbRegister,
  531. gateway->device_read_data[i].registerLength,
  532. data);
  533. uint8_t value1;
  534. if (success)
  535. {
  536. uint16_t value1 = (uint16_t)data[0] << 8 | data[1];
  537. if (value1 == 0xFF00)
  538. {
  539. value1 = 1;
  540. }
  541. else if (value1 == 0x0000)
  542. {
  543. value1 = 0;
  544. }
  545. // 读线圈成功的json
  546. sprintf((char *)payload_out,
  547. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"parameter\":%d}",
  548. action1,
  549. gateway->device_read_data[i].keyword,
  550. gateway->device_read_data[i].deviceId, messageId, success, value);
  551. }
  552. // 读线圈失败的json
  553. else
  554. sprintf((char *)payload_out,
  555. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  556. action1,
  557. gateway->device_read_data[i].keyword,
  558. gateway->device_read_data[i].deviceId, messageId, success);
  559. }
  560. else if (gateway->device_read_data[i].mdbFunctionCode == 0x03)
  561. {
  562. uint16_t data[gateway->device_read_data[i].registerLength/2];
  563. mmodbus_set16bitOrder(gateway->device_read_data[i].bigLittleFormat);
  564. bool success = mmodbus_readHoldingRegisters16i(gateway->device_read_data[i].mdbSlave,
  565. gateway->device_read_data[i].mdbRegister,
  566. gateway->device_read_data[i].registerLength/2,
  567. data);
  568. if (success)
  569. {
  570. float convertedValue;
  571. if (gateway->device_read_data[i].registerLength == 2)
  572. {
  573. uint16_t value = data[0];
  574. convertedValue = (float)value / pow(10, gateway->device_read_data[i].decimalPoint);
  575. }
  576. else if (gateway->device_read_data[i].registerLength == 4)
  577. {
  578. uint32_t value = (uint32_t)data[0] << 16 | data[1];
  579. convertedValue = (float)value / pow(10, gateway->device_read_data[i].decimalPoint);
  580. }
  581. // 读寄存器值成功的json
  582. sprintf((char *)payload_out,
  583. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":%.2f}",
  584. action1,
  585. gateway->device_read_data[i].keyword,
  586. gateway->device_read_data[i].deviceId, messageId, success, convertedValue);
  587. }
  588. else
  589. {
  590. // 读寄存器失败的json
  591. sprintf((char *)payload_out,
  592. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  593. action1,
  594. gateway->device_read_data[i].keyword,
  595. gateway->device_read_data[i].deviceId, messageId, success);
  596. }
  597. }
  598. }
  599. else if (gateway->dataSource == DLT645)
  600. {
  601. float value;
  602. uint8_t minute, hour, day, month, year;
  603. uint8_t read_buf[10];
  604. gateway->device_read_data[i].rxLen = 0;
  605. memset(read_buf, 0, 10);
  606. memset(gateway->device_read_data[i].data, 0, 10); // 清空data内容
  607. dlt645_set_addr(&dlt645, gateway->device_read_data[i].deviceID645);
  608. int8_t rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_2007);
  609. if (rs != -1)
  610. {
  611. if (rs <= 4)
  612. {
  613. memcpy(&value, read_buf, 4);
  614. sprintf((char *)payload_out,
  615. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":%.2f}",
  616. action1,
  617. gateway->device_read_data[i].keyword,
  618. gateway->device_read_data[i].deviceId, messageId, 1, value);
  619. }
  620. else if (rs == 5)
  621. {
  622. memcpy(&value, gateway->device_read_data[i].data, 4);
  623. year = gateway->device_read_data[i].data[4];
  624. month = gateway->device_read_data[i].data[3];
  625. day = gateway->device_read_data[i].data[2];
  626. hour = gateway->device_read_data[i].data[1];
  627. minute = gateway->device_read_data[i].data[0];
  628. sprintf((char *)payload_out,
  629. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X\"}",
  630. action1,
  631. gateway->device_read_data[i].keyword,
  632. gateway->device_read_data[i].deviceId, messageId, 1, year, month, day, hour, minute);
  633. }
  634. else if (rs > 5)
  635. {
  636. memcpy(&value, gateway->device_read_data[i].data, 4);
  637. year = gateway->device_read_data[i].data[8];
  638. month = gateway->device_read_data[i].data[7];
  639. day = gateway->device_read_data[i].data[6];
  640. hour = gateway->device_read_data[i].data[5];
  641. minute = gateway->device_read_data[i].data[4];
  642. sprintf((char *)payload_out,
  643. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d,\"value\":\"%02X%02X%02X%02X%02X%.2f\"}",
  644. action1,
  645. gateway->device_read_data[i].keyword,
  646. gateway->device_read_data[i].deviceId, messageId, 1, year, month, day, hour, minute, value);
  647. }
  648. }
  649. else
  650. {
  651. // dlt645读取失败的json组成
  652. sprintf((char *)payload_out,
  653. "{\"action\":\"%s\",\"identifier\":\"%s\",\"deviceId\":\"%s\",\"messageId\":\"%s\",\"state\":%d}",
  654. action1,
  655. gateway->device_read_data[i].keyword,
  656. gateway->device_read_data[i].deviceId, messageId, 0);
  657. }
  658. }
  659. }
  660. }
  661. }
  662. }
  663. // 上传对应的响应信息
  664. sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  665. EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  666. WaitResponse("<", 1000);
  667. EC800MSendCmd(payload_out, strlen(payload_out));
  668. WaitResponse(RSP_OK, 1000);
  669. }
  670. cJSON_Delete(mqtt_sub_json);
  671. }
  672. }
  673. }
  674. /*
  675. * 函数名:void dlt645_read(CONFIG_PARAMS *gateway)
  676. * 输入参数:设备地址*address 数据标识 readCode
  677. * 输出参数:无
  678. * 返回值:无
  679. * 函数作用:读取645协议内部数据
  680. 备注:dlt645协议为网上下载,返回值为数据长度
  681. 现在要加读日期信息YYMMDDhhmm
  682. 对源代码改动不多的方法是通过返回的数据长度去做判断
  683. 当数据长度为2、3、4时此时 read_buf数据包内仅含采集到的浮点数数据、 把float值4个字节直接拷贝出即可
  684. 当数据长度为5时 read_buf数据包为日期数据 其中5个字节分别为YYMMDDhhmm直接拷贝出来
  685. 当数据长度大于5时 read_buf内既包含浮点数又包含日期时间 其中前4个字节为浮点数数据后五个字节为日期信息
  686. */
  687. void dlt645_read(CONFIG_PARAMS *gateway)
  688. {
  689. uint8_t read_buf[10];
  690. for (int i = 0; i < gateway->device_read_data_num; i++)
  691. {
  692. gateway->device_read_data[i].rxLen = 0;
  693. memset(read_buf, 0, 10);
  694. memset(gateway->device_read_data[i].data, 0, 10);
  695. dlt645_set_addr(&dlt645, gateway->device_read_data[i].deviceID645);
  696. int8_t rs;
  697. if(gateway->dataType645==DLT645_2007){
  698. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_2007);
  699. }
  700. else {
  701. rs = dlt645_read_data(&dlt645, gateway->device_read_data[i].dataType645, read_buf, DLT645_1997);
  702. }
  703. if (rs != -1)
  704. {
  705. if (rs <= 4)
  706. {
  707. memcpy(gateway->device_read_data[i].data, read_buf, 4);
  708. gateway->device_read_data[i].rxLen = rs;
  709. }
  710. else if (rs == 5)
  711. {
  712. memcpy(gateway->device_read_data[i].data, read_buf, 5);
  713. gateway->device_read_data[i].rxLen = rs;
  714. }
  715. else if (rs > 5)
  716. {
  717. memcpy(gateway->device_read_data[i].data, read_buf, 9);
  718. gateway->device_read_data[i].rxLen = rs;
  719. }
  720. }
  721. else
  722. {
  723. gateway->device_read_data[i].rxLen = 0;
  724. }
  725. task_fwdgt_reload();
  726. }
  727. }
  728. // 模块下载download校验值
  729. static uint16_t checksum(const char *str, uint16_t len)
  730. {
  731. uint16_t sum = 0;
  732. uint8_t odd = 0;
  733. // 如果字符串长度为奇数,则将最后一个字符设置为高8位,低8位设置为0
  734. if (len % 2 == 1)
  735. {
  736. odd = 1;
  737. len--;
  738. }
  739. // 将每两个字符作为一个16位的数值进行异或操作
  740. for (uint16_t i = 0; i < len; i += 2)
  741. {
  742. sum ^= ((uint16_t)str[i] << 8) | (uint16_t)str[i + 1];
  743. }
  744. // 如果字符串长度为奇数,则还需要将最后一个字符与0xFF00异或
  745. if (odd)
  746. {
  747. sum ^= (uint16_t)str[len] << 8;
  748. }
  749. // 返回校验和
  750. return sum;
  751. }