简体   繁体   English

嵌入式系统中的AT命令

[英]AT commands in embedded systems

I am using embedded C and trying to make application for GPRS terminal. 我正在使用嵌入式C并试图申请GPRS终端。 My main problem is working with AT commands. 我的主要问题是使用AT命令。 I send AT command using serial line, but if it is some network oriented command its response could take time and because of that I have a lot of waiting, while processor don`t do anything. 我使用串行线发送AT命令,但如果它是一些面向网络的命令,它的响应可能需要时间,因此我有很多等待,而处理器不做任何事情。 Idea is to make this waiting to be done in same way parallel like in different thread. 想法是让这个等待以相同的方式完成并行,就像在不同的线程中一样。 Does anyone have idea how to do that because my system does not support threads? 有谁知道如何做到这一点,因为我的系统不支持线程? I had idea to use some timers, because we have interrupt which is called every 5ms, but I don't know ho many seconds I have to wait for response, and if I compare strings in interrupt to check if all message is received it could be very inefficient, right? 我有想法使用一些定时器,因为我们每5ms调用一次中断,但是我不知道等待响应的时间很长,如果我在中断中比较字符串以检查是否收到所有消息,它可以效率很低吧?

you could either use interrupts, configure the serial interface to interrupt when data is available, or use an RTOS something, like FreeRTOS , to run two threads, one for the main code and the other to block and wait for the serial data. 您可以使用中断,将串行接口配置为在数据可用时进行中断,或者使用RTOS(如FreeRTOS )运行两个线程,一个用于主代码,另一个用于阻塞和等待串行数据。

Update: based on your comments, you say you don't know the size of the data, that's fine, in the interrupt handler check for the byte that terminates the data, this is a simple and generic example you should check the examples for your MCU: 更新:根据你的评论,你说你不知道数据的大小,没关系,在中断处理程序中检查终止数据的字节,这是一个简单而通用的例子你应该检查你的例子MCU:

void on_serial_char()
{     
  //disable interrupts
  disable_interrupts();

  //read byte
  byte = serial.read();

  //check if it's the terminating byte
  if (byte == END) {
      //set the flag here
      MESSAGE_COMPLETE = 1;
  }

  //add byte to buffer
  buf[length++] = byte;

  //enable interrupts
  enable_interrupts();      
}

And check for that flag in your main loop: 并在主循环中检查该标志:

...
if (MESSAGE_COMPLETE) {
    //process data
    ...

    //you may want to clear the flag here
    MESSAGE_COMPLETE = 0;

    //send next command
    ...
}    

You can simply call a packetHandler in each mainLoopCycle. 您可以在每个mainLoopCycle中调用packetHandler。
This handler checks if new characters are available from the serial port. 此处理程序检查串行端口是否有新字符可用。

The packetHandler will build the response message bit for bit, if the message is complete (CR LF found) then it calls a messageReceive function, else it simply returns to the mainLoop. packetHandler将为bit构建响应消息位,如果消息完成(找到CR LF),则它调用messageReceive函数,否则它只返回mainLoop。

int main()
{
  init();
  for (;;)
  {
     packetHandler();
  }
}

char msgBuffer[80];
int pos=0;
void packetHandler()
{
  char ch;
  while ( isCharAvailable() )
  {
    ch=getChar();
    msgBuffer[pos++] = ch;
    if ( ch == '\n' ) 
    {
       messageReceived(msgBuffer);
       pos=0;
    }
  }
}

It sounds like you are rather close to the hardware drivers. 听起来你很接近硬件驱动程序。 If so, the best way is to use DMA, if the MCU supports it, then use the flag from the DMA hardware to determine when to start parse out the received data. 如果是这样,最好的方法是使用DMA,如果MCU支持它,则使用DMA硬件中的标志来确定何时开始解析接收的数据。

The second best option is to use rx interrupts, store every received byte in a simple FIFO, such as a circular buffer, then set some flag once you have received them. 第二个最佳选择是使用rx中断,将每个接收到的字节存储在一个简单的FIFO中,例如循环缓冲区,然后在收到它们后设置一些标志。 One buffer for incoming data and one for the latest valid data received may be necessary. 可能需要一个用于输入数据的缓冲区和一个用于接收最新有效数据的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM