当前位置:网站首页>4G module at command communication package interface designed by charging pile
4G module at command communication package interface designed by charging pile
2022-07-03 09:59:00 【Wukong is so timid】
/********************************************************************
* name : AT_cmd
* function : send data at Command and get the results
* Entrance parameters : cmd:at command cmd_len:len
* ret_buf:result buf_len:result len
*
* Export parameters : The correct return is 0, The error is returned as -1
*******************************************************************/
#define LTE_MAX_TRY_TIME (2) // Reading serial port may fail , Try at most 3 Time
#define UART2_AT_COMMAND_TIMEOUT (5*1000)
uint8_t cur_at_rec_len = 0;
int AT_cmd(uint8_t *cmd, uint8_t cmd_len, uint8_t *rcv_buf, uint8_t* rcv_len)
{
// int fd = -1; // File descriptor
// char rcv_buf[512] = {0};
//char send_buf[UART2_TX_BUFFER_LEN]= {0};
// int rcv_len = 0;
int count = 0;
int ret = -1;
uint16_t send_len = 0;
cur_at_rec_len = *rcv_len;
uint32_t flags = 0;
if(NULL == cmd || cmd_len == 0)
{
printf("at err:cmd is NULL\n");
return -1;
}
if(NULL == rcv_buf || rcv_len == NULL)
{
printf("at err:cmd is NULL\n");
return -1;
}
if (cmd_len >= UART2_TX_BUFFER_LEN)
{
printf("at err:cmd is too long =%d\n",cmd_len);
return -1;
}
// printf("**cmd=%s,cmd_len=%d ,strlen(cmd)=%d**\r\n",cmd,cmd_len,strlen(cmd));
// snprintf(send_buf, sizeof(send_buf), "%s\r", cmd);
// memcpy(send_buf,cmd,strlen(cmd));
// while (count < LTE_MAX_TRY_TIME)
{
//tcflush(fd,TCIOFLUSH);
//printf("send at=%s len=%d\r\n",send_buf,strlen(send_buf));
//uart2_dma_send_data(RxBuffer1,RxCounter1-len);
if(cur_at_rec_len != 0)
{
printf("cur_at_rec_len=%d\r\n",cur_at_rec_len);
USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);
USART_DMACmd(USART2, USART_DMAReq_Rx, DISABLE);// close DMA receive
DMA_Cmd(DMA1_Channel6, DISABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
send_len = uart2_dma_send_data(cmd,cmd_len);
printf("send start(%s) and rx start,a=%d n=%d\r\n",cmd,send_len,cmd_len);
flags = EVENT_FLAGS_UART2_TX_COMPLETE|EVENT_FLAGS_UART2;
printf("*****osEventFlagsWait s flags =0x%08x %d\r\n",flags,get_curtime());
flags =osEventFlagsWait(evt_id_uart, flags, osFlagsWaitAll, UART2_AT_COMMAND_TIMEOUT);
//flags =osEventFlagsWait(evt_id_uart, EVENT_FLAGS_UART2, osFlagsWaitAny, osWaitForever);
//rx at command response from 4g module
printf("*****osEventFlagsWait e flags =0x%08x %d\r\n",flags,get_curtime());
//if(flags == EVENT_FLAGS_UART2_TX_COMPLETE)
//{
//printf("send at end,rx...\r\n");
//}
if( (flags &EVENT_FLAGS_UART2_TX_COMPLETE) == EVENT_FLAGS_UART2_TX_COMPLETE)
printf("send end\r\n");
else
printf("send fail send_len =%d, cmd_len =%d\r\n",send_len, cmd_len);
//if( (flags &EVENT_FLAGS_UART2) == EVENT_FLAGS_UART2)
// printf("rx end\r\n");
// if(send_len == cmd_len)
if( (flags &EVENT_FLAGS_UART2) == EVENT_FLAGS_UART2)
{
//printf("rx...\r\n");
//uint32_t flags =osEventFlagsWait(evt_id_uart, EVENT_FLAGS_UART2, osFlagsWaitAll, UART2_AT_COMMAND_TIMEOUT);
//UART2_AT_COMMAND_TIMEOUT);
//rx at command response from 4g module
//printf("*****osEventFlagsWait flags =0x%08x\r\n",flags);
printf("rx 11 end\r\n");
if(cur_at_rec_len != 0)
{
printf("cur_at_rec_len=%d\r\n",cur_at_rec_len);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);// Turn on DMA receive
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
//if(flags == EVENT_FLAGS_UART2)
{
//printf("uart2 rx at command response from 4g moduler\r\n");
uart2_rec_at_cmd_response(rcv_buf,rcv_len);
// printf("at recv data is %s\r\n", rcv_buf);
/* Yes OK Indicates that the received data is correct */
if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, "OK"))
{
printf("rx 11 ok\r\n");
ret = 0;
// break;
}
else
{
printf("at err:recv invalid data is %s\r\n", rcv_buf);
}
}
//else
// {
// printf("send at command,but 4g moduler no response\r\n");
// printf("at err:recv failed =0x%08x\r\n",flags);
//}
}
else
{
printf("at err:rx failed\n");
}
count++;
}
//sleep(1);
return ret;
}
//bool sim800c_send_cmd(char *cmd_data, char *ack, uint8_t retry, uint32_t time_out)
int at_cmd_ack(uint8_t *cmd, uint16_t cmd_len, uint8_t *rcv_buf,uint8_t* rcv_len,uint8_t *ack, uint32_t time_out)
{
// int count = 0;
int ret = -1;
uint16_t send_len = 0;
uint32_t flags = 0;
if(NULL == cmd || cmd_len == 0)
{
printf("at err:cmd is NULL\n");
return -1;
}
if(NULL == rcv_buf || rcv_len == NULL)
{
printf("at err:cmd is NULL\n");
return -1;
}
if (cmd_len >= UART2_TX_BUFFER_LEN)
{
printf("at err:cmd is too long cmd_len=%d\n",cmd_len);
return -1;
}
cur_at_rec_len = *rcv_len;
if(ack == NULL)
{
ret = AT_cmd(cmd, cmd_len, rcv_buf, rcv_len);
return ret;
}
else
{
send_len = uart2_dma_send_data(cmd,cmd_len);
printf("send start(%s) and rx start,a=%d n=%d\r\n",cmd,send_len,cmd_len);
osDelay(100);
flags = EVENT_FLAGS_UART2_TX_COMPLETE|EVENT_FLAGS_UART2;
flags =osEventFlagsWait(evt_id_uart, flags, osFlagsWaitAll, UART2_AT_COMMAND_TIMEOUT);
printf("*****osEventFlagsWait flags =0x%08x t=%d\r\n",flags,get_curtime());
if( (flags &EVENT_FLAGS_UART2_TX_COMPLETE) == EVENT_FLAGS_UART2_TX_COMPLETE)
printf("send end\r\n");
else
{
printf("send fail send_len =%d, cmd_len =%d\r\n",send_len, cmd_len);
goto out;
}
if( (flags &EVENT_FLAGS_UART2) == EVENT_FLAGS_UART2)
{
uart2_rec_at_cmd_response_check_ack(rcv_buf,rcv_len,ack);
printf("rx 1 end,ack=%s,rcv_len=%d\r\n",ack,*rcv_len);
//uart2_rec_at_cmd_response(rcv_buf,rcv_len);
/* Yes OK Indicates that the received data is correct */
//strstr
//if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, (const char*)ack))
if (strlen((const char*)rcv_buf) > 0 && dx_memmem((char*)rcv_buf, *rcv_len,(char*)ack,strlen((const char*)ack))>=0)
{
ret = 0;
printf("rx 1 ok\r\n");
goto out;
}
//else if (strlen((const char*)rcv_buf) > 0 && dx_memmem((const char*)rcv_buf, rcv_len,"+QIURC: \"recv\",0",strlen(ack))
else if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, "+QIURC: \"recv\",0"))
{
ret = 1;
printf("rx 2 ok,need to get\r\n");
goto out;
}
else if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, "+QIURC: \"pdpdeact\","))
{
ret = 2;
printf("rx 3 ok,but error\r\n");
goto out;
}
else
{
ret = -2;
//printf("at err:recv no ack data is %s ,times=%d\n", rcv_buf,times);
printf("at err:recv len=%d,no ack data is %s\r\n", *rcv_len,rcv_buf);
printf("strlen((const char*)rcv_buf)=%d\r\n",strlen((const char*)rcv_buf));
#if 1
int i =0;
for(i=0; i<*rcv_len; i++)
{
printf("0x%02x,",rcv_buf[i]);
if((i+1)%10 == 0)
printf("\r\n");
}
printf("\r\n");
#endif
}
}
//{
printf("rec_wait..\r\n");
unsigned int times = time_out/1000;
rec_wait:
// reset_uart2_rx_buffer();
#if (UART2_RX_DMA ==1)
// DMA_Enable(DMA1_Channel6,UART2_RX_BUFFER_LEN);// Turn on the next time DMA receive
#endif
while(times)
{
times --;
uint32_t flags =osEventFlagsWait(evt_id_uart, EVENT_FLAGS_UART2, osFlagsWaitAll, 1000);
//UART2_AT_COMMAND_TIMEOUT);
//rx at command response from 4g module
printf("*****osEventFlagsWait flags =0x%08x t=%d\r\n",flags,get_curtime());
if( flags == EVENT_FLAGS_UART2)
{
//printf("uart2 rx at command response from 4g moduler\r\n");
uart2_rec_at_cmd_response_check_ack(rcv_buf,rcv_len,ack);
//uart2_rec_at_cmd_response(rcv_buf,rcv_len);
/* Yes OK Indicates that the received data is correct */
if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, (const char*)ack))
{
ret = 0;
printf("rx 4 ok\r\n");
break;
}
else if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, "+QIURC: \"recv\",0"))
{
ret = 1;
printf("rx 5 ok,need to get\r\n");
goto out;
}
else if (strlen((const char*)rcv_buf) > 0 && strstr((const char*)rcv_buf, "+QIURC: \"pdpdeact\","))
{
ret = 2;
printf("rx 6 ok,but error\r\n");
goto out;
}
else
{
ret = -2;
printf("at err:recv no ack data is %s ,times=%d\n", rcv_buf,times);
goto rec_wait;
}
}
else
{
ret = -3;
printf("send at command,but 4g moduler no response\r\n");
printf("at err:recv failed =0x%08x ,times=%d\r\n",flags,times);
}
}
//}
//count++;
}
out:
reset_uart2_rx_buffer();
#if (UART2_RX_DMA ==1)
DMA_Enable(DMA1_Channel6,UART2_RX_BUFFER_LEN);// Turn on the next time DMA receive
#endif
//sleep(1);
return ret;
}
边栏推荐
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- (1) 什么是Lambda表达式
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- In third tier cities and counties, it is difficult to get 10K after graduation
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
- How does the memory database give full play to the advantages of memory?
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- The third paper of information system project manager in soft examination
猜你喜欢

