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不能连接的情况下,程序将阻塞 ...
随机推荐
- [转]Zookeeper原理及应用场景
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...
- ARPSpoofing教程(四) - 捕获并分析数据包
本程序的主要目标是展示如何解析所捕获的数据包的协议首部. 这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息. 1: #include"pcap.h" 2: ty ...
- iOS - 沙盒中,如何判断存在文件、目录
在iOS开发中,在沙盒中创建沙盒一些存储各个功能的文件目录或者文件. 使用: [NSFileManager defaultManager] 1.判断目录,用她可以. 2.判断文件,用她可以. 3.创建 ...
- JavaScript中的事件对象
JavaScript中的事件对象 JavaScript中的事件对象是非常重要的,恐怕是我们在项目中使用的最多的了.在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含这所有与事件有 ...
- 【原】web移动端常用知识点笔记
摘要:因为平时搞移动端的比例多一点,做个小小的总结.虽然网上很多这方面的总结,不过还是想自己也总结一下,适合自己的才是最好的.这样也方便以后自己的查阅 viewport模板——通用 <!DOCT ...
- 利用WireShark进行DNS协议分析
一.准备工作 系统是Windows 8.1Pro 分析工具是WireShark1.10.8 Stable Version 使用系统Ping命令发送ICMP报文. 二.开始工作 打开CMD.exe键入: ...
- Repeater 获取数据值
<input id="btn_fld_PRD_UM" class="btn" type="button" value="选择 ...
- Oracle - 数据库的实例、表空间、用户、表之间关系
完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...
- Java web 使用页面压缩
借助类,相关依赖: <!-- https://mvnrepository.com/artifact/net.sourceforge.pjl-comp-filter/pjl-comp-filter ...
- JSP 入门 HTML嵌套Java脚步 显示时间
<%@ page import="java.util.Date"%> <%@ page language="java" contentType ...