UART Receive question

Thread Starter

Jswale

Joined Jun 30, 2015
121
Hi All,

I am working on a project and need to gather data via a command terminal using a Buffer on the RX pin of a UART.

It receives 1 byte at a time, e.g if it received HELLO, the RX buffer would be; H then would clear and become E etc etc.

Is there an efficient way to receive full data and then act upon that data. For example if I was actively looking for HELLO and then I would turn on a LED.

Currently I am using an array and dumping the values into that, anyone got a better solution? :)

JSwale
 

JohnInTX

Joined Jun 26, 2012
4,787
A good way to do this is to use an interrupt-driven UART receiver with a FIFO buffer implemented as an array with indexes that 'wrap around' making it a circular queue with a separate index for 'put' and 'get' as well as a counter that indicates how many characters are in the buf. The buffer is initialized by setting the indexes and the counter to 0.

Each character received interrupts the PIC, the character gets put into the buffer and the 'put' index gets bumped. At the end of the array, the index gets wrapped. Make the size of this receive buffer big enough to handle several messages.
Read the buffer by testing the counter for >0 characters to see if there are any. If so, read the next character using the 'get' index and bumping that. Decrement the counter.

The read routine usually needs to disable the UART RX interrupt momemtarily while accessing the array. Just long enough to read the character and decrement the counter. Bumping/wrapping the 'get' index can be done with the interrupts running.

The actual testing for a character sequence gets done in main (non-interrupt) code. The buffer is read and each character is inspected for the particular sequence. You might want to use a separate 'message' buffer that gets loaded according to your rules (start message character, end message etc).

The key point is that characters received get automatically queued at the character rate and de-queued at the processing rate. The interrupt-driven buffer relieves the main processing of the burden of keeping up with the character rate and processing in main relieves the interrupt routine(s) from having to make a lot of decisions on the fly.

Its the standard way of doing this kind of thing.
Good luck.
 
Top