【发布时间】:2009-10-23 14:39:56
【问题描述】:
我在通过以下方式打开的串行端口读取一些数据时遇到了一些问题。我已经多次使用此代码实例并且一切正常,但现在,由于某种我无法弄清楚的原因,我完全无法从串行端口读取任何内容。
我可以写,并且在另一端正确接收到所有内容,但从未收到回复(正确发送)(不,电缆都正常;))
我用来打开串口的代码如下:
fd = open("/dev/ttyUSB0", O_RDWR | O_NONBLOCK | O_NOCTTY);
if (fd == -1)
{
Aviso("Unable to open port");
return (fd);
}
else
{
//Get the current options for the port...
bzero(&options, sizeof(options)); /* clear struct for new port settings */
tcgetattr(fd, &options);
/*-- Set baud rate -------------------------------------------------------*/
if (cfsetispeed(&options, SerialBaudInterp(BaudRate))==-1)
perror("On cfsetispeed:");
if (cfsetospeed(&options, SerialBaudInterp(BaudRate))==-1)
perror("On cfsetospeed:");
//Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Parity disabled */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable XON XOFF (for transmit and receive)
options.c_cflag |= CRTSCTS; /* enable hardware flow control */
options.c_cc[VMIN] = 0; //min carachters to be read
options.c_cc[VTIME] = 0; //Time to wait for data (tenths of seconds)
//Set the new options for the port...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options)==-1)
{
perror("On tcsetattr:");
}
PortOpen[ComPort] = fd;
}
return PortOpen[ComPort];
端口初始化后,我通过简单的写入命令向它写入一些东西...
int nc = write(hCom, txchar, n);
其中 hCom 是文件描述符(没关系),并且(正如我所说)这有效。但是......当我之后阅读时,我从 errno 收到“资源暂时不可用”错误。
我测试了 select 以查看文件描述符何时有未读取的内容...但它总是超时!
我这样读取数据:
ret = read(hCom, rxchar, n);
我总是得到一个 EAGAIN,但我不知道为什么。
更新:
硬件工作正常!我可以看到串行端口上有入站数据,因为我制作了一条调试电缆来读取另一个终端上发生的事情。所以...
我知道非阻塞应该做什么。我的问题是......为什么没有任何内容被阅读!相同的设置在 Windows 上运行良好,因此所有硬件都运行良好...
这快把我逼疯了!我敢肯定这很简单!我什至尝试摆脱 O_NONBLOCK 看看我什么时候会收到一些东西......但是什么都没有......
【问题讨论】:
-
我也有同样的问题。我可以发送但不能接收(通过 USB-RS232 适配器电缆)。我尝试了另一个具有 RS232 端口的 linux 机器,它工作得很好。我所做的唯一更改是从
/dev/ttyUSB0到/dev/ttyS0。第一台电脑是 Fedora,第二台是 Debian。除此之外,不知道。另一件事。当我关闭 com 程序并重新启动它时,我的程序会读取数据!数据是输入缓冲区,但我的程序不知道。此外,gtkterm 工作正常,所以硬件一切正常。我的程序没有看到 UART 中断。这个 linux h/w 抽象层相当混乱
标签: c linux serial-port