我目前正在编写一个程序,需要通过UART接口与AT接口通信(操作系统是Linux)。但我在使用时遇到问题select()
在文件描述符上。由于某种原因 select 不认为文件描述符已准备好读取,但为了缩小问题范围,我使用了以下程序。
int main()
{
char buffer[BSIZE];
fd_set rfds;
int ret;
struct termios cnf;
struct timeval tv;
fd = open("/dev/ttyO1", O_RDWR);
// Have also tried to set fd = 0 for stdin, as a reference
signal(SIGINT, sig_handler);
tcgetattr(fd, &cnf);
old = cnf;
cfmakeraw(&cnf);
tcsetattr(fd, TCSANOW, &cnf);
while (1) {
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
write(fd, "AT\r", 3);
ret = select(fd+1, &rfds, NULL, NULL, &tv);
printf("ret = %d\n", ret);
if (ret < 0) {
perror("select()");
}
else {
ret = read(fd, buffer, BSIZE-1);
buffer[ret] = '\0';
printf("read: \"%s\"\n", buffer);
}
}
return 0;
}
运行看起来像这样
root@linux:~# ./stuff
ret = 0
read: "AT
OK
"
这表明select认为没有数据,但尝试读取时有数据。这对我来说似乎很奇怪。另外,我尝试用 stdin 交换 tty,效果很好。
该代码正在 Texas Instruments EZSDK PSP 内核上运行,但这不应该是问题。另外,stty
设置如下所示
root@linux:~# stty -aF /dev/ttyO1
speed 9600 baud; rows 24; columns 80;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany -imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
我是否错过了一些重要的标志open()
?或者可能需要使用设置一些设置termios
?这种方法需要一些特殊的硬件吗
编辑:
尝试运行该程序时我遇到了同样的问题/dev/ttyUSB0
,这也恰好是一个AT接口。在我看来是这样的tty
有关的。
将 fd 更改为我实际使用和想知道的内容。