gpt4 book ai didi

c - mcu UART,从传感器(rx 引脚)读取串行数据并在 PC 上显示(Tx 引脚)

转载 作者:行者123 更新时间:2023-11-30 15:12:57 26 4
gpt4 key购买 nike

我正在 MCU 上使用 UART。下面是 MCU 通过串行电缆向 PC 打印“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);

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

Pin 5 Serial Output: The sensor has a TTL outputs. The output is an ASCII capital “R”, followed by four ASCII character digits representing the range in millimeters, followed by a carriage return (ASCII 13).

我正在尝试读取 Rx 引脚上的传感器数据并将其传输到 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;
}
}
}

最佳答案

回答您的问题:

是的,从传感器读取的每条消息的数据都应该逐字节保存到数组中。

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

关于c - mcu UART,从传感器(rx 引脚)读取串行数据并在 PC 上显示(Tx 引脚),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34899435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com