-1

I want to write C code for an embedded system such that the data received at the RS232 port should be read continuously without creating a separate "RS232 TASK" for reading the data.

Can anyone help me with this?

I just need a basic approach for reading data without task creation.

Clifford
  • 88,407
  • 13
  • 85
  • 165

2 Answers2

0

Identify the function that tells you if some data was received. Commonly it returns a boolean value or the number of received bytes. (BTW, most protocols on RS232 allows 5 to 8 data bits per transmission.)

Use that function in a conditional block to call the next function that actually reads one or more received bytes. In case that nothing was received, this prevents your loop to block.

Example (without knowing how the functions are named in your case):

/* any task */ {
    for (;;) /* or any other way of looping */ {
        /* do some stuff, if needed */
        if (areRs232DataAvailable()) {
            uint8_t data = fetchRs232ReceivedByte();
            /* handle received data */
        }
        /* do some stuff, if needed */
    }
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
0

I would ask why you think reading data from a UART (which I assume is what you mean by "RS-232") requires a task at all? A solution will depend a great deal on your platform and environment and you have not specified other than FreeRTOS which does not provide any serial I/O support.

If your platform or device library already includes serial I/O, then you might use that, but at the very lowest level, the UART will have a status register with a "data available" bit, and a register or FIFO containing that data. You can simply poll the data availability, then read the data.

To avoid data loss while the processor is perhaps busy with other tasks, you would use either interrupts or DMA. At the very least the UART will be capable of generating an interrupt on receipt of a character. The interrupt handler would place the new data into a FIFO buffer (such as an RTOS message queue), and tasks that receive serial data simply read from the buffer asynchronously.

DMA works similarly, but you queue the data in response to the DMA interrupt. That will reduce the interrupt rate, but you have to deal with the possibility of a partially full DMA buffer waiting indefinitely. Also not all platforms necessarily support UART as a DMA source, or even DMA at all.

Clifford
  • 88,407
  • 13
  • 85
  • 165