sending commands and parsing responses on serial

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm using n ATMEga328P to send commands to a ESP8266 via USART. I wrote some code to send commands and receive responses using two FIFO circular buffers (one receive buffer and one transmit buffer), and USART interrupts take care of sending/receiving data.

(the usual stuff). The ESP module receives a command and responds either with a response and OK\r\n or with ERROR\r\n. I'm checking the RX buffer after every command to see what response i got. I implemented a find(const char*) function that looks for a specific string of characters in the receive buffer, consuming the characters as it goes through them and blocking until it finds the string. 

 

Now, this leaves me with this problem: I want to check wheter I received OK or ERROR and THEN parse the response, but the find function consumes the characters in the buffers (it calls a function USART_receive() that gets the char from the queue).

 

In general, how can I handle serial communication with such a module? (It will come in handy with BLE or GPRS modules as well...) in a robust fashion?

Last Edited: Sun. Sep 3, 2017 - 01:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So why not extract to a second buffer (usually up to \n), do the strcmp() and then any other required processing. 

 

Another approach is a character by character state machine. 

 

BTW, if you are in charge of this protocol why complicate it by sending whole "OK" / "ERROR" (such things are designed for humans not computers). It'd be far simpler to send a one byte ACK/NAK. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
BTW, if you are in charge of this protocol why complicate it by sending whole "OK" / "ERROR" (such things are designed for humans not computers). It'd be far simpler to send a one byte ACK/NAK. 

 

I'm using the Expressif standard AT firmware, so I'm trying to parse the protocol they provide

 

clawson wrote:
So why not extract to a second buffer (usually up to \n), do the strcmp() and then any other required processing. 

 

because I want to look for OK and then for ERROR if I don't get an OK. 

 

What's the general approach (if any) in these situations?

Last Edited: Sun. Sep 3, 2017 - 01:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

__LuckyLuke__ wrote:
 send commands to a ESP8266 via USART.

 

Do you mean, specifically, AT Commands?

 

If so, see: https://www.avrfreaks.net/comment... - includes a link to an ESP8266 AT Command handler.

 

See also: https://www.avrfreaks.net/comment...

 

EDIT

 

Should've read the whole thread first - now I see you are using AT commands!

 

In that case, the links will certainly give you plenty to go on.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sun. Sep 3, 2017 - 05:03 PM