【发布时间】:2013-04-04 08:00:13
【问题描述】:
我正在使用 PL2303 驱动程序通过 USB 连接作为串行端口读取数据。它在执行open 以及设置它们的 TTY 选项和非阻塞时成功返回。当我尝试关闭连接时,它挂起。在这种状态下,它读取“�”而不是字符。
我可以用cutecom完美地连接到设备。这是奇怪的部分:
- 如果我首先通过cutecom(串行监视器)连接到设备,之后我的程序每次都可以正常连接和关闭。它读取字符,因为我希望它们被读取。 (没有 �)。
- 如果我断开并重新连接硬件,我的程序将再次挂起,直到我运行 cutecom。
因为在我使用cutecom 后它可以工作,它让我觉得我在初始连接或连接设置中遗漏了一些东西。这是我用来连接的:
baud_rate = 38400;
fd = open (device_path, O_RDONLY | O_NOCTTY );
在我的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;
}
在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;
}
【问题讨论】:
-
我发现您的代码存在 2 个问题。首先是
set_tty_options函数似乎没有完全初始化tty_options结构。这可以解释“如果我先运行 X,我的程序可以工作,但如果单独运行,则会挂起/失败”。这是程序没有正确或完全初始化其环境的典型症状。其次,set_blocking函数对于规范输入是虚假的。c_cc[VMIN]和c_cc[VTIME]只能用于非规范(又名原始)输入。对于规范输入的非阻塞read(),请使用fcntl()进行设置。
标签: c linux serial-port