将 usart 接收到的 uint8_t* 数据与常量字符串进行比较
Comparing an usart received uint8_t* data with a constant string
我正在研究 Arduino Due,尝试使用 DMA 功能,因为我正在从事一个速度至关重要的项目。我发现以下函数可以通过串口接收:
uint8_t DmaSerial::get(uint8_t* bytes, uint8_t length) {
// Disable receive PDC
uart->UART_PTCR = UART_PTCR_RXTDIS;
// Wait for PDC disable to take effect
while (uart->UART_PTSR & UART_PTSR_RXTEN);
// Modulus needed if RNCR is zero and RPR counts to end of buffer
rx_tail = (uart->UART_RPR - (uint32_t)rx_buffer) % DMA_SERIAL_RX_BUFFER_LENGTH;
// Make sure RPR follows (actually only needed if RRP is counted to the end of buffer and RNCR is zero)
uart->UART_RPR = (uint32_t)rx_buffer + rx_tail;
// Update fill counter
rx_count = DMA_SERIAL_RX_BUFFER_LENGTH - uart->UART_RCR - uart->UART_RNCR;
// No bytes in buffer to retrieve
if (rx_count == 0) { uart->UART_PTCR = UART_PTCR_RXTEN; return 0; }
uint8_t i = 0;
while (length--) {
bytes[i++] = rx_buffer[rx_head];
// If buffer is wrapped, increment RNCR, else just increment the RCR
if (rx_tail > rx_head) { uart->UART_RNCR++; } else { uart->UART_RCR++; }
// Increment head and account for wrap around
rx_head = (rx_head + 1) % DMA_SERIAL_RX_BUFFER_LENGTH;
// Decrement counter keeping track of amount data in buffer
rx_count--;
// Buffer is empty
if (rx_count == 0) { break; }
}
// Turn on receiver
uart->UART_PTCR = UART_PTCR_RXTEN;
return i;
}
所以,据我理解,这个函数写入变量bytes,作为一个指针,只要不超过就接收到长度。所以我这样称呼它:
dma_serial1.get(data, 8);
没有将其返回值分配给变量。我认为接收到的值存储在 uint8_t* data
中,但我可能错了。
最后我想做的是检查接收到的数据是否是某个字符来做决定,像这样:
if (data == "t"){
//do something//}
我怎样才能完成这项工作?
要像 if (data == "t")
那样比较字符串,您需要一个字符串比较函数,例如 strcmp
。为此,您必须确保参数实际上是(以 0 结尾的)C 字符串:
uint8_t data[9];
uint8_t size = dma_serial1.get(data, 8);
data[size]='[=10=]';
if (strcmp(data,"t")==0) {
...
}
如果您环境中的默认字符类型是 signed char
,要将 data
直接传递给字符串函数,需要从无符号转换为有符号:
if (strcmp(reinterpret_cast<const char*>(data),"t")==0) {
...
}
所以一个完整的 MVCE 可能如下所示:
int get(uint8_t *data, int size) {
data[0] = 't';
return 1;
}
int main()
{
uint8_t data[9];
uint8_t size = get(data, 8);
data[size]='[=12=]';
if (strcmp(reinterpret_cast<const char*>(data),"t")==0) {
cout << "found 't'" << endl;
}
}
输出:
found 't'
我正在研究 Arduino Due,尝试使用 DMA 功能,因为我正在从事一个速度至关重要的项目。我发现以下函数可以通过串口接收:
uint8_t DmaSerial::get(uint8_t* bytes, uint8_t length) {
// Disable receive PDC
uart->UART_PTCR = UART_PTCR_RXTDIS;
// Wait for PDC disable to take effect
while (uart->UART_PTSR & UART_PTSR_RXTEN);
// Modulus needed if RNCR is zero and RPR counts to end of buffer
rx_tail = (uart->UART_RPR - (uint32_t)rx_buffer) % DMA_SERIAL_RX_BUFFER_LENGTH;
// Make sure RPR follows (actually only needed if RRP is counted to the end of buffer and RNCR is zero)
uart->UART_RPR = (uint32_t)rx_buffer + rx_tail;
// Update fill counter
rx_count = DMA_SERIAL_RX_BUFFER_LENGTH - uart->UART_RCR - uart->UART_RNCR;
// No bytes in buffer to retrieve
if (rx_count == 0) { uart->UART_PTCR = UART_PTCR_RXTEN; return 0; }
uint8_t i = 0;
while (length--) {
bytes[i++] = rx_buffer[rx_head];
// If buffer is wrapped, increment RNCR, else just increment the RCR
if (rx_tail > rx_head) { uart->UART_RNCR++; } else { uart->UART_RCR++; }
// Increment head and account for wrap around
rx_head = (rx_head + 1) % DMA_SERIAL_RX_BUFFER_LENGTH;
// Decrement counter keeping track of amount data in buffer
rx_count--;
// Buffer is empty
if (rx_count == 0) { break; }
}
// Turn on receiver
uart->UART_PTCR = UART_PTCR_RXTEN;
return i;
}
所以,据我理解,这个函数写入变量bytes,作为一个指针,只要不超过就接收到长度。所以我这样称呼它:
dma_serial1.get(data, 8);
没有将其返回值分配给变量。我认为接收到的值存储在 uint8_t* data
中,但我可能错了。
最后我想做的是检查接收到的数据是否是某个字符来做决定,像这样:
if (data == "t"){
//do something//}
我怎样才能完成这项工作?
要像 if (data == "t")
那样比较字符串,您需要一个字符串比较函数,例如 strcmp
。为此,您必须确保参数实际上是(以 0 结尾的)C 字符串:
uint8_t data[9];
uint8_t size = dma_serial1.get(data, 8);
data[size]='[=10=]';
if (strcmp(data,"t")==0) {
...
}
如果您环境中的默认字符类型是 signed char
,要将 data
直接传递给字符串函数,需要从无符号转换为有符号:
if (strcmp(reinterpret_cast<const char*>(data),"t")==0) {
...
}
所以一个完整的 MVCE 可能如下所示:
int get(uint8_t *data, int size) {
data[0] = 't';
return 1;
}
int main()
{
uint8_t data[9];
uint8_t size = get(data, 8);
data[size]='[=12=]';
if (strcmp(reinterpret_cast<const char*>(data),"t")==0) {
cout << "found 't'" << endl;
}
}
输出:
found 't'