c++ - 从 Amazon FreeRTOS 的串行问题中读取

标签 c++ c freertos rtos esp32

我正在尝试从任务线程上的 ESP32 DevKitC 克隆上的 uart2 读取数据。

我的代码如下:

uint8_t data[128];
data[0] = '\0'; //null terminate so we don't print garbage

for( ; ; ) {
    //get a chunk of data off the FIFO buffer
    const int uart_num = UART_NUM_2;
    int length = 0;
    IotLogInfo( "preread" );
    length = uart_read_bytes(uart_num, data, 10, 1000 / portTICK_RATE_MS); //read 10 bytes or time out after a second.

    if(length == -1)
        IotLogInfo( "read reported error! -1" );

    //did we rx anything?
    if(length > 0){
        data[length] = '\0'; //null terminate the string
        IotLogInfo( "Rx: %i bytes", length );
        IotLogInfo( "Rx: %s ", data );
    }
    else{
        IotLogInfo( "rx no data" );
    }
    vTaskDelay(1000 / portTICK_PERIOD_MS);
}

出于某种原因,传输的字符串“UVW”的字符 2 和 3 被弄乱了,即使缓冲区包含多个传输:

122 4911 [_readSerialForM] [INFO ][DEMO][49110] preread

123 5011 [_readSerialForM] [INFO ][DEMO][50110] Rx: 3 bytes

124 5011 [_readSerialForM] [INFO ][DEMO][50110] Rx: U⸮⸮ 

126 5011 [_readSerialForM] [INFO ][DEMO][50110] preread

127 5111 [_readSerialForM] [INFO ][DEMO][51110] Rx: 3 bytes

128 5111 [_readSerialForM] [INFO ][DEMO][51110] Rx: U⸮⸮ 

130 5111 [_readSerialForM] [INFO ][DEMO][51110] preread

131 5510 [_readSerialForM] [INFO ][DEMO][55100] Rx: 12 bytes

132 5510 [_readSerialForM] [INFO ][DEMO][55100] Rx: U⸮⸮U⸮⸮U⸮⸮U⸮⸮ 

134 5510 [_readSerialForM] [INFO ][DEMO][55100] preread

135 5710 [_readSerialForM] [INFO ][DEMO][57100] Rx: 6 bytes

136 5710 [_readSerialForM] [INFO ][DEMO][57100] Rx: U⸮⸮U⸮⸮ 

138 5710 [_readSerialForM] [INFO ][DEMO][57100] preread

139 5910 [_readSerialForM] [INFO ][DEMO][59100] Rx: 6 bytes

140 5910 [_readSerialForM] [INFO ][DEMO][59100] Rx: U⸮⸮U⸮⸮ 

“UVW”应该是 0x55 x56 x57 但它似乎被解释为“0x55 0xD5 0xFD”,更奇怪的是,如果我将“UUU”发送到应该是“0x55 0x55 0x55”的序列号,它到达时是“0x55” 0x55 0xF5",这很奇怪,因为字节 #2 在它之前的字节重复时没有格式错误。

我已经用 arduino 独立验证了传输,它读取完美。所以我很困惑。

我确信这是我犯的一个相当简单的错误。但是我不知道它是什么。

提前谢谢你。

最佳答案

串行接口(interface)必须正确配置。两端设置 必须相同。

Wikipedia

Many settings are required for serial connections used for asynchronous start-stop communication, to select speed, number of data bits per character, parity, and number of stop bits per character.

(...)

Often if the settings are entered incorrectly the connection will not be dropped; however, any data sent will be received on the other end as nonsense.

如果接收器在线路上看到错误的位模式,它应该引发“帧错误”或“奇偶校验错误”。 但是,无法检测到某些错误,并且通常会忽略这些错误状态。

串行接口(interface)问题的另一个重要来源是电气方面。 传统 RS232 的电压高达 +/-15V。相比之下,微 Controller 更喜欢经典的逻辑信号(3.3V 或 5V)。 混合电线可能允许在一个方向上进行一些通信。 要排除这种复杂情况,您应该使用万用表或更好的示波器检查信号。

关于c++ - 从 Amazon FreeRTOS 的串行问题中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56678040/

相关文章:

c - 为共享内存初始化结构体中的 int

embedded - 如何更改 FreeRTOS 中任务的最大可用堆大小?

stack - ISR(中断服务程序)是否有单独的堆栈?

c++ - 构造函数生成对象但不为其赋值

c++ - 使用 QTcpSocket 时出错

c - 获取 LNK2001 : unresolved external symbol when I run my program 的错误代码

c - 在c中使用scanf()获取多行输入

multithreading - 使用 netconn 同时处理多个 LwIP 连接

C++ 错误 : no appropriate default constructor available

c++ - 使用 fstream 将文件读入单独的变量最有效