#include "stdio.h"
#define RxRDY 0x01
#define TxRDY 0x20
#define LSR 0x4Fd
#define LCR 0x4Fb
#define BRDH 0x4F9
#define BRDL 0x4F8
#define THR 0x4F8
#define RDR THR
main()
{
   char *msg[]={"PC<-------->89C2051  ",
		" CashDrawer Test     ",
		"1 = Open CashDrawer  ",
		"2 = CashDrawer state ",
		"Q = Quit             "};
   char i;
   char key;
   char a=0xff;
   char b;
   int	timer,timeout;
   outportb(LCR,0x80);
   outportb(BRDH,0x00);
   outportb(BRDL,0x60);
   outportb(LCR,0x03);
   inportb(RDR);
   clrscr();
   gotoxy(1,6);printf("%s",msg[0]);
   gotoxy(1,7);printf("%s",msg[1]);
   gotoxy(1,8);printf("%s",msg[2]);
   gotoxy(1,9);printf("%s",msg[3]);
   gotoxy(1,10);printf("%s",msg[4]);
start:   gotoxy(1,12);printf("input select: ");
   while(1)
   {
      a=0xff;
      gotoxy(15,12);
      key=toupper(getch());
      if(key=='Q') goto quit;
      else if(key=='1')
      {
	a=1;
	printf("%2c",key);
      }
      else if(key=='2')
      {
	a=2;
	printf("%2c",key);
      }

      else if(key==0x3b)
      {
	a=1;
	printf("F1");
      }
      else if(key==0x3c)
      {
	a=2;
	printf("F2");
      }

      if(a>=1 & a<=2)
      {
	timer=0; /* sent data. */
	while(((inportb(LSR) & TxRDY)!=TxRDY) & (timer<=25000))	; 
	  {
	     timer=timer+1;
	     if (timer>=25000)
	       {
		 timeout=timer;
	         goto start;
	       }
	  }

	  ;
	outportb(THR,a);

	timer=0;
	while(((inportb(LSR) & RxRDY)!=RxRDY) & (timer <= 25000))
	  {
	     timer=timer+1;
	     if (timer>=25000)
	       {
		 timeout=timer;
	         goto start;
	       }
	  }
	  ;
	a=inportb(RDR);
	  ;	
	gotoxy(1,14);printf("receive char:%2d words",a);
	gotoxy(1,16);printf("receive string:");
	printf("%20c",0x20); /* space */
	gotoxy(18,16);
	for(i=0;i<a;i++)
	  {
	     timer=2;
	     while(((inportb(LSR) & RxRDY)!=RxRDY) & (timer <= 25000))
	       {
	         timer=timer+1;
	         if (timer>=25000)
		   {
		     timeout=timer;
		     goto error;
	           }

	       }
	       ;
	     b=inportb(RDR);
	     printf("%c",b);

	  }
	if (((a!='4') & (a!='5')) || ((b!= 'N') & (b !='E')))
	   {
	      goto error;
	   }
	else
	   {
	      gotoxy(1,18); printf("Cashdraw function test  !");
	   }	
      }      
   }
   ok:        gotoxy(1,18); printf("Cashdraw function test  !");
	      goto start;
   error:     gotoxy(1,18); printf("Cashdraw function test  !");
	      goto start;
   ;
   quit: gotoxy(1,20);
}
