免费注册 查看新帖 |

Chinaunix

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

一个串口编程的问题,只能写不能读 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-07-31 19:22 |只看该作者 |倒序浏览
int main(int argc, char **argv){
int fd;
int nread;
char buff[512];
char *dev  = "/dev/ttyS1"; //串口二
fd = OpenDev(dev);
set_speed(fd,19200);
if (set_Parity(fd,8,1,'N') == FALSE)  {
printf("Set Parity Error\n";
exit (0);
}
while (1) //循环读取数据
{   
while((nread = read(fd, buff, 512))>;0)
{
printf("\nLen %d\n",nread);
buff[nread+1] = '\0';   
printf( "\n%s", buff);   
}
}
//close(fd);  
// exit (0);
}
这是执行读操作的主函数代码,请各位熟悉串口编程的大侠多多指教

论坛徽章:
0
2 [报告]
发表于 2003-07-31 20:37 |只看该作者

一个串口编程的问题,只能写不能读

不是很熟悉
只能说说自己的理解

1 参数有没有设置对 串中有很多选项
2 有没有数据接收到

论坛徽章:
0
3 [报告]
发表于 2003-07-31 20:37 |只看该作者

一个串口编程的问题,只能写不能读

另外以前他们也有问过串口的
你可以查查看他们的例子

精华中说不定也有

论坛徽章:
0
4 [报告]
发表于 2003-07-31 21:16 |只看该作者

一个串口编程的问题,只能写不能读

参数不对的话,写数据的时候应该也会有问题啊,但写数据的时候很正常的说,将代码改成这样输出是这样
3
hello
请教大家,为什么printf("%d\n",nread);这一行没有输出呢,即使read失败也应该输出-1,实在是搞不懂
int main(int argc, char **argv)
{
        int fd;
        int nread;
        char buff[512];
        char *dev ="/dev/ttyS1";
        fd = OpenDev(dev);
        printf ("%d\n", fd);
        if (fd>;0)
    set_speed(fd,19200);
        else
                {
                printf("Can't Open Serial Port!\n";
                exit(0);
                }
  if (set_Parity(fd,8,1,'N')== FALSE)
  {
    printf("Set Parity Error\n";
    exit(1);
  }
  while(1)
          {
                        printf ("hello\n";
                        nread = read (fd, buff, ;
                        printf("%d\n",nread);
          }
    //close(fd);
    //exit(0);
}

论坛徽章:
0
5 [报告]
发表于 2003-07-31 21:27 |只看该作者

一个串口编程的问题,只能写不能读

那你对照一下别人的串口程序吧
mengwq前几天发过一个串口程序

你可以参考一下

论坛徽章:
0
6 [报告]
发表于 2003-07-31 22:09 |只看该作者

一个串口编程的问题,只能写不能读

mengwq?
没有这个用户啊,论坛里的帖子我都搜过了,不过好像讨论最终都以没有结果告终,斑竹能不能帮我确认一下,如果正常的话,对于上面的程序,nread是read的返回值,无论如何
printf("%d\n",nread);这一句都应该会有输出,谢谢。

论坛徽章:
0
7 [报告]
发表于 2003-07-31 22:18 |只看该作者

一个串口编程的问题,只能写不能读

就是论坛FTP提供者

串口我是没有写过
但是他们的程序有的错不是在接收的
而是在其它地方的

看看

论坛徽章:
0
8 [报告]
发表于 2003-07-31 22:21 |只看该作者

一个串口编程的问题,只能写不能读

你的输入比如说程序名叫t
运行的时候这么输入的嘛?
./t helloworld

论坛徽章:
0
9 [报告]
发表于 2003-07-31 22:24 |只看该作者

一个串口编程的问题,只能写不能读

你的serial有数据输入吗?没有的话是没有数据的
IBM developerWorks上有文章,下面是代码

  1. #include     <stdio.h>;      /*标准输入输出定义*/
  2. #include     <stdlib.h>;     /*标准函数库定义*/
  3. #include     <unistd.h>;     /*Unix标准函数定义*/
  4. #include     <sys/types.h>;  /**/
  5. #include     <sys/stat.h>;   /**/
  6. #include     <fcntl.h>;      /*文件控制定义*/
  7. #include     <termios.h>;    /*PPSIX终端控制定义*/
  8. #include     <errno.h>;      /*错误号定义*/

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


  11. /***@brief  设置串口通信速率
  12. *@param  fd     类型 int  打开串口的文件句柄
  13. *@param  speed  类型 int  串口速度
  14. *@return  void*/

  15. int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
  16.             B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  17. int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,
  18.             38400,  19200,  9600, 4800, 2400, 1200,  300, };

  19. void set_speed(int fd, int speed)
  20. {
  21.   int   i;
  22.   int   status;
  23.   struct termios   Opt;
  24.   tcgetattr(fd, &Opt);
  25.   for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
  26.    {
  27.            if  (speed == name_arr[i])
  28.            {
  29.                tcflush(fd, TCIOFLUSH);
  30.             cfsetispeed(&Opt, speed_arr[i]);
  31.             cfsetospeed(&Opt, speed_arr[i]);
  32.             status = tcsetattr(fd, TCSANOW, &Opt);
  33.             if  (status != 0)
  34.             perror("tcsetattr fd1");
  35.              return;
  36.              }
  37.    tcflush(fd,TCIOFLUSH);
  38.    }
  39. }
  40. /**
  41. *@brief   设置串口数据位,停止位和效验位
  42. *@param  fd     类型  int  打开的串口文件句柄*
  43. *@param  databits 类型  int 数据位   取值 为 7 或者8*
  44. *@param  stopbits 类型  int 停止位   取值为 1 或者2*
  45. *@param  parity  类型  int  效验类型 取值为N,E,O,,S
  46. */
  47. int set_Parity(int fd,int databits,int stopbits,int parity)
  48. {
  49.         struct termios options;
  50. if  ( tcgetattr( fd,&options)  !=  0)
  51.   {
  52.           perror("SetupSerial 1");
  53.           return(FALSE);
  54.   }
  55.   options.c_cflag &= ~CSIZE;
  56.   switch (databits) /*设置数据位数*/
  57.   {
  58.           case 7:
  59.                   options.c_cflag |= CS7;
  60.                   break;
  61.           case 8:
  62.                 options.c_cflag |= CS8;
  63.                 break;
  64.         default:
  65.                 fprintf(stderr,"Unsupported data size\n");
  66.                 return (FALSE);
  67.         }
  68.   switch (parity)
  69.           {
  70.           case 'n':
  71.         case 'N':
  72.                 options.c_cflag &= ~PARENB;   /* Clear parity enable */
  73.                 options.c_iflag &= ~INPCK;     /* Enable parity checking */
  74.                 break;
  75.         case 'o':
  76.         case 'O':
  77.                 options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/
  78.                 options.c_iflag |= INPCK;             /* Disnable parity checking */
  79.                 break;
  80.         case 'e':
  81.         case 'E':
  82.                 options.c_cflag |= PARENB;     /* Enable parity */
  83.                 options.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
  84.                 options.c_iflag |= INPCK;       /* Disnable parity checking */
  85.                 break;
  86.         case 'S':
  87.         case 's':  /*as no parity*/
  88.                 options.c_cflag &= ~PARENB;
  89.                 options.c_cflag &= ~CSTOPB;
  90.                 break;
  91.         default:
  92.                 fprintf(stderr,"Unsupported parity\n");
  93.                 return (FALSE);
  94.                 }
  95.   /* 设置停止位*/   
  96.   switch (stopbits)
  97.           {
  98.           case 1:
  99.                   options.c_cflag &= ~CSTOPB;
  100.                 break;
  101.         case 2:
  102.                 options.c_cflag |= CSTOPB;
  103.                 break;
  104.         default:
  105.                 fprintf(stderr,"Unsupported stop bits\n");
  106.                 return (FALSE);
  107.         }
  108.   /* Set input parity option */
  109.   if (parity != 'n')
  110.                   options.c_iflag |= INPCK;
  111.     options.c_cc[VTIME] = 150; // 15 seconds
  112.     options.c_cc[VMIN] = 0;

  113.   tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  114.   if (tcsetattr(fd,TCSANOW,&options) != 0)
  115.           {
  116.                   perror("SetupSerial 3");
  117.                 return (FALSE);
  118.         }
  119.   return (TRUE);
  120. }
  121. /**
  122. *@breif 打开串口
  123. */
  124. int OpenDev(char *Dev)
  125. {
  126. int        fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
  127.         if (-1 == fd)
  128.                 { /*设置数据位数*/
  129.                         perror("Can't Open Serial Port");
  130.                         return -1;
  131.                 }
  132.         else
  133.         return fd;

  134. }
  135. /**
  136. *@breif         main()
  137. */
  138. int main(int argc, char **argv)
  139. {
  140.         int fd;
  141.         int nread;
  142.         char buff[512];
  143.         char *dev ="/dev/ttyS1";
  144.         fd = OpenDev(dev);
  145.         if (fd>;0)
  146.     set_speed(fd,19200);
  147.         else
  148.                 {
  149.                 printf("Can't Open Serial Port!\n");
  150.                 exit(0);
  151.                 }
  152.   if (set_Parity(fd,8,1,'N')== FALSE)
  153.   {
  154.     printf("Set Parity Error\n");
  155.     exit(1);
  156.   }
  157.   while(1)
  158.           {
  159.                    while((nread = read(fd,buff,512))>;0)
  160.                    {
  161.                       printf("\nLen %d\n",nread);
  162.                       buff[nread+1]='\0';
  163.                       printf("\n%s",buff);
  164.                     }
  165.           }
  166.     //close(fd);
  167.     //exit(0);
  168. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2003-07-31 22:49 |只看该作者

一个串口编程的问题,只能写不能读

我的代码其实和你贴的来自同一地方,只能写不能读,写的数据我已经包含在代码里了,不是命令行输入的,和windows下的程序测试,只能写不能读。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP