现在我正在通过 Udoo Neo 上的 C++ 中的 UART 与设备通信。为此,我使用 termios 建立连接并将数据写入设备。为此,我想使用硬件流控制并使用 termios 设置标志(CRTSCTS)。
对于硬件流控制,我已将设备 RTS 线连接到板 CTS,并且我还通过示波器检查,如果设备还没有准备好读取,它会给我一个高电平有效。
问题是,在下面的示例中,我仍然丢失了字节,只是用数字向设备发送垃圾邮件,但电路板输出表明一切都正确写入。
我以为使用硬件流控时UART会被阻塞,这样就不会丢失任何信息。我没有正确理解这一点 - 还是代码中有错误?
谢谢您的帮助
const char dev[] = "/dev/ttymxc4";
int main(int argc, char **argv) {
int fd;
struct termios t; ///< control structure for a general asynchronous interface
// edited code
tcgetattr(fd, &t);
t.c_iflag &= ~(IGNBRK | BRKINT | ICRNL |
INLCR | PARMRK | INPCK | ISTRIP | IXON);
t.c_oflag = 0;
t.c_cflag &= ~(CSIZE | PARENB);
t.c_cflag |= (CS8 | CRTSCTS);
// edited code
t.c_cflag |= (CLOCAL | CREAD);
t.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
t.c_cc[VMIN] = 0;
t.c_cc[VTIME] = 0;
cfsetispeed(&t,B57600); /* normal shall be: B115200 Baud */
fd = ::open(dev, O_RDWR);
if (fd==-1) {
printf("UART: cannot open file: %s\n",dev);
return -1;
}
tcsetattr(fd,TCSANOW, &t);
// edited code
fcntl(fd, F_SETFL, 0);
int count = 0;
while (true) {
count++;
std::stringstream output;
output << count << ",";
::write(fd, output.str().c_str(), output.str().length());
printf("%d, writing: %s\n", fd, output.str().c_str());
usleep(10000);
}
return 0;
}