CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Linux/Unix社区 >  程序开发区

在linux下的串口编程中,如何判断串口的缓存是否为空,或者有数?请高手指教!

楼主Bladekiller(幽灵程序员(ghostcoder))2004-09-03 20:47:23 在 Linux/Unix社区 / 程序开发区 提问

在linux下的串口编程中,如何判断串口的缓存是否为空,或者有数?请高手指教! 问题点数:100、回复次数:4Top

1 楼zhjie374(zhjie374)回复于 2004-09-04 15:42:36 得分 30

我做过LINUX下的串口通讯程序!不过好象没有遇到这样的问题!  
  我直接write   read不就可以?Top

2 楼yanghuajia(我要抢分)回复于 2004-09-04 17:18:55 得分 20

用selectTop

3 楼Bladekiller(幽灵程序员(ghostcoder))回复于 2004-09-07 09:09:23 得分 0

开来没有人会,我闪!Top

4 楼yuyuanwei(我是新手)回复于 2004-09-07 14:51:52 得分 50

我直接write   可以的,,,read的时候我碰到问题,,不知道为什么,,,,也来这求教的,,下面就是我试验write的程序,,希望有帮助,,  
  //*******************************************************************  
  //模块编号:    
  //模块名称:   tcpclient.c                                                                                                                  
  //功能描述:   客户端,连接服务器端并向其发送数据      
  //作         者:                                                                                                                                
  //日         期:                                                                                                                
  //版   本   号:                                                                                                                                
  //修改历史:      
  //*******************************************************************  
  #include   <sys/types.h>  
  #include   <sys/socket.h>  
  #include   <netinet/in.h>  
  #include   <unistd.h>  
  #include   <arpa/inet.h>  
  #include   <signal.h>  
  #include   <stdio.h>  
  #include   <stdlib.h>  
  #include   <memory.h>  
  #include   <string.h>  
  #include   <sys/ipc.h>  
  #include   <sys/msg.h>  
  #include   <errno.h>  
  #include   <time.h>  
   
   
  void     timeoutprocess();  
   
  main()  
  {  
      int   sockfd;  
      int     nwritten;  
      int   nbytes;  
      struct   sockaddr_in   addr;  
      char   *buf="hello   world\n";  
       
      if   ((sockfd   =   socket(PF_INET,SOCK_STREAM,0))   <   0)/*创建socket文件描述符*/  
      {  
        herror   ("socket   err");  
        exit   (-1);    
      }  
       
      addr.sin_family   =   AF_INET;  
      addr.sin_port   =   htons   (1111);   /*设置目的端口*/  
      addr.sin_addr.s_addr   =   inet_addr("127.0.0.1");/*设置目的地址*/    
      bzero   (addr.sin_zero,8);  
      if   (connect   (sockfd,   &addr,   sizeof(addr))   <0)   /*连接服务器*/  
      {  
        herror   ("connect   err");  
        close   (sockfd);  
        exit   (-1);  
      }  
      else  
      printf("connected\n");  
       
      nbytes   =   strlen(buf);  
       
      printf("%d\n",   nbytes);  
       
      nwritten   =   writen(sockfd,   buf,   nbytes,   2);  
     
   
   
    if   (nwritten   !=   nbytes)  
            printf("write   fail\n");  
      else  
            printf("write   over\n");  
       
    close   (sockfd);  
  }  
   
  //***********************************************************  
  //函数名     :   writen  
  //功     能     :   向网络连接写n个字节的数据    
  //参     数     :   int   sd   :   系统socket标识      
  //                     unsigned   char   *   ptr:   向网络连接写的内容  
  //                     int   nbyte   :   向网络连接写的字节数  
  //                     nit   timeout   :   超时时间                          
  //返     回     :   0   :   成功   -1   :   失败  
  //日期:  
  //调用的系统库函数:  
  //调用的用户库函数:write(sd,ptr,nleft)  
  //调用的不同源文件的用户函数:  
  //调用的同源文件的用户函数:  
  //涉及的全局变量:    
  //修改历史:  
  //**********************************************************  
   
  int     writen(int   sd,   unsigned   char   *ptr,   int   nbyte,   int   timeout)  
  {  
      int     nleft,   nread;  
   
      nleft   =   nbyte;  
   
      signal(SIGALRM,   timeoutprocess);  
      alarm(timeout);  
   
      while   (nleft   >   0)    
      {  
          nread   =   write(sd,   ptr,   nleft);  
          if   (nread   <=   0)    
          {    
              if   (errno   ==   EINTR)    
              {  
                  signal(SIGALRM,   SIG_IGN);  
                  alarm(0);  
                  return   (nbyte   -   nleft);  
              }  
              signal(SIGALRM,   SIG_IGN);  
              alarm(0);  
              return   (nread);  
          }  
          nleft   -=   nread;  
          ptr   +=   nread;  
      }  
      signal(SIGALRM,   SIG_IGN);  
      alarm(0);  
       
      return   (nbyte   -   nleft);  
  }  
   
  //***********************************************************  
  //函数名     :   timeoutprocess  
  //功     能     :   超时处理  
  //参     数     :    
  //返     回     :    
  //日期:  
  //调用的系统库函数:  
  //调用的用户库函数:  
  //调用的不同源文件的用户函数:  
  //调用的同源文件的用户函数:  
  //涉及的全局变量:    
  //修改历史:  
  //**********************************************************  
   
  void     timeoutprocess()  
  {  
      signal(SIGALRM,   SIG_IGN);  
      alarm(0);  
  }Top

相关问题

  • linux下如何清空串口的输入输出缓存
  • linux 串口处理
  • How to:判断串口的输入缓冲是否有数据?
  • PC 机如何判断串口有数据发来。
  • 如何清除ClientDataSet1的缓存的所有数据?
  • 判断串口的缓冲区有数据的函数是什么。
  • linux里控制串口的资料!?
  • 怎么在LINUX下写串口程序?
  • 急求linux串口通讯的例子.
  • linux下用java访问串口问题

关键词

  • linux
  • 串口
  • include
  • write

得分解答快速导航

  • 帖主:Bladekiller
  • zhjie374
  • yanghuajia
  • yuyuanwei

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo