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不能连接的情况下,程序将阻塞 ...
随机推荐
- [JavaEE] NIO与IO的区别
nio是new io的简称,从jdk1.4就被引入了.现在的jdk已经到了1.6了,可以说不是什么新东西了.但其中的一些思想值得我来研究.这两天,我研究了下其中的套接字部分,有一些心得,在此分享. 首 ...
- dubbo 学习总结
1 Dubbo 配置 dubbo配置xml配置 属性配置 注解配置 api配置 注解配置 (+) (#) 服务提供方注解: import com.alibaba.dubbo.config ...
- python编码
一.编码 常见的编码比较: ascii:1个字节 unicode:2个字节 utf-8:英文1个字节,汉字3个字节 二.一个编码问题 问:如下代码设置了在代码中添加了coding: utf-8,但是在 ...
- 云计算之KVM安装(二)
云计算之KVM安装(二) 调整虚拟机 虚拟化Intel使用的是intel VT-X AMD使用的是AMD-V 创建虚拟机步骤 1.准备虚拟机硬盘 2.需要系统iso镜像 3.需要安装一个vnc的客户端 ...
- python从Microsoft Excel文件中导入数据
excel中后缀为csv和xls,二者区别如下:1.xls 文件就是Microsoft excel电子表格的文件格式.2.csv是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. ...
- confluence安装
confluence安装 1.jre安装 java下载http://www.java.com/zh_CN/download/manual.jsp 创建目录和解压缩 mkdir -p /usr/loca ...
- Nginx个人简单理解
首先我们来补充下一些基本知识: 什么是代理服务器? 先举个简单的例子,现在我们在百度访问谷歌的网站,发现现在进不去,这个时候我们可以FQ(关于FQ,可以借鉴下这个博文:http://zhangge.n ...
- js初学—实现checkbox全选功能
布局如下: <p ><input type="checkbox" id="che1"/>全选</p><div id=& ...
- PS------“窗口” -> "扩展功能"使用方法
http://forum.xitek.com/thread-1330039-1-1-1.html
- linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本
手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...