【问题标题】:mcu UART, reading serial data from a sensor (rx pin) and displaying (Tx pin) on PCmcu UART,从传感器(rx 引脚)读取串行数据并在 PC 上显示(Tx 引脚)
【发布时间】:2016-04-26 06:18:46
【问题描述】:

我正在使用 MCU 上的 UART。下面是通过串行电缆连接到 PC 的 mcu 打印“hello world”的示例代码。

#define BANNER ("Hello, world!\n\n")
int main(void)
{
ti_uart_write_buffer(TI_UART_0, (uint8_t *)BANNER,
                 sizeof(BANNER));
    return 0;
}

这是头文件中的ti_uart_write_buffer 定义。

/**
 * Perform a write on the UART interface. This is a blocking
 * synchronous call. The function will block until all data has
 * been transferred.
 *
 * @brief UART multi-byte data write.
 * @param [in] uart UART index.
 * @param [in] data Data to write to UART.
 * @param [in] len Length of data to write to UART.
 * @return ti_rc_t ti_RC_OK on success, error code otherwise.
 */
ti_rc_t ti_uart_write_buffer(const ti_uart_t uart, const uint8_t *const data,
                 uint32_t len);

我正在使用带有串行接口的传感器。这是来自数据表。

Pin 5 串行输出:传感器具有 TTL 输出。输出是一个 ASCII 大写“R”,后跟四个 ASCII 字符数字 表示以毫米为单位的范围,后跟回车 (ASCII 13)。

我正在尝试读取 Rx 引脚上的 sesor 数据并将其通过 Tx 引脚传输到 PC。

这是我收集到的头文件中需要使用的 uart 读取函数。

/**
 * Perform a single character read from the UART interface.
 * This is a blocking synchronous call.
 *
 * @brief UART character data read.
 * @param [in] uart UART index.
 * @param [out] data Data to read from UART.
 * @return qm_uart_status_t Returns UART specific return code.
 */
ti_uart_status_t ti_uart_read(const ti_uart_t uart, uint8_t *data);

我的问题是:

  1. 我在 main() 中创建了一个循环,它不断读取串行数据并将新数据添加到数组中,直到到达结束标记。这有意义吗?
  2. 鉴于上面的 hello world 示例,我如何打印出数组?

typedef uint8_t byte;
const byte numChars = 32;
char receivedChars[32]; 
bool newData = false;

void recvWithEndMarker();

int main(void)
{
        do{  
    recvWithEndMarker();
        }
        while (1);
    return 0;
}

void recvWithEndMarker()
{
     static byte ndx = 0;
     char endMarker = '\r';
     char rc;
     while (newData == false) {
        rc = ti_uart_read_non_block(QM_UART_0);  
        if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
                        }
                }
            else {
                receivedChars[ndx] = '\0'; 
                ndx = 0;
                newData = true;
        }
    }
}

【问题讨论】:

  • 该函数每次读取一个字符。您必须循环并将数组的单个单元格传递给函数。
  • PC机通常有RS232输入/输出,而MCU有TTL电平输入输出。这有很大的不同,您需要一些转换器(即用于电平转换的 max232*-IC)
  • 您必须确保波特率、停止位数量、奇偶校验位格式与传感器完全相同 --> mcu 和 mcu --> pc
  • 这个表达式:sizeof(BANNER)); 将导致在字符串末尾传输 NUL 字节。你真的想这样做吗?或许你真的很想用:strlen(BANNER))
  • 您需要围绕ti_uart_read() 函数编写一个“包装器”,该函数 1) 在输入“R”之前丢弃字符。 2) 将“R”中的所有字符保存到缓冲区中,直到/包括输入 0x0C 3) 将 0x00 字节附加到输入字符数组。您的 main() 函数初始化所有内容,然后循环调用 ti_uart_read() 的包装器,然后调用 ti_uart_write_buffer()

标签: c sensors uart


【解决方案1】:

回答你的问题:

是的,读取的数据,对于来自传感器的每条消息都应该逐字节保存到一个数组中。

当从传感器接收到完整的消息时, 然后调用 write 函数。

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 2022-01-20
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多