码迷,mamicode.com
首页 > 系统相关 > 详细

linux C之getchar()非阻塞方式

时间:2016-08-29 17:35:27      阅读:997      评论:0      收藏:0      [点我收藏+]

标签:

参考链接: 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:\n");
      scanf("%c", &com_num);
   } else {
      // com_num = *argv[1];
      strcpy(com_num, argv[1]);
   }


#ifdef USR_DEBUG
   printf("main entry\n");
#endif // USR_DEBUG
   CComCommon *m_pComUart = new CCommUart;
#ifdef USR_DEBUG
   printf("create CCommUart\n");
#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\n", key);
         if (key == q)
            break;
      } /*else {
         fprintf(stderr, "<no key detected>\n");
      }*/



      /*********************接收的测试案例*************************/
      memset(Rbuff, 0, 256);
      nsize = m_pComUart->GetData(Rbuff, 256, 1000);
      if (nsize > 0)
      {
         printf("nsize:%d\n", 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, "\n");
               nCnt = 0;
            } else {
               nCnt++;
            }

         }

         // 二进制输入
         // fwrite (Rbuff , sizeof(char), sizeof(Rbuff), pFile);

//    nCnt = 0;
// }else{
//    nCnt++;
// }
         printf("\n");

         // printf("%s\n", Rbuff);

      }



      // printf("rec buff data:%s\n", Rbuff);
      // printf("get char:  %d\n", m_pComUart->GetChar(10));


      // sleep(1);
      // usleep(1000);
   }
   delete m_pComUart;

   fclose (pFile);

// 线程的操作
   if (tty_set_flag == 0)
      tty_reset();


}

 

linux C之getchar()非阻塞方式

标签:

原文地址:http://www.cnblogs.com/hzijone/p/5818483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!