免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2013 | 回复: 4

熟悉串口编程的,来帮个忙。。。 [复制链接]

论坛徽章:
0
发表于 2007-04-06 14:58 |显示全部楼层
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <stdio.h>

  6. #define BAUDRATE B38400
  7. #define MODEMDEVICE "/dev/ttyS0"
  8. #define _POSIX_SOURCE 1
  9. #define FALSE 0
  10. #define TRUE 1

  11. volatile int STOP=FALSE;

  12. int main()
  13. {
  14.     int fd,res;
  15.     char buf[10];
  16.     char buf1[10]= "hellozhang";
  17.     struct termios oldtio,newtio;
  18.     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  19.     if (fd < 0)
  20.     {
  21.         perror(MODEMDEVICE);
  22.         exit(-1);
  23.     }

  24.     tcgetattr(fd,&oldtio);
  25.     bzero(&newtio, sizeof(newtio));

  26.     newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  27.     newtio.c_iflag = IGNPAR;
  28.     newtio.c_oflag = 0;

  29.     newtio.c_lflag = 0;

  30.     newtio.c_cc[VTIME]    = 0;
  31.     newtio.c_cc[VMIN]     = 5;
  32.     tcflush(fd, TCIFLUSH);
  33.     tcsetattr(fd,TCSANOW,&newtio);
  34.     while (STOP==FALSE)
  35.     {
  36.         res = write(fd, buf1, 8);
  37.         printf("Result. res = %d, i = %d\n", res, i);

  38.     }
  39.     tcsetattr(fd,TCSANOW,&oldtio);
  40.     return 0;
  41. }
复制代码


就在那个while循环里面,现在是运行到write的时候就停在那里不走了,不知道怎么回事。

环境:linux AS 3

[ 本帖最后由 yourantianya 于 2007-4-6 16:37 编辑 ]

论坛徽章:
0
发表于 2007-04-06 15:19 |显示全部楼层
补充:有时可以循环多次然后停顿,如512次。
有时一次也走不了就停了,什么原因?

[ 本帖最后由 yourantianya 于 2007-4-6 16:51 编辑 ]

论坛徽章:
0
发表于 2007-04-06 18:22 |显示全部楼层
char buf1[10]= "hellozhang";
一看就内存越界

