View topic - Clear input buffer

Clear input buffer

anything that doesn't fit to other groups.

Clear input buffer

Postby Ifflag » Wed Aug 22, 2018 6:14 am

Hello,
For a project I have to write a program that controls a sorting machine connected to a PC running QNX 6.5.0 on it. To control the machine I use a switch case.
The problem is I don't know how to clear the input buffer.

Here is the code:

Code: Select all
do
   {
      read ( petra_in , &u_capt.byte , 1 );

      printf("\n\n\n\tACTIVATEURS\n");
      printf("*******************\n\n");
      printf("CP (Carriage position) : %x \t[1, 2, 3, 4]\n", u_act.act.CP);
      printf("C1 (Convoyeur 1): %x \t[a]\n", u_act.act.C1);
      printf("C2 (Convoyeur 2): %x \t[z]\n", u_act.act.C2);
      printf("PV (Plongeur vaccum): %x \t[v]\n", u_act.act.PV);
      printf("PA (Plongeur activate): %x \t[b]\n", u_act.act.PA);
      printf("AA (Arm activate): %x \t[e]\n", u_act.act.AA);
      printf("GA (Arm Gripper activate): %x \t[g]\n", u_act.act.GA);

      printf("\n\n");
      printf("\n\tCAPTEURS\n");
      printf("*******************\n\n");
      printf("DE (Parts dispenser sensor): %x \n\r", u_capt.capt.DE);
      printf("AP (Arm position sensor): %x \n\r", u_capt.capt.AP);
      printf("CS (Carriage in position): %x \n\r", u_capt.capt.CS);
      printf("PP (Plunger position): %x \n\r", u_capt.capt.PP);
      printf("T (Material thickness sensor): %x \n\r", u_capt.capt.T);
      printf("S (Slot detection sensor): %x \n\r", u_capt.capt.S);
      printf("L1 (Cut-out detection sensor): %x \n\r", u_capt.capt.L1);
      printf("L2 (Cut-out detection sensor): %x \n\r", u_capt.capt.L2);

      printf("\nq pour quitter\n\n");
      printf("-> ");


                fflush(stdin);

      scanf("%c", &c);

      switch(c)
      {
         case '1' ://CPpose1

               u_act.act.CP = 0;

            break;
         case '2' ://CPpose2

               u_act.act.CP = 1;

            break;
         case '3' ://CPpose3

               u_act.act.CP = 2;

            break;
         case '4' ://C1pose4

               u_act.act.CP = 3;

            break;
         case 'a' ://C1

               if(u_act.act.C1 == 1)
                  u_act.act.C1 = 0;
               else
                  u_act.act.C1 = 1;

            break;
         case 'z' ://C2

               if(u_act.act.C2 == 1)
                  u_act.act.C2 = 0;
               else
                  u_act.act.C2 = 1;

            break;
         case 'v' ://PV

               if(u_act.act.PV == 1)
                  u_act.act.PV = 0;
               else
                  u_act.act.PV = 1;

            break;
         case 'b' ://PA

               if(u_act.act.PA == 1)
                  u_act.act.PA = 0;
               else
                  u_act.act.PA = 1;

            break;
         case 'e' ://AA

               if(u_act.act.AA == 1)
                  u_act.act.AA = 0;
               else
                  u_act.act.AA = 1;
            break;
         case 'g' ://GA

               if(u_act.act.GA == 1)
                  u_act.act.GA = 0;
               else
                  u_act.act.GA = 1;
            break;

            default : printf("Touche inconnue \n");
                //break;
      }
      rc= write ( petra_out , &u_act.byte , 1 );
      if (rc<=0)
      {
         printf ("rc: %d\n ",rc);
         perror("");
      }
      printf("\n\n");
   }while(c != 'q');


I know that fflush(stdin) is not portable, so I have tried the following:
1. Replacing
Code: Select all
fflush(stdin);
with
Code: Select all
fseek(stdin, 0, SEEK_END);
Didn't work.

2.Replacing
Code: Select all
fflush(stdin);
scanf("%c", &c);
with
Code: Select all
do{
    c = getchar();
while(c  != '\n' && c != EOF);
Didn't work either (Maybe this one doesn't work because I have not placed c = getchar() in the while loop, but im not sure)

Or is it because of the 'default' case in the switch case(); ?

I have no way to test and I must have finished the project for next week, that's why I'm asking.
Ifflag
New Member
 
Posts: 8
Joined: Thu Apr 12, 2018 10:07 pm
Location: Belgium

Re: Clear input buffer

Postby nico04 » Wed Aug 22, 2018 6:29 am

Bonjour,

Why do you want to flush stdin ?

Nicolas
nico04
Senior Member
 
Posts: 106
Joined: Wed Sep 29, 2010 9:59 am
Location: France

Re: Clear input buffer

Postby Ifflag » Wed Aug 22, 2018 6:44 am

Bonjour 8)

Because of the '\n' new line character, when I press a key the process is always looping twice:
First loop: the caracter (i.e. a, b, c, d...)
Second loop: the new line caracter, that's where is goes in the default case and prints "Touche inconnu"

Daniel
Ifflag
New Member
 
Posts: 8
Joined: Thu Apr 12, 2018 10:07 pm
Location: Belgium

Re: Clear input buffer

Postby nico04 » Wed Aug 22, 2018 7:44 am

Please try "scanf("%c\n", &c);"
nico04
Senior Member
 
Posts: 106
Joined: Wed Sep 29, 2010 9:59 am
Location: France

Re: Clear input buffer

Postby maschoen » Fri Aug 24, 2018 5:38 am

Take a look at

tcflush()

eg.

tcflush(fileno(stdin), TCIFLUSH);
maschoen
QNX Master
 
Posts: 2684
Joined: Wed Jun 25, 2003 5:18 pm

Re: Clear input buffer

Postby Ifflag » Fri Aug 24, 2018 3:55 pm

char buff[5];

I used fgets(buff, 5, stdin); which also can be used with stdin but I just read the first case of the array (buff[0]) to be sure that I will get the first character and nothing else
Ifflag
New Member
 
Posts: 8
Joined: Thu Apr 12, 2018 10:07 pm
Location: Belgium


Return to General Programming

Who is online

Users browsing this forum: No registered users and 5 guests