我正在使用MCU上的UART。下面是mcu打印“hello world”的示例代码。通过串行电缆连接到PC。
#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);
我使用带有串行接口的传感器。这是来自数据表。
引脚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);
我的问题是:
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;
}
}
}
答案 0 :(得分:1)
回答你的问题:
是的,对于来自传感器的每条消息,读取的数据应该逐字节保存到数组中。
当从传感器收到完整的消息时, 然后调用write函数。