一个可执行的二进制文件包含的不仅仅是机器指令
![[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation](/img/06/7fd985faf8806ceface3864d4b3180.png)
[Li Kou brush question notes (II)] special skills, module breakthroughs, classification and summary of 45 classic questions, and refinement in continuous consolidation
![【顺利毕业】[1]-游览 [学生管理信息系统]](/img/91/72cdea3eb3f61315595330d2c9016d.png)
【顺利毕业】[1]-游览 [学生管理信息系统]

STM32 serial port usart1 routine

Vector processor 9_ Basic multilevel interconnection network

內存數據庫究竟是如何發揮內存優勢的?

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

The third paper of information system project manager in soft examination

IDEA远程断点调试jar包项目

手机都算是单片机的一种,只不过它用的硬件不是51的芯片
随机推荐
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Qt QComboBox QSS样式设置
STM32 running lantern experiment - library function version
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
Interruption system of 51 single chip microcomputer
Chromium Embedded Framework (CEF) 介绍
单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了
Exception handling of arm
Introduction to chromium embedded framework (CEF)
Idea remote breakpoint debugging jar package project
An executable binary file contains more than machine instructions
51 MCU tmod and timer configuration
2020-08-23
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
[male nanny style] teach you to open the first wechat applet
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
PRACH --- originator
JS foundation - prototype prototype chain and macro task / micro task / event mechanism