简体   繁体   English

如何在不创建 RS232 任务的情况下从 RS232 端口读取数据(嵌入式 FreeRTOS C)?

[英]How to Read data from RS232 port without RS232 task creation (Embedded FreeRTOS C)?

I wanna write a embedded C Code so that the data received at the RS232 port should be read continuously and I don't wanna create a separate "RS232 TASK" for reading the data.Can anyone help me with this?我想编写一个嵌入式 C 代码,以便连续读取在 RS232 端口接收的数据,我不想创建单独的“RS232 任务”来读取数据。有人可以帮我吗? I just need a basic approach for reading data without task creation我只需要一种无需创建任务即可读取数据的基本方法

Identify the function that tells you if some data was received.识别 function,它会告诉您是否收到了一些数据。 Commonly it returns a boolean value or the number of received bytes.通常它返回一个 boolean 值或接收到的字节数。 (BTW, most protocols on RS232 allows 5 to 8 data bits per transmission.) (顺便说一句,RS232 上的大多数协议允许每次传输 5 到 8 个数据位。)

Use that function in a conditional block to call the next function that actually reads one or more received bytes.在条件块中使用该 function 来调用实际读取一个或多个接收到的字节的下一个 function。 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 */
    }
}

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?我会问你为什么认为从 UART 读取数据(我认为这就是你所说的“RS-232”)需要一项任务? 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.解决方案在很大程度上取决于您的平台和环境,并且您没有指定除 FreeRTOS 之外不提供任何串行 I/O 支持的解决方案。

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.如果您的平台或设备库已经包含串行 I/O,那么您可能会使用它,但在最低级别,UART 将有一个带有“数据可用”位的状态寄存器,以及一个包含该数据的寄存器或 FIFO。 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.为避免在处理器可能忙于其他任务时丢失数据,您可以使用中断或 DMA。 At the very least the UART will be capable of generating an interrupt on receipt of a character.至少 UART 将能够在接收到字符时产生中断。 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.中断处理程序会将新数据放入 FIFO 缓冲区(例如 RTOS 消息队列),接收串行数据的任务只是从缓冲区异步读取。

DMA works similarly, but you queue the data in response to the DMA interrupt. DMA 的工作方式类似,但您将数据排队以响应 DMA 中断。 That will reduce the interrupt rate, but you have to deal with the possibility of a partially full DMA buffer waiting indefinitely.这将降低中断率,但您必须处理部分满的 DMA 缓冲区无限期等待的可能性。 Also not all platforms necessarily support UART as a DMA source, or even DMA at all.此外,并非所有平台都必须支持 UART 作为 DMA 源,甚至根本不支持 DMA。

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

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