首页 > 代码库 > linux C之getchar()非阻塞方式
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()非阻塞方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。