Uart没有在C++中阅读整个消息

Uart not reading whole message in C++

本文关键字:消息 C++ Uart      更新时间:2023-10-16

我正在使用termios从uart设备中读取线路,尽管它的行为不稳定。我使用的是阻塞规范模式,我发现大多数时候我只接收消息的后端。我启用的标志有:ICRNL、ICANON、ISIG、CLOCAL、O_RDWR、O_NOCTTY和CREAD。我的波特率是38400。

从设备发出的消息以回车结束,我使用了一个逻辑分析器来查看进出设备的信号是否符合我的预期。

//The code for all of our flags
uart.inputModes.IgnoreCharactersWithParityError = false;
uart.localModes.CanonicalInput = true;
uart.localModes.EnableSignals = true;
uart.controlModes.ParityEnabled = false;
uart.controlModes.IgnoreModemStatusLines = true;
uart.controlModes.EnableReceiver = true;
uart.inputModes.MapCR_To_NL = true;
uart.openDeviceUart();
int readArraySize = 50;
unsigned char readData[readArraySize];
memset(readData, 0, readArraySize);
uart.SendBytes("$C01,03,41r");
int myBytes = uart.ReadBytes(readData, readArraySize);
std::cout << "Bytes Received: " << myBytes << std::endl;
for (int i = 0; i < readArraySize; i++) {
std::cout << (char) readData[i];
}

uart.CLOSE();

所以我找到了问题的原因。我的termios代码是正确的,我的信号看起来和我们预期的差不多,但我们为设备使用的命令启动了一个流,每次我们想读取时,我们都会打开它。谢谢大家的帮助。