Nat*_*ate 5 c linux serial-port
我通过USB连接读取数据作为PL2303驱动程序的串行端口.它在执行时open以及在我设置TTY选项和非阻塞时成功返回.当我尝试关闭连接时,它会挂起.在这种状态下,它读取" "而不是字符.
我可以用cutecom完美地连接到设备.这是奇怪的部分:
因为它在我使用cutecom后工作,它让我觉得我在初始连接或连接设置中遗漏了一些东西.这是我用来连接的内容:
baud_rate = 38400;
fd = open (device_path, O_RDONLY | O_NOCTTY );
Run Code Online (Sandbox Code Playgroud)
在我的set_tty_options功能:
struct termios tty_options;
memset (&tty_options, 0, sizeof(tty_options));
tcgetattr (fd, &tty_options);
cfsetispeed(&tty_options, baud_rate); // set baud rate
tty_options.c_cflag = (tty_options.c_cflag & ~CSIZE) | CS8; // 8 bit msgs
tty_options.c_cflag |= (CLOCAL | CREAD); // enable reading
tty_options.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty_options.c_cflag |= parity;
tty_options.c_cflag &= ~CSTOPB;
tty_options.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty_options) != 0)
{
printf("error %d from tcsetattr\n", errno);
return TTY_ERROR;
}
Run Code Online (Sandbox Code Playgroud)
在set_blocking功能:
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tggetattr", errno);
return FAILURE;
}
// 0 or 1 byte is enough to return from read
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d setting term attributes", errno);
return FAILURE;
}
Run Code Online (Sandbox Code Playgroud)
这就是我最终所做的。我基本上是通过复制和粘贴Cutecom源代码中的部分来解决这个问题的。
打开时...
int fd, n;
fd = open (device_path, O_RDONLY | O_NOCTTY | O_NDELAY);
... error check fd ...
n = fcntl(ail_info->ail_serial_fd, F_GETFL, 0);
fcntl(fd, F_SETFL, n & ~O_NDELAY);
Run Code Online (Sandbox Code Playgroud)你不能像我一样设置波特率。您必须使用定义的B38400;
baud = B38400;
然后,我添加了 wallyk 的答案。
tty_settings.c_lflag = 0;
编辑:根据锯末评论,我找到了一种更好的方法将其设置为原始输入。
tty_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Run Code Online (Sandbox Code Playgroud)
它有效。
| 归档时间: |
|
| 查看次数: |
1781 次 |
| 最近记录: |