Search code examples
c++serial-portuartmbed

Dispatch message properly UART


In my code, I have a serial interrupt that triggers when an event (receiving) occurs. In that, I'm saving the data to buffer and with that, sending the buffer to analyze. The problem I have is, how can I fetch the whole response from the serial port to buffer not just one char at the time?

Serial pc(USBTX, USBRX);
Serial sim(GSM_TX_PIN, GSM_RX_PIN);
count =0;
int main()
{
    sim.attach(&dispatchMsg); //Interrupt handler
    wait(1);

    sim.puts("AT\r\n");

    while()
    {
    }
}

void dispatchMsg()
{
    while(sim.readable()){
        char c = sim.getc();
        buffer[count++] = c;
    } 

    //Here i want to wait for whole response from uart instead i fetch only one char
    void analyzeString(buffer);
}

void analyzeString(char* str)
{            
    /*Do something with the str ...*/         
}

Example of incoming responses from sim module:

AT
OK
ERROR
+CMGI: ...

IDE: mbed


Solution

  • Use the ATCmdParser instead of using raw UART. It already does parsing and buffering for you and is part of Mbed OS 5.