GetChar () non-blocking mode of Linux C

Source: Internet
Author: User

Reference Link: 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 namespacestd;#defineUsr_debugStatic structTermios ori_attr, cur_attr;Static__inlineintTty_reset (void){   if(Tcsetattr (Stdin_fileno, Tcsanow, &ori_attr)! =0)      return-1; return 0;}Static__inlineintTty_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__inlineintKbhit (void) {Fd_set RfDs; structTimeval TV; intretval; /*Watch stdin (FD 0) to see if 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) would be true.*/   Else      return 0; return 0;}intMainintargcChar*argv[]) {//non-blocking GetChar initialization   intTty_set_flag; Tty_set_flag=Tty_set (); Charcom_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_debugCcomcommon*m_pcomuart =NewCcommuart; #ifdef usr_debug printf ("Create ccommuart\n");#endif //Usr_debug   Charsendstr[ the] = {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");//Write FileFILE*PFile; Charfilename[124] = {0}; sprintf (FileName,"Readdata_dir/%s.txt", Com_num); PFile= fopen (FileName,"W"); Charrbuff[ the] = {0}; intNsize =0; inti =0; intncnt =0;  while(1) {      /********************* sending test *******************************/      //M_pcomuart->sendbuff (buff,sizeof (buff)); //m_pcomuart->sendstr ("1234567887654321abcdefgh"); //m_pcomuart->sendstr ("Kkkkkkkkkkkkkkkkkk");      if(Kbhit ()) {Const intKey =GetChar (); //printf ("%c pressed\n", key);         if(Key = ='Q')             Break; } /*else {fprintf (stderr, "<no key detected>\n"); }*/      /********************* received test case *************************/memset (Rbuff,0, the); Nsize= M_pcomuart->getdata (Rbuff, the, +); 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", (unsignedChar) rbuff[i]); if(ncnt = =7) {fprintf (PFile,"\ n"); Ncnt=0; } Else{ncnt++; }         }         //Binary Input//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 (+);} Delete M_pcomuart; Fclose (pFile);//operation of the thread   if(Tty_set_flag = =0) Tty_reset ();}

GetChar () non-blocking mode of Linux C

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.