LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1003|回复: 2

串口怪问题

[复制链接]
发表于 2007-1-26 15:35:17 | 显示全部楼层 |阅读模式
串口怪问题




  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>

  9. #define FALSE  -1
  10. #define TRUE   0


  11. int OpenDev(char *Dev)
  12. {
  13.         int        fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY        
  14.         if (-1 == fd)        
  15.         {                        
  16.                 perror("Can't Open Serial Port");
  17.                 return -1;               
  18.         }        
  19.         else        
  20.                 return fd;
  21. }
  22. int main(int argc, char **argv)
  23. {
  24.         int fd, i = 10, attrfd;
  25.         int nwrite;
  26.         static struct termios terattr;
  27.         char buff[512] = "hello world";
  28.         char *dev  = "/dev/ttyS0";
  29.         fd = OpenDev(dev);
  30.         printf("fd == %d\n", fd);
  31.         if(-1 == fd)
  32.                 return -1;
  33.         attrfd = tcgetattr(fd, &terattr);
  34.         if(-1 == attrfd)
  35.                 perror("attrfd");
  36.         printf("iflag == %d\n", terattr.c_iflag);
  37.         printf("oflag == %d\n", terattr.c_oflag);
  38.         printf("cflag == %d\n", terattr.c_cflag);
  39.         printf("lflag == %d\n", terattr.c_lflag);
  40.                
  41.                
  42. }
复制代码


运行输出



  1. fd == 3
  2. attrfd: Input/output error
  3. iflag == 134513255
  4. oflag == 134519244
  5. cflag == 10906172
  6. lflag == 12074996

复制代码

疑问:
1)既然串口能打开,那么tcgetattr怎么会出错呢,一般是什么问题引起的?是不是驱动有问题?
2)既然tcgetattr出错,为什么还能或得串口属性(属性是不是错误的?还没验证)
发表于 2007-3-14 00:31:46 | 显示全部楼层
1、不清楚,俺的没出错
2、显示出来的“属性”不是tcgetattr得到的,可能是随机的,可以在tcgetattr调用之前显示一遍做对比
回复 支持 反对

使用道具 举报

发表于 2007-3-15 18:59:21 | 显示全部楼层
在哪里运行?在ioctl里打些东西,看看TCGETS的返回。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表