123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605 |
- //----------------------------说明
- // 程序使用hal库,数据传输使用的串口,串口设置为发送+dma,接收+dma+空闲中断的模式
- #include "EC800.h"
- //-----------------------------变量定义
- //---------
- ec800Date s_ec800Date = {
- .ip = "39.98.37.180",
- .port = 32449,
- .clientid = "NULL",
- .username = "hechun",
- .password = "admin123",
- // .username = "SZC30010-AE01",
- // .password = "qazSZC30010-AE01",
- };
- messageDate s_messageDate = {0};
- typedef struct{
- uint8_t maxSpeed; // 服务器下发的允许最大车速
- uint16_t year; // 上传时间 年
- uint8_t month; // 上传时间 月
- uint8_t day; // 上传时间 日
- uint8_t hour; // 上传时间 时
- uint8_t minute; // 上传时间 分
- uint8_t sec; // 上传时间 秒
- }recDate;
- recDate s_recDate = {0};
- nmea_msg s_nmea_msg = {0};
- nmea_utc_time s_nmea_utc_time = {0};
- // AT指令响应超时时间定义
- #define REC_TIMEOUT (100) // 1ms
- /**
- * @brief 计算字符串的长度
- * @param str: 所需计算字符串的指针
- * @note
- * @retval 无
- */
- int EC800_calculateStringLength(const char* str) {
- int length = 0;
- while (str[length] != '\0') {
- length++;
- }
- return length;
- }
- /**
- * @brief 发送指令函数
- * @param command: 指令
- * @param enterNum: 进入函数次数,只有第一次的时候清除buff
- * @note
- * @retval 无
- */
- void EC800M_SendCommand(const char* command)
- {
- uint32_t stringLen = 0;
- stringLen = EC800_calculateStringLength(command);
- // 使用DMA
- // //等待发送状态OK
- // while(HAL_DMA_GetState(&hdma_usart3_tx) == HAL_DMA_STATE_BUSY) osDelay(1);
- // //发送数据
- // HAL_UART_Transmit_DMA(&huart3, (uint8_t*)command, stringLen);
- // 不使用发送dma
- HAL_UART_Transmit(&huart4, (uint8_t*)command, stringLen, 1000);
-
- }
- /**
- * @brief 接收指令回复函数
- * @param haystack: 接收的字符串数据
- * @param needle: 正确回复的数据
- * @note
- * @retval 无
- */
- char* EC800M_RecRespond(char *haystack, const char *needle ){
- char* p = NULL;
-
- HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
- // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
- while(g_usart4_rx_sta == 0) osDelay(1);
- // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
- g_usart4_rx_sta= 0;
- p = strstr(haystack, needle);
- memset(haystack, 0, USART4_REC_LEN); // 清除数据buff
- HAL_UART_DMAStop(&huart4);
- return p;
- }
- // 自定义的搜索函数
- void* search_sequence(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) {
- const unsigned char* h = (const unsigned char*)haystack;
- const unsigned char* n = (const unsigned char*)needle;
- // 如果needle为空或haystack长度小于needle长度,则直接返回NULL
- if (needle_len == 0 || haystack_len < needle_len) {
- return NULL;
- }
- for (size_t i = 0; i <= haystack_len - needle_len; ++i) {
- size_t j;
- for (j = 0; j < needle_len; ++j) {
- if (h[i + j] != n[j]) {
- break; // 如果当前字符不匹配,则跳出内层循环
- }
- }
- if (j == needle_len) {
- return (void*)(h + i); // 找到匹配的序列,返回其在haystack中的位置
- }
- }
- // 如果遍历了整个haystack都没有找到匹配的序列,则返回NULL
- return NULL;
- }
- // 接收并比较响应字符串
- uint8_t Accept_and_Compare_Str(const char* needle){
- uint8_t temp = 0;
- static uint16_t timeOutCnt = 0; // 超时计数
- HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
- // __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
- while(!temp) {
- osDelay(1);
-
- if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, needle, strlen(needle))){
- temp = 1;
- }
- if(search_sequence(g_usart4_rx_buf, USART4_REC_LEN, "ERROR", strlen("ERROR"))){
- temp = 2;
- }
- if(timeOutCnt < REC_TIMEOUT){
- timeOutCnt++;
- }else{
- timeOutCnt = 0;
- temp = 3;
- }
- }
- g_usart4_rx_sta = 0;
- HAL_UART_DMAStop(&huart4);
- // __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE); // 关闭空闲中断
- return temp;
- }
- /**
- * @brief 接收使能函数
- * @note
- * @retval 无
- */
- void EC800_recEnable(void){
- HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
- __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
- while(g_usart4_rx_sta == 0) osDelay(1);
- }
- /**
- * @brief 提取检查信号响应字符串字段中的信号量并进行信号判断
- * @param data: 响应的信号量指针
- * @note
- * @retval 无
- */
- static uint8_t EC800_extractSignal(char *data){
- char *token;
- int field_1;
- // 使用strtok函数提取字段
- token = strtok(data, ":,");
- while (token != NULL) {
- field_1 = atoi(token); // 转换为整数类型
- token = strtok(NULL, ":,");
- memset(data, 0, USART4_REC_LEN); // 清除数据buff
- g_usart3_rx_sta = 0; // 清除接收状态
-
- if (field_1 < 31) {
- return 1;
- } else {
- return 0;
- }
- }
- return 0;
- }
- /**
- * @brief 获取IMSI号
- * @param 无
- * @note
- * @retval 1: imsi没有获取到 0;imsi获取到
- */
- uint8_t EC800_getIMSI(void){
- char* found = NULL;
- char number[16]; // 15 digits + 1 for null terminator
- int numberIndex = 0;
- int numberFound = 0;
- EC800M_SendCommand(AT_CIMI);
- EC800_recEnable();
-
- found = strstr(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("IMSI is get\r\n");
- }else{
- printf("IMSI is not get\r\n");
- return 1;
- }
- for (int i = 0; i < strlen(g_usart4_rx_buf); i++) {
- if (isdigit(g_usart4_rx_buf[i])) {
- number[numberIndex] = g_usart4_rx_buf[i];
- numberIndex++;
- if (numberIndex >= 15) {
- numberFound = 1;
- break;
- }
- }
- }
- number[numberIndex] = '\0';
- if (numberFound) {
- memcpy(s_messageDate.imsi, number, strlen(number));
- printf("IMSI is: %s\n", number);
- } else {
- printf("Error: Unable to extract the number\n");
- }
- return 0;
- }
- /**
- * @brief EC800M确认网络并链接MQTT服务器
- * @param NONE
- * @note
- * @retval 无
- */
- uint8_t linkStep = 0;
- void EC800M_link(void){
- uint8_t temp = 0;
- char command[100] = {0};
- char errrCnt = 0; // 错误计数
- char* found = NULL;
- switch(linkStep){
- case 0: // 基础配置
- EC800M_SendCommand(AT_CMD_TEST);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Module status normal\r\n");
- }else{
- errrCnt++;
- printf("Module status abnormal\r\n");
- }
-
- if(errrCnt == 0){
- linkStep = 1;
- }else{
- errrCnt = 0;
- }
- break;
- case 1: // 获取ISMI号
- errrCnt = EC800_getIMSI();
-
- if(errrCnt == 0){
- linkStep = 2;
- }else{
- errrCnt = 0;
- }
- break;
- case 2:
- EC800M_SendCommand(AT_CMD_ATE0);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Cancel module echo\r\n");
- }else{
- errrCnt++;
- printf("Failed to cancel module echo\r\n");
- }
-
- if(errrCnt == 0){
- linkStep = 3;
- }else{
- errrCnt = 0;
- }
- break;
- case 3:
- EC800M_SendCommand(AT_CMD_CPIN);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_CPIN_READY);
- if (found != NULL) {
- printf("SIM card normal\r\n");
- }else{
- errrCnt++;
- printf("SIM card abnormal\r\n");
- }
-
- if(errrCnt == 0){
- linkStep = 4;
- }else{
- errrCnt = 0;
- }
- break;
- case 4:
- EC800M_SendCommand(AT_CMD_CSQ);
- EC800_recEnable();
-
- temp = EC800_extractSignal(g_usart4_rx_buf);
- if (temp) {
- printf("signal normal\r\n");
- }else{
- errrCnt++;
- printf("signal abnormal\r\n");
- }
- if(errrCnt == 0){
- linkStep = 5;
- }else{
- errrCnt = 0;
- }
- break;
- case 5:
- EC800M_SendCommand(AT_CMD_CREG);
-
- EC800_recEnable();
-
- // 使用字符串处理函数判断是否为正常状态
- if (strstr(g_usart4_rx_buf, "1") != NULL || strstr(g_usart4_rx_buf, "5") != NULL) {
- printf("The module successfully registered on the GSM network\r\n");
- } else {
- errrCnt++;
- printf("The module failed to register on the GSM network\r\n");
- }
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
- g_usart4_rx_sta = 0; // 清除接收状态
-
- if(errrCnt == 0){
- linkStep = 6;
- }else{
- errrCnt = 0;
- }
- break;
- case 6:
- EC800M_SendCommand(AT_CMD_QIDEACT);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
-
- if (found != NULL) {
- printf("Successfully disabled mobile scene\r\n");
- }else{
- errrCnt++;
- printf("Failed to disable mobile scene\r\n");
- }
-
- if(errrCnt == 0){
- linkStep = 7;
- }else{
- errrCnt = 0;
- }
- break;
- case 7:
- EC800M_SendCommand(AT_CMD_QIACT);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
-
- if (found != NULL) {
- printf("Successfully enabled mobile scene\r\n");
- }else{
- errrCnt++;
- printf("Failed to enable mobile scene\r\n");
- }
- if(errrCnt == 0){
- linkStep = 8;
- }else{
- errrCnt = 0;
- }
- break;
- case 8: // 连接mqtt服务器
- sprintf(command, "AT+QMTCFG=\"qmtping\",0,%d\r\n", 30);
- EC800M_SendCommand(command);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
-
- if (found != NULL) {
- printf("MQTT heartbeat set successfully\r\n");
- }else{
- errrCnt++;
- printf("Failed to set MQTT heartbeat\r\n");
- }
- if(errrCnt == 0){
- linkStep = 9;
- }else{
- errrCnt = 0;
- }
- break;
- case 9:
- EC800M_SendCommand(AT_CMD_QMTCFG_SET_DATA_MODE);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
-
- if (found != NULL) {
- printf("Data receiving mode set successfully.\r\n");
- }else{
- errrCnt++;
- printf("Failed to set data receiving mode\r\n");
- }
- if(errrCnt == 0){
- linkStep = 10;
- }else{
- errrCnt = 0;
- }
- break;
- case 10:
- sprintf(command,"AT+QMTOPEN=0,\"%s\",%d\r\n", s_ec800Date.ip, s_ec800Date.port);
- EC800M_SendCommand(command);
- // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
- // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTOPEN);
- //
- // if (found != NULL) {
- // printf("Successfully opened a network for the module\r\n");
- // }else{
- // errrCnt++;
- // printf("Failed to open a network for the module\r\n");
- // }
- temp = Accept_and_Compare_Str(AT_RESP_QMTOPEN);
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
- if (temp == 1) {
- printf("Successfully opened a network for the module\r\n");
- }else{
- errrCnt++;
- printf("Failed to open a network for the module\r\n");
- }
- if(errrCnt == 0){
- linkStep = 11;
- }else{
- errrCnt = 0;
- }
- break;
- case 11:
- sprintf(command,"AT+QMTCONN=0,%s,%s,%s\r\n",s_ec800Date.clientid, s_ec800Date.username, s_ec800Date.password);
- EC800M_SendCommand(command);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_QMTCONN);
- if (found != NULL) {
- printf("Successfully connected to the MQTT server\r\n");
- }else{
- errrCnt++;
- printf("Failed to connect to the MQTT server\r\n");
- }
-
- if(errrCnt == 0){
- linkStep = 12;
- }else{
- errrCnt = 0;
- }
- break;
- default: break;
- }
- }
- /**
- * @brief EC800M初始化GNSS
- * @param NONE
- * @note
- * @retval 无
- */
- uint8_t gnssStep = 0;
- void EC800_gnss_init(void){
- char errrCnt = 0; // 错误计数
- char* found = NULL;
-
- switch(gnssStep){
- case 6:
- EC800M_SendCommand(AT_QGPS_0);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Turn off the GNSS module\r\n");
- }else{
- errrCnt++;
- printf("Failed to turn off the GNSS module\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 7;
- }else{
- gnssStep = 7;
- }
- break;
- case 7:
- EC800M_SendCommand(AT_QGPS_1);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Turn on the GNSS module\r\n");
- }else{
- errrCnt++;
- printf("Failed to turn on the GNSS module\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 8;
- }else{
- errrCnt = 0;
- }
- break;
- case 0:
- EC800M_SendCommand(AT_QGPSCFG_out_port);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Outputting NMEA Sentences via Serial Debugging\r\n");
- }else{
- errrCnt++;
- printf("The NMEA sentence output is set incorrectly\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 1;
- }else{
- errrCnt = 0;
- }
- break;
- case 1:
- EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
- }else{
- errrCnt++;
- printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 2;
- }else{
- errrCnt = 0;
- }
- break;
- case 2:
- EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Configuring the output type of NMEA sentences to all formats\r\n");
- }else{
- errrCnt++;
- printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 3;
- }else{
- errrCnt = 0;
- }
- break;
- case 3:
- EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
- }else{
- errrCnt++;
- printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 4;
- }else{
- errrCnt = 0;
- }
- break;
- case 4:
- EC800M_SendCommand(AT_QGPSCFG_autogps);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Disable GNSS auto-start\r\n");
- }else{
- errrCnt++;
- printf("Failed to disable GNSS auto-start\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 5;
- }else{
- errrCnt = 0;
- }
- break;
- case 5:
- EC800M_SendCommand(AT_QGPSCFG_apflash);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Disable AP-Flash quick start function\r\n");
- }else{
- errrCnt++;
- printf("Failed to disable AP-Flash quick start function\r\n");
- }
-
- if(errrCnt == 0){
- gnssStep = 6;
- }else{
- errrCnt = 0;
- }
- break;
- default:
- break;
- }
- }
- //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
- void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
- {
- int year,month,day,hour;
- int lastday = 0; //last day of this month 本月天数
- int lastlastday = 0; //last day of last month 上个月天数
- year = utc_time->year; //utc time
- month = utc_time->month;
- day = utc_time->date;
- hour = utc_time->hour + timezone;
-
- //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
- if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
- {
- lastday = 31;//本月天数
- lastlastday = 30;//这里应该补上上个月的天数
-
- if(month == 3)
- {
- if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
- lastlastday = 29;
- else
- lastlastday = 28;
- }
-
- if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
- lastlastday = 31;
- }
- else if(month == 4 || month == 6 || month == 9 || month == 11)
- {
- lastday = 30;
- lastlastday = 31;
- }
- else
- {
- lastlastday = 31;
-
- if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
- lastday = 29;
- else
- lastday = 28;
- }
- if(hour >= 24)// if >24, day+1
- {
- hour -= 24;
- day += 1;
- if(day > lastday)// next month, day-lastday of this month
- {
- day -= lastday;
- month += 1;
- if(month > 12)// next year, month-12
- {
- month -= 12;
- year += 1;
- }
- }
- }
-
- if(hour < 0)// if <0, day-1
- {
- hour += 24;
- day -= 1;
- if(day < 1)// month-1, day=last day of last month
- {
- day = lastlastday;
- month -= 1;
- if(month < 1)// last year, month=12
- {
- month = 12;
- year -= 1;
- }
- }
- }
-
- // transfer value to local_time
- local_time->year = year;
- local_time->month = month;
- local_time->date = day;
- local_time->hour = hour;
- local_time->min = utc_time->min;
- local_time->sec = utc_time->sec;
- }
- /**
- * @brief 常用时间格式转时间戳
- * @param NONE
- * @note
- * @retval 无
- */
- uint32_t EC800_mktime (unsigned int year, unsigned int mon,
- unsigned int day, unsigned int hour,
- unsigned int min, unsigned int sec)
- {
- if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
- mon += 12; /**//* Puts Feb last since it has leap day */
- year -= 1;
- }
-
- return (((
- (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
- year*365 - 719499
- )*24 + hour /**//* now have hours */
- )*60 + min /**//* now have minutes */
- )*60 + sec; /**//* finally seconds */
- }
- /**
- * @brief 读取GNSS的数据
- * @param NONE
- * @note
- * @retval 无
- */
- void EC800_readGnssDate(void){
- char* found = NULL;
-
- EC800M_SendCommand(AT_QGPSGNMEA_RMC);
- EC800_recEnable();
- found = strstr(g_usart4_rx_buf, AT_RESP_OK);
- if (found != NULL) {
- printf("Location information received successfully\r\n");
- }else{
- printf("Location information reception failed\r\n");
- return ;
- }
- printf("Location information:%s\r\n", g_usart4_rx_buf);
- NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart4_rx_buf);
- utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
- printf("timer-%d/%d/%d %d:%d:%d\r\n",s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec);
- s_messageDate.Timestamp = EC800_mktime(s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec);
-
- g_usart3_rx_sta= 0;
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
- printf("latitude:%d%d--longitude:%d%d\r\n", s_nmea_msg.latitude, s_nmea_msg.nshemi, s_nmea_msg.longitude, s_nmea_msg.ewhemi);
- /* 尝试获取互斥量,等待无限长时间 */
- if(osMutexAcquire(s_messageDate_locationHandle, osWaitForever) == osOK)
- {
- /* 安全地访问s_rs485RecDate结构体 */
- s_messageDate.latitude = s_nmea_msg.latitude;
- s_messageDate.nshemi = s_nmea_msg.nshemi;
- s_messageDate.longitude = s_nmea_msg.longitude;
- s_messageDate.ewhemi = s_nmea_msg.ewhemi;
-
- /* 访问完成,释放互斥量 */
- osMutexRelease(s_messageDate_locationHandle);
- }
-
- }
- /**
- * @brief EC800M订阅主题
- * @param topic1: 订阅的主题名称1字符串形式
- * @param topic2: 订阅的主题名称2字符串形式
- * @note
- * @retval 无
- */
- uint8_t EC800_subscribeToTopic(const char* topic1){
- char command[100] = {0};
- uint8_t recTemp = 0;
-
- sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
- EC800M_SendCommand(command);
- recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
- if (recTemp == 1) {
- printf("Subscription to the topic successful\r\n");
- return 1;
- }else if (recTemp == 2){
- printf("Subscription to the topic failed\r\n");
- return 0;
- }
- return 0;
- }
- /**
- * @brief EC800M发布消息
- * @param topic: 消息发布的主题 字符串形式
- * @param message: 发布的消息 字符串形式
- * @param len: 发布消息的字节长度
- * @note
- * @retval 无
- */
- uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
- char command[100] = {0};
- char* found = NULL;
- char* responseCmd = ">";
- static uint8_t publishStep = 0;
- uint8_t recBack = 0;
- switch(publishStep){
- case 0:
- sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
- EC800M_SendCommand(command);
-
- found = EC800M_RecRespond(g_usart4_rx_buf, responseCmd);
- if (found != NULL) {
- found = NULL; // 将found指针指向NULL处
- printf("The topic has been linked\r\n");
- publishStep = 1;
- }else{
- printf("The topic link failed\r\n");
- publishStep = 0;
- }
- // break;
- case 1:
- EC800M_SendCommand(message);
- // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
- // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
- // if (found != NULL) {
- // printf("The message has been successfully posted\r\n");
- // publishStep = 0;
- // }else{
- // printf("Message publication failed\r\n");
- // publishStep = 0;
- // }
- recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
- if (recBack == 1) {
- publishStep = 0;
- printf("The message has been successfully posted\r\n");
- }else{
- printf("Message publication failed\r\n");
- publishStep = 0;
- }
-
- break;
- default:
- break;
- }
- return recBack;
- }
- /**
- * @brief 登录 topic
- * @param NONE
- * @note topic 101
- * @retval 无
- */
- uint8_t Login_Topic(void){
- cJSON *root = NULL;
- char *jsonString = NULL;
- uint8_t temp = 0;
- char command[50] = {0};
- // 定义对象 { }
- root = cJSON_CreateObject();
- // 插入元素,对应 键值对
- cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
- cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
- cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
- cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
-
- sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
- cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
-
- cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
- cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
- cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
- // 将 JSON 对象转换为字符串
- jsonString = cJSON_Print(root);
- cJSON_Delete(root);
- temp = EC800_publishMessage("toServer/mqtt/101", jsonString, strlen(jsonString));
- free(jsonString);
- return temp;
- }
- /**
- * @brief 下位机请求围栏数据
- * @param NONE
- * @note topic 604
- * @retval 无
- */
- uint8_t requesting_Fence_Data(void){
- cJSON *root = NULL;
- char *jsonString = NULL;
- uint8_t temp = 0;
- char command[50] = {0};
- // 定义对象 { }
- root = cJSON_CreateObject();
- // 插入元素,对应 键值对
- cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
- cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
- cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
- cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
- sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
- cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
-
- // cJSON_AddItemToObject(root, "vin", cJSON_CreateString((char *)s_messageDate.VIN));
- cJSON_AddItemToObject(root, "vin", cJSON_CreateString("5Xt2Aq8Jb9L"));
- cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
- cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
- cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
- // 将 JSON 对象转换为字符串
- jsonString = cJSON_Print(root);
- cJSON_Delete(root);
- temp = EC800_publishMessage("toServer/mqtt/604", jsonString, strlen(jsonString));
- free(jsonString);
- return temp;
- }
- /**
- * @brief EC800M与服务器进行时间对时
- * @note
- * @retval 无
- */
- uint8_t EC800_time_calibration(void){
- cJSON *root = NULL;
- char *jsonString = NULL;
- uint8_t temp = 0;
- char command[50] = {0};
- // 定义对象 { }
- root = cJSON_CreateObject();
- // 插入元素,对应 键值对
- cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
- cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
- cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
- cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
-
- sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
- cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
-
- cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
- cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
- cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
- // 将 JSON 对象转换为字符串
- jsonString = cJSON_Print(root);
- cJSON_Delete(root);
- temp = EC800_publishMessage("toServer/mqtt/105", jsonString, strlen(jsonString));
- free(jsonString);
- return temp;
- }
- /**
- * @brief 将服务器响应的字符串中的年月日进行分离
- * @note
- * @retval 无
- */
- void parseServerTime(char* str){
- char year[5];
- char month[3];
- char day[3];
- char hour[3];
- char minute[3];
- char second[3];
- strncpy(year, str, 4);
- year[4] = '\0';
- strncpy(month, str + 4, 2);
- month[2] = '\0';
- strncpy(day, str + 6, 2);
- day[2] = '\0';
- strncpy(hour, str + 8, 2);
- hour[2] = '\0';
- strncpy(minute, str + 10, 2);
- minute[2] = '\0';
- strncpy(second, str + 12, 2);
- second[2] = '\0';
- s_recDate.year = atoi(year);
- s_recDate.month = atoi(month);
- s_recDate.day = atoi(day);
- s_recDate.hour = atoi(hour);
- s_recDate.minute = atoi(minute);
- s_recDate.sec = atoi(second);
- s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour,s_recDate.minute,s_recDate.sec);
- printf("Year (int): %d\n", s_recDate.year);
- printf("Month (int): %d\n", s_recDate.month);
- printf("Day (int): %d\n", s_recDate.day);
- printf("Hour (int): %d\n", s_recDate.hour);
- printf("Minute (int): %d\n", s_recDate.minute);
- printf("Second (int): %d\n", s_recDate.sec);
- }
- /**
- * @brief 解析对时数据帧的响应
- * @note
- * @retval 无
- */
- void EC800_parseRespondTime(cJSON *root, cJSON *item){
- uint8_t temp = 0;
- // 获取"resultCode"字段的值
- item = cJSON_GetObjectItem(root, "resultCode");
- if (item == NULL) {
- printf("Field \"resultCode\" not found\n");
- }else{
- // 打印"resultCode"字段的值
- printf("resultCode: %d\n", item->valueint);
- if((item->valueint) == 1){
- printf("The upload of synchronized data was successful.\n");
- temp = 1; // 上传成功
- }else{
- printf("The upload of synchronized data has failed.\n");
- }
- }
- if(temp == 1){
- item = cJSON_GetObjectItem(root, "serverTime");
- if (item == NULL) {
- printf("Field \"serverTime\" not found\n");
- }else{
- // 打印"serverTime"字段的值
- printf("serverTime: %s\n", item->valuestring);
- parseServerTime(item->valuestring);
- }
- }
- }
- /**
- * @brief EC800M上传实时数据
- * @note
- * @retval 无
- */
- uint8_t EC800_uploadRealDate(void){
- cJSON *root = NULL;
- char *jsonString = NULL;
- cJSON *rt000 = NULL;
- cJSON *rt000Object1 = NULL;
- uint8_t temp = 0;
- char command[50] = {0};
- // 定义对象 { }
- root = cJSON_CreateObject();
- //
- // 插入元素,对应 键值对
- cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
- cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
- cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
- cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
- sprintf(command, "%x%x%x", s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
- cJSON_AddItemToObject(root, "devId", cJSON_CreateString(command));
-
- cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
- cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
- cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
- // 定义 { } 对象
- rt000Object1 = cJSON_CreateObject();
- cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
- cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber(s_messageDate.demandVol));
- cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber(s_messageDate.demandCur));
- cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString((char *)s_messageDate.VIN));
- cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(s_messageDate.lat_long_data));
- cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
- cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
- cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime));
- cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage));
- cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(s_messageDate.accTotalDrivTime_h_min));
- cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
- cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(s_messageDate.runTime));
- // 定义 [ ] 数组
- rt000 = cJSON_CreateArray();
- // 往数组中添加元素
- cJSON_AddItemToArray(rt000, rt000Object1);
- // 将子项插入根项中
- cJSON_AddItemToObject(root, "rt000", rt000);
- //
- // 将 JSON 对象转换为字符串
- jsonString = cJSON_Print(root);
- cJSON_Delete(root);
- temp = EC800_publishMessage("toServer/mqtt/201", jsonString, strlen(jsonString));
- free(jsonString);
- return temp;
- }
- /**
- * @brief 解析实时数据响应
- * @note
- * @retval 无
- */
- void EC800_respondRealDate(cJSON *root, cJSON *item){
- // 获取"name"字段的值
- item = cJSON_GetObjectItem(root, "resultCode");
- if (item == NULL) {
- printf("Field \"resultCode\" not found\n");
- }else{
- // 打印"name"字段的值
- printf("resultCode: %d\n", item->valueint);
- if((item->valueint) == 1){
- printf("Uploaded real-time data successfully\n");
- }else{
- printf("The upload of real-time data has failed\n");
- }
- }
- }
- /**
- * @brief 解析升級数据
- * @note
- * @retval 无
- */
- void EC800_respondUpdateDate(cJSON *root, cJSON *item, ftpInfo *p_ftpInfo){
- /* 获取address对象 */
- cJSON *hardwareUpdate = cJSON_GetObjectItem(root, "paramList");
- if (hardwareUpdate == NULL) {
- printf("Failed to parse paramList data\n");
- return ;
- }
- // 获取"name"字段的值
- item = cJSON_GetObjectItem(hardwareUpdate, "function");
- if (item == NULL) {
- printf("Field \"update\" not found\n");
- }else{
- // 打印"name"字段的值
- printf("function: %d\n", item->valueint);
- // 1:表示固件更新 2:下发时速
- if((item->valueint) == 1){
- s_ec800Date.hardwareUpdate = 1; // 更新固件
- printf("update begin\n");
- }else{
- printf("no update\n");
- }
- }
- // // 获取"ftpAddr"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "ftpAddr");
- // if (item == NULL) {
- // printf("Field \"ftpAddr\" not found\n");
- // }else{
- // printf("ftpAddr: %s\n", item->valuestring);
- //
- // /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
- // strncpy(p_ftpInfo->ftpAddr, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
- //
- // /* 确保字符串以空字符结尾 */
- // p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->ftpAddr) - 1] = '\0';
- // }
- //
- // // 获取"ftpPort"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "ftpPort");
- // if (item == NULL) {
- // printf("Field \"ftpPort\" not found\n");
- // }else{
- // // 打印"name"字段的值
- // printf("ftpPort: %d\n", item->valueint);
- //
- // p_ftpInfo->ftpPort = item->valueint;
- // }
- //
- //
- // // 获取"account"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "account");
- // if (item == NULL) {
- // printf("Field \"account\" not found\n");
- // }else{
- // printf("account: %s\n", item->valuestring);
- //
- // /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
- // strncpy(p_ftpInfo->account, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
- //
- // /* 确保字符串以空字符结尾 */
- // p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->account) - 1] = '\0';
- // }
- //
- // // 获取"passWord"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "passWord");
- // if (item == NULL) {
- // printf("Field \"passWord\" not found\n");
- // }else{
- // printf("account: %s\n", item->valuestring);
- //
- // /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
- // strncpy(p_ftpInfo->passWord, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
- //
- // /* 确保字符串以空字符结尾 */
- // p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->passWord) - 1] = '\0';
- // }
- //
- // // 获取"passWord"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "textDirectory");
- // if (item == NULL) {
- // printf("Field \"textDirectory\" not found\n");
- // }else{
- // printf("textDirectory: %s\n", item->valuestring);
- //
- // /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
- // strncpy(p_ftpInfo->textDirectory, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
- //
- // /* 确保字符串以空字符结尾 */
- // p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textDirectory) - 1] = '\0';
- // }
- //
- // // 获取"passWord"字段的值
- // item = cJSON_GetObjectItem(hardwareUpdate, "textName");
- // if (item == NULL) {
- // printf("Field \"textName\" not found\n");
- // }else{
- // printf("textName: %s\n", item->valuestring);
- //
- // /* 复制字符串到ftpInfo结构体的ftpAddr成员 */
- // strncpy(p_ftpInfo->textName, item->valuestring, sizeof(p_ftpInfo->ftpAddr) - 1);
- //
- // /* 确保字符串以空字符结尾 */
- // p_ftpInfo->ftpAddr[sizeof(p_ftpInfo->textName) - 1] = '\0';
- // }
- }
- /**
- * @brief 解析围栏经纬度数据
- * @note
- * @param root: cJSON 根对象
- * @param Latitude: 纬度数组
- * @param Longitude: 经度数组
- * @retval 成功返回1,失败返回0
- */
- int parse_Latitude_Longitude_Data(cJSON *root, double Latitude[], double Longitude[]) {
- // 确保输入不为空
- if (root == NULL) {
- printf("Root object is NULL\n");
- return 0;
- }
- // 获取 polygonLat 数组
- cJSON *polygonLat = cJSON_GetObjectItem(root, "polygonLat");
- if (polygonLat == NULL || polygonLat->type != cJSON_Array) {
- printf("polygonLat is not an array or is missing\n");
- return 0;
- }
-
- // 获取 polygonLng 数组
- cJSON *polygonLng = cJSON_GetObjectItem(root, "polygonLon");
- if (polygonLng == NULL || polygonLat->type != cJSON_Array) {
- printf("polygonLng is not an array or is missing\n");
- return 0;
- }
- // 获取数组大小并检查是否匹配和是否超出预定义的最大大小
- int polygonLatSize = cJSON_GetArraySize(polygonLat);
- int polygonLngSize = cJSON_GetArraySize(polygonLng);
- if (polygonLatSize != polygonLngSize || polygonLatSize > REC_COORDINATE_DEPTH) {
- printf("Array sizes mismatch or exceed maximum allowed size\n");
- return 0;
- }
- // 解析 polygonLat 数组
- for (int i = 0; i < polygonLatSize; ++i) {
- cJSON *latItem = cJSON_GetArrayItem(polygonLat, i);
- if (latItem == NULL || latItem->type != cJSON_Number) {
- printf("Lat item is not a number or is missing\n");
- return 0;
- }
- Latitude[i] = latItem->valuedouble;
- }
- // 解析 polygonLng 数组
- for (int i = 0; i < polygonLngSize; ++i) {
- cJSON *lngItem = cJSON_GetArrayItem(polygonLng, i);
- if (lngItem == NULL || lngItem->type != cJSON_Number) {
- printf("Lng item is not a number or is missing\n");
- return 0;
- }
- Longitude[i] = lngItem->valuedouble;
- }
- return 1; // 成功
- }
- /**
- * @brief 解析登录响应
- * @note
- * @return
- */
- void parse_Login_Response(cJSON *root, cJSON *item){
- // 获取"name"字段的值
- item = cJSON_GetObjectItem(root, "resultCode");
- if (item == NULL) {
- printf("Field \"resultCode\" not found\n");
- }else{
- // 打印"name"字段的值
- printf("resultCode: %d\n", item->valueint);
- if((item->valueint) == 1){
- s_messageDate.loginResult = 1; // 登录成功
- printf("Login response data successfully\n");
- }else{
- printf("Login response has failed\n");
- }
- }
- }
- /**
- * @brief 通信响应解析
- * @note
- * @return 当前的控制命令
- */
- short EC800_respondParse(void){
- cJSON *root = NULL;
- cJSON *item = NULL;
- uint16_t cmd = 0;
- // 找到JSON数据的起始位置
- const char* start = strchr(g_usart4_rx_buf, '{');
- if (start == NULL) {
- printf("JSON data not found\n");
- return 0;
- }
- // 解析JSON数据
- root = cJSON_Parse(start);
- if (root == NULL) {
- printf("Failed to parse JSON data\n");
- cJSON_Delete(root);
- return 0;
- }
- // 获取"controlCode"字段的值
- item = cJSON_GetObjectItem(root, "controlCode");
- if (item == NULL) {
- printf("Field \"devId\" not found\n");
- }else{
- // 打印"controlCode"字段的值
- printf("controlCode: %d\n", item->valueint);
- cmd = item->valueint;
- }
- // 解析对应命令的对应数据
- switch(cmd){
- case 102:
- parse_Login_Response(root, item);
- break;
- case 106:
- EC800_parseRespondTime(root, item);
- break;
- case 202:
- EC800_respondRealDate(root, item);
- break;
- case 602:
- EC800_respondUpdateDate(root, item, &s_ftpInfo);
- break;
- case 605:
- parse_Latitude_Longitude_Data(root, polygonLat, polygonLng);
- break;
- default :
- break;
- }
- cJSON_Delete(root);
- return (cmd);
- }
- /**
- * @brief EC800M状态转换与使用
- * @note
- * @retval 无
- */
- uint8_t stateStep = 0;
- void EC800_stateTransition_use(void){
- uint16_t right = 1; // 返回是否为正确
- static uint16_t timesCnt = 0;
- static uint8_t tudeErrcnt = 0; // 定位信息错误计数
- ec800Date *p_ec800Date = &s_ec800Date;
- char command[100] = {0};
-
- switch(stateStep){
- case 0: // 初始化模块
- EC800M_link();
- if(linkStep == 12){
- stateStep = 1;
- p_ec800Date->ec800InitFlag = 1; // 初始化完成
- }else{
- p_ec800Date->ec800InitFlag = 0; // 初始化未完成
- }
- break;
- case 1: // 初始化GNSS
- EC800_gnss_init();
- if(gnssStep == 8){
- stateStep = 2;
- }
- break;
- case 2: // 订阅主题
- sprintf(command, "toclient/%d/%x%x%x", DEV_TYPE, s_messageDate.devId[0], s_messageDate.devId[1], s_messageDate.devId[2]);
- right = EC800_subscribeToTopic(command);
- if(right != 1){ // 发布消息失败,可能断开链接
- // stateStep = 0; // 重新初始化
- }else{
- stateStep = 3;
- }
- break;
- case 3: // 登录服务器主题
- right = Login_Topic();
-
- if(right == 1){
- stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
- }
- break;
- case 4: // 下位机请求围栏数据
- right = requesting_Fence_Data();
-
- if(right == 1){
- stateStep = 8; // 去步骤6等待登录响应 收到登录响应后,发布实时消息
- }
- break;
- case 5:
- // 获取定位信息
- EC800_readGnssDate();
- // 没有获取到定位信息 重复获取三次
- if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
- tudeErrcnt++;
- if(tudeErrcnt == 4){
- tudeErrcnt = 0;
- stateStep = 6;
- }
- }else{
- tudeErrcnt = 0;
- stateStep = 6;
- }
- break;
- case 6: // 与服务器对时
- right = EC800_time_calibration();
- if(right == 1){
- stateStep = 8; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
- }
- break;
- case 7: // 发布消息
- right = EC800_uploadRealDate();
- stateStep = 8;
- // if(right == 0){ // 发布消息失败,可能断开链接
- // stateStep = 0; // 重新初始化
- // linkStep = 0;
- // stateStep = 6;
- // }else if(right == 1){
- // stateStep = 6;
- // }
- break;
- case 8: // 接收消息
- HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN); //设置接收缓冲区
- __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
- if(g_usart4_rx_sta!=0){
- HAL_UART_Transmit(&huart1, (uint8_t*)g_usart4_rx_buf, strlen(g_usart4_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
- right = EC800_respondParse();
- if((right == CTR_CODE_LOGIN) && (s_messageDate.loginResult == 1)){ // 登录响应
- stateStep = 4;
- right = 0;
- timesCnt = 0;
- }else if(right == CTR_CODE_LOGIN){ // 登录响应 未登录成功
- stateStep = 3;
- right = 0;
- timesCnt = 0;
- }else if(right == CTR_CODE_FENCES){ // 围栏数据响应
- stateStep = 5;
- right = 0;
- timesCnt = 0;
- }
- else if(right == CTR_CODE_JUDETIME){ // 对时响应
- stateStep = 7;
- right = 0;
- timesCnt = 0;
- }else if(right == CTR_CODE_REMOTE){ // 固件更新
- stateStep = 0xff; // 停止占用4G模块
- }
-
- memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff,接收新的数据
- g_usart4_rx_sta = 0; // 清除接收状态
- }
- // 转弯行驶中
- if(turnFlag == 1){
- if(timesCnt < TURN_INTERVAL_TIME_MS){
- timesCnt++;
- }else{
- timesCnt= 0;
- stateStep = 5;
- HAL_UART_DMAStop(&huart4);
- __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE);
- }
- }else{ // 正常行驶中
- if(timesCnt < PUBLISH_TIME_MS){
- timesCnt++;
- }else{
- timesCnt= 0;
- stateStep = 5;
- HAL_UART_DMAStop(&huart4);
- __HAL_UART_DISABLE_IT(&huart4, UART_IT_IDLE);
- }
- }
-
- break;
- default:
- break;
- }
- }
|