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]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');[/code]
I know that fflush(stdin) is not portable, so I have tried the following:
- Replacing
fflush(stdin);
withfseek(stdin, 0, SEEK_END);
Didn’t work.
2.Replacing fflush(stdin);
scanf("%c", &c);
with
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.