zoukankan      html  css  js  c++  java
  • linux C之getchar()非阻塞方式

    参考链接: http://blog.csdn.net/zydlyq/article/details/50963360

    #include "../include/CommUart.h"
    #include "ComCommon.h"
    #include <stdio.h>
    #include <unistd.h>
    #include <iostream>
    #include <stdlib.h>
    
    #include <string.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <termios.h>
    
    using namespace std;
    
    #define USR_DEBUG
    
    
    
    
    static struct termios ori_attr, cur_attr;
    
    static __inline  int tty_reset(void)
    {
       if (tcsetattr(STDIN_FILENO, TCSANOW, &ori_attr) != 0)
          return -1;
    
       return 0;
    }
    
    
    
    
    
    static __inline  int tty_set(void)
    {
    
       if ( tcgetattr(STDIN_FILENO, &ori_attr) )
          return -1;
    
       memcpy(&cur_attr, &ori_attr, sizeof(cur_attr) );
       cur_attr.c_lflag &= ~ICANON;
    //        cur_attr.c_lflag |= ECHO;
       cur_attr.c_lflag &= ~ECHO;
       cur_attr.c_cc[VMIN] = 1;
       cur_attr.c_cc[VTIME] = 0;
    
       if (tcsetattr(STDIN_FILENO, TCSANOW, &cur_attr) != 0)
          return -1;
    
       return 0;
    }
    
    
    
    
    static __inline  int kbhit(void)
    {
    
       fd_set rfds;
       struct timeval tv;
       int retval;
    
       /* Watch stdin (fd 0) to see when it has input. */
       FD_ZERO(&rfds);
       FD_SET(0, &rfds);
       /* Wait up to five seconds. */
       tv.tv_sec  = 0;
       tv.tv_usec = 0;
    
       retval = select(1, &rfds, NULL, NULL, &tv);
       /* Don't rely on the value of tv now! */
    
       if (retval == -1) {
          perror("select()");
          return 0;
       } else if (retval)
          return 1;
       /* FD_ISSET(0, &rfds) will be true. */
       else
          return 0;
       return 0;
    }
    
    
    
    
    
    
    
    
    int main(int argc, char*argv[]) {
    
    
    
    //非阻塞getChar初始化
       int tty_set_flag;
       tty_set_flag = tty_set();
    
       char com_num[3] = {0};
       if (argc < 2) {
          printf("please enter com num:
    ");
          scanf("%c", &com_num);
       } else {
          // com_num = *argv[1];
          strcpy(com_num, argv[1]);
       }
    
    
    #ifdef USR_DEBUG
       printf("main entry
    ");
    #endif // USR_DEBUG
       CComCommon *m_pComUart = new CCommUart;
    #ifdef USR_DEBUG
       printf("create CCommUart
    ");
    #endif // USR_DEBUG
    
    
       char sendStr[256] = {0};
       strcpy(sendStr, "/dev/ttyS100");
       strcat(sendStr, com_num);
       strcat(sendStr, ",115200,8,1,n");
       // m_pComUart->InitComComm("/dev/ttyS1001,115200,8,1,n");
       m_pComUart->InitComComm(sendStr);
    
       // strcat(sendStr,"kkkkkkkkkkkkkkkkkk");
    
    
    //  写入文件
    
       FILE * pFile;
       char fileName[124] = {0};
       sprintf(fileName,"readData_dir/%s.txt",com_num);
       pFile = fopen (fileName, "w");
    
    
    
    
       char Rbuff[256] = {0};
       int nsize = 0;
       int i = 0;
       int nCnt = 0;
       while (1) {
    
          /*********************发送测试*******************************/
          // m_pComUart->SendBuff(buff,sizeof(buff));
          // m_pComUart->SendStr("1234567887654321abcdefgh");
          // m_pComUart->SendStr("kkkkkkkkkkkkkkkkkk");
    
    
          if (kbhit()) {
             const int key = getchar();
             // printf("%c pressed
    ", key);
             if (key == 'q')
                break;
          } /*else {
             fprintf(stderr, "<no key detected>
    ");
          }*/
    
    
    
          /*********************接收的测试案例*************************/
          memset(Rbuff, 0, 256);
          nsize = m_pComUart->GetData(Rbuff, 256, 1000);
          if (nsize > 0)
          {
             printf("nsize:%d
    ", nsize);
    
    // if(nCnt==10){
             for (i = 0; i < nsize; i++)
             {
    
                // printf("%02x ", (unsigned char)Rbuff[i]);
    
                fprintf(pFile, "%02x", (unsigned char)Rbuff[i]);
                if (nCnt == 7) {
                   fprintf(pFile, "
    ");
                   nCnt = 0;
                } else {
                   nCnt++;
                }
    
             }
    
             // 二进制输入
             // fwrite (Rbuff , sizeof(char), sizeof(Rbuff), pFile);
    
    //    nCnt = 0;
    // }else{
    //    nCnt++;
    // }
             printf("
    ");
    
             // printf("%s
    ", Rbuff);
    
          }
    
    
    
          // printf("rec buff data:%s
    ", Rbuff);
          // printf("get char:  %d
    ", m_pComUart->GetChar(10));
    
    
          // sleep(1);
          // usleep(1000);
       }
       delete m_pComUart;
    
       fclose (pFile);
    
    // 线程的操作
       if (tty_set_flag == 0)
          tty_reset();
    
    
    }
  • 相关阅读:
    博客转移到cnblogs
    mmsplayer for ios 支持iphone ,ipad,ipod touch 版本已经完成。
    mmsplayer for ios v1.0
    无ldf文件情况下恢复数据库数据纪实
    我的Blog页面设计
    使用C#生成静态页面
    C++开发工具
    使用JNDI来读取Notes通讯录中的数据
    关于Char与string
    JavaScript站点荟萃
  • 原文地址:https://www.cnblogs.com/hzijone/p/5818483.html
Copyright © 2011-2022 走看看