参考链接: 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()非阻塞方式的更多相关文章

  1. socket设置为非阻塞方式(windows和linux)

    Windows用以下方法将socket设置为非阻塞方式 : unsigned long ul=1; SOCKET s=socket(AF_INET,SOCK_STREAM,0); int ret=io ...

  2. Linux NIO 系列(03) 非阻塞式 IO

    目录 一.非阻塞式 IO 附:非阻塞式 IO 编程 Linux NIO 系列(03) 非阻塞式 IO Netty 系列目录(https://www.cnblogs.com/binarylei/p/10 ...

  3. 非阻塞方式connect编程

    参考博客: ①setsockopt()函数使用详解:http://blog.csdn.net/tody_guo/article/details/5972588 ②setsockopt :SO_LING ...

  4. 用WINSOCK API实现同步非阻塞方式的网络通讯

    Option Base 0Option Explicit '* ************************************************** *'*  模块名称:Winsock ...

  5. linux c编程:非阻塞I/O

    通常来说,从普通文件读数据,无论你是采用 fscanf,fgets 也好,read 也好,一定会在有限的时间内返回.但是如果你从设备,比如终端(标准输入设备)读数据,只要没有遇到换行符(‘\n’),r ...

  6. linux centos 安装Jenkins(非docker方式)

    写在前面 我之前写过Asp.net Core 使用Jenkins + Dockor 实现持续集成.自动化部署(一):Jenkins安装这jenkisn的安装过程,但这篇使用的是docker的方式安装的 ...

  7. select应用于read函数 超时非阻塞方式

    /* * "Timed" read - timout specifies the # of seconds to wait before * giving up (5th argu ...

  8. Linux设备驱动中的阻塞和非阻塞I/O

    [基本概念] 1.阻塞 阻塞操作是指在执行设备操作时,托不能获得资源,则挂起进程直到满足操作所需的条件后再进行操作.被挂起的进程进入休眠状态(不占用cpu资源),从调度器的运行队列转移到等待队列,直到 ...

  9. linux下socket connect 阻塞方式 阻塞时间控制

    同事今天问我,如何在linux下的c代码里面控制connect的阻塞时间.应用的背景是:linux下的c程序有两个目标IP需要connect,如果用阻塞方式,当其中一个IP不能连接的情况下,程序将阻塞 ...

随机推荐

  1. vi编辑文件出现Can't open file for writing错误

    可以用 ll 命令查看一下文件的权限,很有可能是没有权限,用chmod命令修改一下权限就可以了(当然是文件所有者或者root用户才能修改),或者切换成root用户(不推荐)

  2. wpf button的mouse(leftbutton)down/up,click事件不响应解决办法

    按照WPF的帮助说明,某些控件的路由事件被内部处理了,已经被标记为Handled,自行定义的事件处理代码便不再起作用了,有时候会很郁闷!         不过WPF提供了必要的方法.         ...

  3. ARPSpoofing教程(四) - 捕获并分析数据包

    本程序的主要目标是展示如何解析所捕获的数据包的协议首部. 这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息. 1: #include"pcap.h" 2: ty ...

  4. HD1814Peaceful Commission(模板题)

    题目链接 题意: 和平委员会 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条 ...

  5. BZOJ4590: [Shoi2015]自动刷题机

    显然看着就是二分,仔细看的话显然刷的题数随n增大单调不升. 挂了一发是因为无解输出一个-1而不是两个…… #include<cstdio> #include<algorithm> ...

  6. elasticsearch api

    count curl -XGET http://21.3.5.121:9200/ipv4geo/service/_count -d '{"query":{"match&q ...

  7. 提取hdfs文件名的方法

    #!/bin/bash #------------------------------------------------------------- # 把目录中的所有文件getmerge到本地 # ...

  8. Jquery 实现密码框的显示与隐藏【转载自http://blog.csdn.net/fengzhishangsky/article/details/11809069】

    <html> <head>  <script type="text/JavaScript"  src="jQuery-1.5.1.min.j ...

  9. 基于Python实现对PDF文件的OCR识别

    http://www.jb51.net/article/89955.htm https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-pytho ...

  10. MySQL学习笔记——存储过程