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) != )
return -; return ;
} static __inline int tty_set(void)
{ if ( tcgetattr(STDIN_FILENO, &ori_attr) )
return -; 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] = ;
cur_attr.c_cc[VTIME] = ; if (tcsetattr(STDIN_FILENO, TCSANOW, &cur_attr) != )
return -; return ;
} 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(, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = ;
tv.tv_usec = ; retval = select(, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */ if (retval == -) {
perror("select()");
return ;
} else if (retval)
return ;
/* FD_ISSET(0, &rfds) will be true. */
else
return ;
return ;
} int main(int argc, char*argv[]) { //非阻塞getChar初始化
int tty_set_flag;
tty_set_flag = tty_set(); char com_num[] = {};
if (argc < ) {
printf("please enter com num:\n");
scanf("%c", &com_num);
} else {
// com_num = *argv[1];
strcpy(com_num, argv[]);
} #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[] = {};
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[] = {};
sprintf(fileName,"readData_dir/%s.txt",com_num);
pFile = fopen (fileName, "w"); char Rbuff[] = {};
int nsize = ;
int i = ;
int nCnt = ;
while () { /*********************发送测试*******************************/
// 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, , );
nsize = m_pComUart->GetData(Rbuff, , );
if (nsize > )
{
printf("nsize:%d\n", nsize); // if(nCnt==10){
for (i = ; i < nsize; i++)
{ // printf("%02x ", (unsigned char)Rbuff[i]); fprintf(pFile, "%02x", (unsigned char)Rbuff[i]);
if (nCnt == ) {
fprintf(pFile, "\n");
nCnt = ;
} 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 == )
tty_reset(); }
linux C之getchar()非阻塞方式的更多相关文章
- socket设置为非阻塞方式(windows和linux)
Windows用以下方法将socket设置为非阻塞方式 : unsigned long ul=1; SOCKET s=socket(AF_INET,SOCK_STREAM,0); int ret=io ...
- Linux NIO 系列(03) 非阻塞式 IO
目录 一.非阻塞式 IO 附:非阻塞式 IO 编程 Linux NIO 系列(03) 非阻塞式 IO Netty 系列目录(https://www.cnblogs.com/binarylei/p/10 ...
- 非阻塞方式connect编程
参考博客: ①setsockopt()函数使用详解:http://blog.csdn.net/tody_guo/article/details/5972588 ②setsockopt :SO_LING ...
- 用WINSOCK API实现同步非阻塞方式的网络通讯
Option Base 0Option Explicit '* ************************************************** *'* 模块名称:Winsock ...
- linux c编程:非阻塞I/O
通常来说,从普通文件读数据,无论你是采用 fscanf,fgets 也好,read 也好,一定会在有限的时间内返回.但是如果你从设备,比如终端(标准输入设备)读数据,只要没有遇到换行符(‘\n’),r ...
- linux centos 安装Jenkins(非docker方式)
写在前面 我之前写过Asp.net Core 使用Jenkins + Dockor 实现持续集成.自动化部署(一):Jenkins安装这jenkisn的安装过程,但这篇使用的是docker的方式安装的 ...
- select应用于read函数 超时非阻塞方式
/* * "Timed" read - timout specifies the # of seconds to wait before * giving up (5th argu ...
- Linux设备驱动中的阻塞和非阻塞I/O
[基本概念] 1.阻塞 阻塞操作是指在执行设备操作时,托不能获得资源,则挂起进程直到满足操作所需的条件后再进行操作.被挂起的进程进入休眠状态(不占用cpu资源),从调度器的运行队列转移到等待队列,直到 ...
- linux下socket connect 阻塞方式 阻塞时间控制
同事今天问我,如何在linux下的c代码里面控制connect的阻塞时间.应用的背景是:linux下的c程序有两个目标IP需要connect,如果用阻塞方式,当其中一个IP不能连接的情况下,程序将阻塞 ...
随机推荐
- 05.K米评测
体验产品:K米Android版本 软件版本:4.3.0 设备型号:魅蓝note2 操作系统: Android5.1 体验时间:2016.10.15-2016.10.15 体验人:黄瑞钰(0314025 ...
- jsf primefaces note
对应cdi,inject的变量,必须使用getter才能访问的值. @Inject DateView dateView; dateView.getFromDate1() 这样是娶不到的: dateVi ...
- primefaces p:tableData 显示 List<List>
@javax.faces.bean.ViewScoped public class Controlador { private List<List> estadistico; @PostC ...
- python 安装包总结
PIL安装(Centos6.6) 1. 安装PIL所需的系统库 (centos6.6)yum install zlib zlib-devel -yyum install libjpeg libjpeg ...
- UI学习之常用方法(续)
UIView 1. UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(100,100, 100, 30)]; [vv.layer setB ...
- android语音识别和合成第三方 .
讯飞语音云 http://open.voicecloud.cn/index.php 目前支持6大类型的SDK下载,包括Android. iPhone平台移动应用的接入,Windows.Linux平台P ...
- Java工厂设计模式
程序在接口和子类之间加入一个过渡类,通过此过渡类端取得接口的实例化对象,一般都会称这个过渡端为工厂类 //=============================================== ...
- 关于Linux发行版的选择
Linux发行版很多,分为以RedHat为代表的商业发行版和以Debian为代表的免费发行版.前者典型版本有CentOS.Fedora.SUSE等,后者的典型版本有Ubuntu等 CentOS.Ubu ...
- (转)Java中的String为什么是不可变的
转自:http://www.importnew.com/7440.html String是所有语言中最常用的一个类.我们知道在Java中,String是不可变的.final的.Java在运行时也保存了 ...
- 对二进制加密(分散保存-s=sy+a+b)
#include <stdio.h> #define L 40 void jiaM(int * s,int * a,int *b,int *sy); void jieM(int * a,i ...