免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1335 | 回复: 2
打印 上一主题 下一主题

串口怪问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-27 11:07 |只看该作者 |倒序浏览
串口怪问题



  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出错,为什么还能或得串口属性(属性是不是错误的?还没验证)

论坛徽章:
0
2 [报告]
发表于 2007-01-27 14:06 |只看该作者
你的程序在我的机器上实验了一下, 没有问题, 输出结果如下:
fd == 3
iflag == 1280
oflag == 5
cflag == 3261
lflag == 35387
我看了一下, 在我的机器上/dev/ttyS0这个设备是存在的. 我猜在LZ的机器上没有!
既然tcgetattr()出错, 打印terattr.c_iflag等参数的值就没有任何意义了

论坛徽章:
0
3 [报告]
发表于 2007-01-29 08:58 |只看该作者
原帖由 jackalchen 于 2007-1-27 14:06 发表
你的程序在我的机器上实验了一下, 没有问题, 输出结果如下:
fd == 3
iflag == 1280
oflag == 5
cflag == 3261
lflag == 35387
我看了一下, 在我的机器上/dev/ttyS0这个设备是存在的. 我猜在LZ的机器上没有! ...

/dev/ttyS0这个设备在我机器上也是存在的,是通过PCI槽扩展出来的
我只是觉得奇怪,怎么打开设备能成功,为什么tcgetattr怎么会出错?且能获错误的属性
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP