星空网 > 软件开发 > 操作系统

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_DEBUGstatic 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()非阻塞方式

关键词:linux

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商ZAFUL如何红遍欧美:无死角拉流量 国内营销玩法全输出:https://www.ikjzd.com/articles/125977
亚马逊测评到底有没有必要:https://www.ikjzd.com/articles/125979
谷歌最新研发项目—移动视频购物平台Shoploop:https://www.ikjzd.com/articles/125981
独立站运营,做好这几点,转化率提高至少10%:https://www.ikjzd.com/articles/125982
独立站卖家如何利用Google广告引流?:https://www.ikjzd.com/articles/125983
亚马逊旺季更要防止账号被冻结!:https://www.ikjzd.com/articles/126
夹江千佛岩景区门票(夹江千佛岩景区门票价格):https://www.vstour.cn/a/411232.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流