LinuxSir.cn,穿越时空的Linuxsir!

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

请教:如何使用ioctl()控制波特率等参数?

[复制链接]
发表于 2002-12-7 14:26:35 | 显示全部楼层 |阅读模式
请教各位高手:我如何使用ioctl()控制串行口波特率,起始位,数据位,停止位,校验位等参数,也就是控制这些参数的ioctl()的参数是什么?
发表于 2002-12-7 18:46:00 | 显示全部楼层
举一个例子,参考一下。:-)

  1. #include <sys/ioctl.h>
  2. #include <string.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <termio.h>
  8. #include <sys/time.h>

  9. int fd;
  10. char writebuf[128];
  11. char readbuf[64];

  12. void flush232();
  13. int writegpib();

  14. void flush232()
  15. {
  16. printf("in flush232\n");
  17. ioctl(fd,TCFLSH, 0);
  18. ioctl(fd,TCFLSH, 1);
  19. }

  20. int writegpib()
  21. {
  22. int n;
  23. int retry =0;

  24. printf("in writegpib writing %s\n",writebuf);
  25. printf("length of writebuf: %d\n",strlen(writebuf));
  26. do {
  27.         n = write(fd,writebuf, strlen(writebuf));
  28.         printf ("error = %d\n",errno);
  29.         printf("wrote %d characters to %d\n",n,fd);
  30. } while ((n<=0) && retry++ < 5);
  31. retry = 0;
  32. do {
  33.         n = write(fd,"\r",1);
  34.         printf ("error = %d\n",errno);
  35.         printf("wrote %d characters to %d\n",n,fd);
  36. } while ((n<=0) && retry++ < 5);

  37. if (n<0) n = 0;
  38. return n;

  39. }

  40. void
  41. delay(int t) {
  42. struct timeval tp,to;
  43. struct timezone tzp;

  44. printf("in delay for %d\n",t);

  45. if (gettimeofday(&tp,&tzp) == 0) {
  46.         to.tv_usec=tp.tv_usec+t;
  47.         to.tv_sec=tp.tv_sec+(to.tv_usec/1000000);
  48.         to.tv_usec%=1000000;
  49.         while (tp.tv_sec < to.tv_sec) {
  50.                 gettimeofday(&tp,&tzp);
  51.         }
  52.         while (tp.tv_usec < to.tv_usec && tp.tv_sec == to.tv_sec) {
  53.                 gettimeofday(&tp,&tzp);
  54.         }
  55. } else {
  56.         printf("gettimeofday failed\n");
  57. }
  58. }
  59.                
  60. void
  61. main(void)
  62. {

  63. printf ("in  main()\n");
  64. printf ("error = %d\n",errno);

  65. struct termio term;
  66. int stat = 1;
  67. int n;
  68. int retry = 0;
  69. int flags;

  70. do {
  71.         printf("opening serial port\n");
  72.         fd = open("/dev/tty00", O_RDWR|O_NONBLOCK, int(0));
  73.         printf ("error = %d\n",errno);
  74. } while ((fd<=0) && retry++ < 5);

  75. if (fd < 0) {
  76.         printf("failed to open serial port\n");
  77.         stat = 0;
  78. }

  79. if (stat) {
  80.         ioctl(fd,TCGETA, &term);
  81.         printf ("error = %d\n",errno);
  82.         term.c_iflag=(IGNPAR|IGNBRK|IXON|IXOFF);
  83.         term.c_cflag=(B9600|HUPCL|CREAD|CS8);
  84.         term.c_oflag=0;
  85.         term.c_lflag=0;
  86.         term.c_cc[4]=100;
  87.         term.c_cc[5]=1;
  88.         ioctl(fd,TCSETA, &term);
  89.         printf ("error = %d\n",errno);
  90.         ioctl(fd,TCFLSH, 2);
  91.         printf ("error = %d\n",errno);
  92. }

  93. if (stat) {
  94.         flush232();
  95.         sprintf(writebuf,"id");
  96.         if (!writegpib()) stat=0;
  97.         delay(2000000);
  98.         do {
  99.                 printf("reading from fd(%d)\n",fd);
  100.                 n = read(fd,readbuf,56);
  101.                 printf ("error = %d\n",errno);
  102.                 printf("read %d from fd(%d)\n",n,fd);
  103.         } while ((n<=0) && retry++ < 5);

  104.         printf("readbuf = %s\n",readbuf);

  105.         if (n<=0) {
  106.                 stat=0;
  107.         } else {
  108.                 if (strstr(readbuf,"GPIB")) {
  109.                         printf("got GPIB\n");
  110.                 }
  111.         }
  112. }

  113. if (stat) {
  114.         fcntl(fd,F_GETFL,&flags);
  115.         flags=flags & (~O_NONBLOCK);
  116.         fcntl(fd,F_SETFL,&flags);
  117. } else {
  118.         close(fd);
  119. }
  120. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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