论坛徽章:
0
发表于 2007-04-06 23:20 |显示全部楼层
网上的例子:看了应该懂   关键是termios结构的设置,man tcgetattr

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

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


  11. int speed_arr[]={ B38400, B19200, B9600, B4800, B2400, B1200, B300,
  12.             B38400, B19200, B9600, B4800, B2400, B1200, B300,};
  13. int name_arr[]={38400,  19200,  9600,  4800,  2400,  1200,  300,
  14.             38400,  19200,  9600, 4800, 2400, 1200,  300};
  15. void set_speed(int fd,int speed)
  16. {
  17. struct termios    term;
  18. int               i = 0;
  19. int               status = 0;



  20. tcgetattr(fd,&term);//取得属性,终端也是文件,只是一种特殊的文件
  21. for(i; i<sizeof(speed_arr)/sizeof(speed_arr[0]);i++)
  22. {  
  23.      if(speed == name_arr[i])
  24.      {
  25.       tcflush(fd,TCIOFLUSH);
  26.       cfsetispeed(&term,speed_arr[i]);
  27.       cfsetospeed(&term,speed_arr[i]);
  28.         
  29.        if(tcsetattr(fd,TCSANOW,&term) == -1);//只要any成功用tcgetattr再作判断
  30.       {
  31.           printf("tcsetattr error,allofthem not set:%s\n",strerror(errno));
  32.           exit(errno);
  33.           }

  34.       tcfflush(fd,TCIOFLUSH);
  35.      }  
  36.    
  37.    }

  38. }



  39. int set_parity(int fd,int databits,int stopbits,int parity)
  40. {
  41.        struct   termios   term;
  42.        if(tcgetattr(fd,&term) == -1)
  43.        {
  44.         printf("tcgetattr error:%s\n",strerror(errno));
  45.         exit(errno);
  46.         }


  47.       term.c_cflag &= ~CSIZE;//character size  mask

  48.      switch(databits)
  49.         {  
  50.                  case 7:
  51.                    term.c_cflag |= CS7;
  52.                   break;
  53.             case 8:
  54.                 term.c_cflag |= CS8;
  55.                 break;
  56.          default:
  57.                 fprintf(stderr,"Unsupported data size\n");
  58.                 return (FALSE);
  59.         }


  60.      switch(stopbits)
  61.        {
  62.                   case 1:
  63.                   term.c_cflag &= ~CSTOPB;
  64.                 break;
  65.           case 2:
  66.                 term.c_cflag |= CSTOPB;
  67.                 break;
  68.           default:
  69.                 fprintf(stderr,"Unsupported stop bits\n");
  70.                 return (FALSE);
  71.         }

  72.    switch(parity)
  73.       {
  74.                 case 'n':
  75.         case 'N':
  76.                 term.c_cflag &= ~PARENB;   /* Clear parity enable */
  77.                 term.c_iflag &= ~INPCK;     /* Enable parity checking */
  78.                 break;
  79.         case 'o':
  80.         case 'O':
  81.                 term.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/
  82.                 term.c_iflag |= INPCK;             /* Disnable parity checking */
  83.                 break;
  84.         case 'e':
  85.         case 'E':
  86.                 term.c_cflag |= PARENB;     /* Enable parity */
  87.                 term.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
  88.                 term.c_iflag |= INPCK;       /* Disnable parity checking */
  89.                 break;
  90.         case 'S':
  91.         case 's':  /*as no parity*/
  92.                 term.c_cflag &= ~PARENB;
  93.                 term.c_cflag &= ~CSTOPB;
  94.                 break;
  95.         default:
  96.                 fprintf(stderr,"Unsupported parity\n");
  97.                 return (FALSE);
  98.          }
  99.   if (parity != 'n')
  100.           term.c_iflag |= INPCK;
  101.     term.c_cc[VTIME] = 150; // 15 seconds
  102.     term.c_cc[VMIN] = 0;

  103.   tcflush(fd,TCIFLUSH); /* Update the term and do it NOW */
  104.   if (tcsetattr(fd,TCSANOW,&term) != 0)
  105.   {
  106.     perror("SetupSerial 3");
  107.     return (FALSE);
  108.    }

  109.    return (TRUE);
  110. }



  111. int OpenDev(char *Dev)
  112. {
  113.   int        fd;
  114.   fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
  115.    if (-1 == fd)
  116.     { /*设置数据位数*/
  117.       perror("Can't Open Serial Port");
  118.       return -1;
  119.     }
  120.   else
  121.      fcntl(fd,F_SETFL,0);
  122.            
  123.   return fd;
  124. }


  125. int main(int argc, char **argv)
  126. {
  127.         int fd;
  128.         int nread;
  129.         char buff[512];
  130.         char *dev ="/dev/ttyS1";
  131.         fd = OpenDev(dev);
  132.         if (fd>0)   
  133.                      set_speed(fd,19200);
  134.         else
  135.         {
  136.           printf("Can't Open Serial Port!\n");
  137.           exit(0);
  138.         }
  139.       if (set_parity(fd,8,1,'N')== FALSE)
  140.       {
  141.         printf("Set Parity Error\n");
  142.         exit(1);
  143.       }
  144.      while(1)
  145.           {
  146.                    while((nread = read(fd,buff,512))>0)
  147.                    {
  148.                       printf("\nLen %d\n",nread);
  149.                       buff[nread+1]='\0';
  150.                       printf("\n%s",buff);
  151.                     }
  152.           }
  153.     //close(fd);
  154.     //exit(0);
  155. }
复制代码

[ 本帖最后由 lsmup 于 2007-4-6 23:59 编辑 ]

论坛徽章:
0
发表于 2007-04-07 09:57 |显示全部楼层
好的,看看

不过这个数组的问题应该不是导致程序停止的原因,我用别的值也试过的,测试的结果不确定。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP