|
串口怪问题
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #define FALSE -1
- #define TRUE 0
- int OpenDev(char *Dev)
- {
- int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
- if (-1 == fd)
- {
- perror("Can't Open Serial Port");
- return -1;
- }
- else
- return fd;
- }
- int main(int argc, char **argv)
- {
- int fd, i = 10, attrfd;
- int nwrite;
- static struct termios terattr;
- char buff[512] = "hello world";
- char *dev = "/dev/ttyS0";
- fd = OpenDev(dev);
- printf("fd == %d\n", fd);
- if(-1 == fd)
- return -1;
- attrfd = tcgetattr(fd, &terattr);
- if(-1 == attrfd)
- perror("attrfd");
- printf("iflag == %d\n", terattr.c_iflag);
- printf("oflag == %d\n", terattr.c_oflag);
- printf("cflag == %d\n", terattr.c_cflag);
- printf("lflag == %d\n", terattr.c_lflag);
-
-
- }
复制代码
运行输出
- fd == 3
- attrfd: Input/output error
- iflag == 134513255
- oflag == 134519244
- cflag == 10906172
- lflag == 12074996
复制代码
疑问:
1)既然串口能打开,那么tcgetattr怎么会出错呢,一般是什么问题引起的?是不是驱动有问题?
2)既然tcgetattr出错,为什么还能或得串口属性(属性是不是错误的?还没验证) |
|