/*

	sketmast.c

	skt2.bat = cl /AL sket sketmast.c mmserial imga

*/
#include <stdio.h>
#include <string.h>

#define MAX_X 640
#define MAX_Y 480
#define TRUE  1
#define FALSE 0
#define WAIT for(i=0;i<10000;i++)

struct point{
	unsigned char button;
	int x;
	int y; };
struct   Color 
{ 
	unsigned char  r, g, b; 
}lut[256],lutm[256],lutc[256],luts[256]; 
unsigned int port[6] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x3220, 0x3228 };
struct point inpoint;
unsigned char Buffer[12][640];

/* extern functions */
extern int htoi();
extern void initser();
extern unsigned int getser();
extern unsigned char putser(unsigned char);
extern struct point get_coord();

unsigned int comport;
int init_board();
int plot_lines();
int draw_on_screen();
int get_a_point();
int locate_a_point();
int map_a_point();

/********************************************************************
**
**
**
********************************************************************* */

int mouse_a_point(ix,iy,res,lx,ly,color)

int *ix,*iy,*lx,*ly,color;
char res;

{
	int i,j,k;
	int x,y,cx=-1,cy=-1;
	float scale=0.0,sx,sy;
	int iscale,mx,my;
	int hit=-1;

	*ix=*iy=-1;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X-1.0;
		sy=scale*(float)12.0/(float)MAX_Y-1.0;
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(my-MAX_Y)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}
	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */

	i=j=k=0;
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				uncursor(3,cx,cy,5,Buffer);
				return(-1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			*lx=inpoint.x;
			*ly=inpoint.y;
			if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
			{
				x=inpoint.x/iscale-mx;
				y=inpoint.y/iscale-my;
			}
			else
			{
				x=inpoint.x*iscale-mx;
				y=inpoint.y*iscale-my;
			}
			if(x>=MAX_X)x=MAX_X-1;
			if(y>=MAX_Y)y=MAX_Y-1;
			if(x<0)x=0;
			if(y<0)y=0;

			if(inpoint.button==0)
			{
				if(hit!=-1)
					return(hit);
				if(cx!=x||cy!=y)
				{
					uncursor(0,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(0,cx,cy,5,Buffer);
					cursor(0,cx,cy,color,5);
				}
			}
			else if(inpoint.button==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=1;
			}
			else if(inpoint.button>1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=inpoint.button;
			}
		}
	}
}




/********************************************************************
**
**
**
********************************************************************* */

int map_a_point(ix,iy,res,lx,ly)

int *ix,*iy,*lx,*ly;
char res;

{
	int i,j,k;
	int x,y,cx=-1,cy=-1;
	float scale=0.0,sx,sy;
	int iscale,mx,my;
	int hit=-1;

	*ix=*iy=-1;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X+1.0;
		sy=scale*(float)12.0/(float)MAX_Y+1.0;
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(my-MAX_Y)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}
	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */

	i=j=k=0;
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				uncursor(3,cx,cy,5,Buffer);
				return(-1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			*lx=inpoint.x;
			*ly=inpoint.y;
			if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
			{
				x=inpoint.x/iscale-mx;
				y=inpoint.y/iscale-my;
			}
			else
			{
				x=inpoint.x*iscale-mx;
				y=inpoint.y*iscale-my;
			}
			if(x>=MAX_X)x=MAX_X-1;
			if(y>=MAX_Y)y=MAX_Y-1;
			if(x<0)x=0;
			if(y<0)y=0;

			if(inpoint.button==0)
			{
				if(hit!=-1)
					return(hit);
				if(cx!=x||cy!=y)
				{
					uncursor(3,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(3,cx,cy,5,Buffer);
				}
			}
			else if(inpoint.button==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=1;
			}
			else if(inpoint.button>1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=inpoint.button;
			}
		}
	}
}



/********************************************************************
**
**
**
********************************************************************* */

int locate_a_point(ix,iy,res,lx,ly)

int *ix,*iy,*lx,*ly;
char res;

{
	int i,j,k;
	int x,y,cx=-1,cy=-1;
	float scale=0.0,sx,sy;
	int iscale,mx,my;
	int hit=-1;

	*ix=*iy=-1;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X;
		sy=scale*(float)12.0/(float)MAX_Y;
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(my-MAX_Y)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}
	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */

	i=j=k=0;
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				uncursor(3,cx,cy,5,Buffer);
				return(-1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			*lx=inpoint.x;
			*ly=inpoint.y;
			if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
			{
				x=inpoint.x/iscale-mx;
				y=inpoint.y/iscale-my;
			}
			else
			{
				x=inpoint.x*iscale-mx;
				y=inpoint.y*iscale-my;
			}
			if(x>=MAX_X)x=MAX_X-1;
			if(y>=MAX_Y)y=MAX_Y-1;
			if(x<0)x=0;
			if(y<0)y=0;

			if(inpoint.button==0)
			{
				if(hit!=-1)
					return(hit);
				if(cx!=x||cy!=y)
				{
					uncursor(3,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(3,cx,cy,5,Buffer);
				}
			}
			else if(inpoint.button==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=1;
			}
			else if(inpoint.button>1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=inpoint.button;
			}
		}
	}
}


/********************************************************************
**
**
**
********************************************************************* */

int get_a_point(ix,iy,res)

int *ix,*iy;
char res;

{
	int i,j,k;
	int x,y,cx=-1,cy=-1;
	float scale=0.0,sx,sy;
	int iscale,mx,my;
	int hit=-1;

	*ix=*iy=-1;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X;
		sy=scale*(float)12.0/(float)MAX_Y;
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(my-MAX_Y)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}
	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */

	i=j=k=0;
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				uncursor(3,cx,cy,5,Buffer);
				return(-1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
			{
				x=inpoint.x/iscale-mx;
				y=inpoint.y/iscale-my;
			}
			else
			{
				x=inpoint.x*iscale-mx;
				y=inpoint.y*iscale-my;
			}
			if(x>=MAX_X)x=MAX_X-1;
			if(y>=MAX_Y)y=MAX_Y-1;
			if(x<0)x=0;
			if(y<0)y=0;

			if(inpoint.button==0)
			{
				if(hit!=-1)
					return(hit);
				if(cx!=x||cy!=y)
				{
					uncursor(3,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(3,cx,cy,5,Buffer);
				}
			}
			else if(inpoint.button==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=1;
			}
			else if(inpoint.button>1)
			{
				uncursor(3,cx,cy,5,Buffer);
				*ix=x;
				*iy=y;
				hit=inpoint.button;
			}
		}
	}
}

/********************************************************************
**
**
**
********************************************************************* */

int draw_on_screen(color,res)

int color;
char res;

{
	int i,j,k;
	int x,y,cx=-1,cy=-1,ix,iy,ixo=-1,iyo=-1,ix1,iy1;
	float scale=0.0,sx,sy;
	int iscale,mx,my;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X;
		sy=scale*(float)12.0/(float)MAX_Y;
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(my-MAX_Y)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}

	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */

	i=j=k=0;
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				uncursor(3,cx,cy,5,Buffer);
				return(1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
			{
				x=inpoint.x/iscale-mx;
				y=inpoint.y/iscale-my;
			}
			else
			{
				x=inpoint.x*iscale-mx;
				y=inpoint.y*iscale-my;
			}
			if(x>=MAX_X)x=MAX_X-1;
			if(y>=MAX_Y)x=MAX_Y-1;
			if(x<0)x=0;
			if(y<0)y=0;

			if(inpoint.button==0)
			{
				if(cx!=x||cy!=y)
				{
					uncursor(3,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(3,cx,cy,5,Buffer);
				}
			}
			else if(inpoint.button==1)
			{
				i=0;
				uncursor(3,cx,cy,5,Buffer);
				if(j>0)
					plotln(3,x,y,ixo,iyo,color);
				else
				{
					plotpt(3,x,y,color);
					ix1=x;iy1=y;
				}
				ixo=x;iyo=y;
				j+=1;
				cx=x;cy=y;
				tcursor(3,cx,cy,5,Buffer);
			}
			else if(inpoint.button==2)
			{
				i=j=0;
				uncursor(3,cx,cy,5,Buffer);
				plotln(3,x,y,ixo,iyo,color);
				plotln(3,ix1,iy1,ixo,iyo,color);
				cx=x;cy=y;
				tcursor(3,cx,cy,5,Buffer);
			}
			else if(inpoint.button==4)
			{
				i=j=0;
			}
		}
	}
}



/********************************************************************
**
**
**
********************************************************************* */

int plot_lines(file,color,res)

char *file,res;
int color;

{
	int i=0,j,k=1;
	int x,y,ix,iy,ixo=-1,iyo=-1,ix1,iy1;
	FILE *fpout;
	float scale=0.0,sx,sy;
	int iscale,mx,my,x1,y1;
	int px,py,pxo=-1,pyo=-1,same,reset=1,px1,py1;
	int cx=-1,cy=-1;

	if(res=='d')scale= 100.0;
	if(res=='e')scale= 200.0;
	if(res=='g')scale= 400.0;
	if(res=='h')scale= 500.0;
	if(res=='j')scale=1000.0;
	if(res=='f')scale= 254.0;
	if(res=='i')scale= 508.0;
	if(res=='q')scale=1016.0;
	if(res=='l')scale=   1.0;
	if(res=='n')scale=   2.0;
	if(res=='p')scale=   4.0;
	if(scale<=0.0)return(-1);

	if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
	{
		sx=scale*(float)18.0/(float)MAX_X+0.999;
		sy=scale*(float)12.0/(float)MAX_Y+0.999;
		iscale=sx;
		if(sy>sx)iscale=sy;
		mx=(float)18.0*scale;
		mx/=iscale;
		mx=(MAX_X-mx)/2;
		my=(float)12.0*scale;
		my/=iscale;
		my=(MAX_Y-my)/2;
	}
	else
	{
		sx=(float)MAX_X/(scale*(float)18.0);
		sy=(float)MAX_Y/(scale*(float)12.0);
		iscale=sx;
		if(sy<sx)iscale=sy;
		mx=(float)18.0*scale;
		mx*=iscale;
		mx=(mx-MAX_X)/2;
		my=(float)12.0*scale;
		my*=iscale;
		my=(my-MAX_Y)/2;
	}
	putser((unsigned char)('@'));	/* Continouos Mode */
	putser((unsigned char)('b'));	/* Set Origin to Upper Left Corner */
	putser((unsigned char)res);	/* set resolution */


/*	putser((unsigned char)('B'));*/	/* Point mode */
/*	putser((unsigned char)('b'));*/	/* Set Origin to Upper Left Corner */

	strcat(file,".xyc");
	fpout=fopen(file,"wt");
	while(1)
	{
		if(kbhit())
			if(getch()==0x1b)   /* 'Esc' */
			{
				if(cx>0)
					uncursor(3,cx,cy,5,Buffer);
				fclose(fpout);
				return(1);
			}
		inpoint=get_coord();
		if(inpoint.button!=255)
		{
			if(inpoint.button==0)
			{
				reset=1;
				if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
				{
					x=inpoint.x/iscale+mx;
					y=inpoint.y/iscale+my;
				}
				else
				{
					x=inpoint.x*iscale+mx;
					y=inpoint.y*iscale+my;
				}
				if(x>=MAX_X)x=MAX_X-1;
				if(y>=MAX_Y)x=MAX_Y-1;
				if(x<0)x=0;
				if(y<0)y=0;
				if(cx!=x||cy!=y)
				{
					if(cx>=0)
						uncursor(3,cx,cy,5,Buffer);
					cx=x;cy=y;
					tcursor(3,cx,cy,5,Buffer);
				}
			}
			if(inpoint.button==1&&reset==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				reset=0;
				if(i==0)
				{
					fprintf(fpout,"\n%3d",k++);
				}
				if(i%4==0)
				{
					fprintf(fpout,"\n");
				}
				else
				{
					fprintf(fpout,"  ");
				}
				if(scale*(float)18.0>MAX_X||scale*(float)12.0>MAX_Y)
				{
					x=inpoint.x/iscale+mx;
					y=inpoint.y/iscale+my;
				}
				else
				{
					x=inpoint.x*iscale+mx;
					y=inpoint.y*iscale+my;
				}
				if(x>=MAX_X)x=MAX_X-1;
				if(y>=MAX_Y)x=MAX_Y-1;
				if(x<0)x=0;
				if(y<0)y=0;

				ix=x;
				iy=y;
				px=inpoint.x;
				py=inpoint.y;
				same=0;
				if(px==pxo&&py==pyo)
					same=1;
				if(same==0)
				{
					fprintf(fpout,"%4d %4d",inpoint.x,inpoint.y);
					if(i>0)
						plotln(3,ix,iy,ixo,iyo,color);
					i+=1;
					pxo=px;
					pyo=py;
					ixo=ix;
					iyo=iy;
				}
				if(i==1)
				{
					px1=inpoint.x;
					py1=inpoint.y;
					x1=x;
					y1=y;
					ix1=ix;
					iy1=iy;
				}
				cx=cy=-1;
			}
			if(inpoint.button==2&&reset==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				reset=0;
				plotln(3,ixo,iyo,ix1,iy1,color);
				if(i%4==0)
				{
					fprintf(fpout,"\n");
				}
				else
				{
					fprintf(fpout,"  ");
				}
				fprintf(fpout,"%4d %4d//",px1,py1);
				i=0;
				cx=cy=pxo=pyo=ixo=iyo=-1;
			}
			if(inpoint.button==3&&reset==1)
			{
				reset=0;
				k=1;
			}
			if(inpoint.button==4&&reset==1)
			{
				uncursor(3,cx,cy,5,Buffer);
				reset=0;
				fprintf(fpout,"//");
				i=0;
				cx=cy=pxo=pyo=ixo=iyo=-1;
			}
		}
	}
}





/********************************************************************
**
**
**
********************************************************************* */

int init_board(argc,argv)

int argc;
char *argv;

{
	int i,com_found;

	com_found = FALSE;
	if(argc>1)
	{
		strlwr(argv);
		if(strchr(argv,'x')!= NULL)
		htoi(argv+2);
		else
		{
			if(stricmp(argv,"/1")==0||stricmp(argv,"1")==0)
				comport=0x3f8;  /* COM1 */
			else if(stricmp(argv,"/2")==0||stricmp(argv,"2")==0)
				comport = 0x2f8;	/* COM2 */
			else if(stricmp(argv,"/3")==0||stricmp(argv,"3")==0)
				comport=0x3220;	/* COM3 */
			else if(stricmp(argv,"/4")==0||stricmp(argv,"4")==0)
				comport=0x3228;	/* COM4 */
			else
			{
				printf("\n Command Line Syntax ERROR! ");
				printf("\n Incorrect Switch Usage: %s\n",argv);
				exit(1);
			}
		}
		i=0;
		while(i<2&&!com_found)
		{
			if(chkcom())
				com_found=TRUE;
			i++;
		}
		if(!com_found)
		printf("\n  Communication Failure at Specified Port!");
	}
	if(!com_found)
	{
			/** autosensing of Com port begin **/
		for(i=0;i<6;i++)
		{
			comport=port[i];
			if(chkcom())
			i=10;
		}
		/* if i != 11, Com 1-4 is not connected to tablet */
		if(i!=11)
		{
			printf("  i = %d\n",i);getch();
			printf("\n  Digitizer Not Connected To Valid COM Port!");
			exit(1);
		}
		if(argc>1)
		{
			printf("\n  Tablet Found Connected");
			switch(comport)
			{
				case 0x3f8:
					printf(" To COM1 !");
					break;
				case 0x2f8:
					printf(" To COM2 !");
					break;
				case 0x3220:
					printf(" To COM3 !");
					break;
				case 0x3228:
					printf(" To COM4 !");
					break;
				default:
					printf(" To COM = %x!(hex)",comport);
			}
			exit(1);
		}
	}
	putser((unsigned char)(0x00));  /* RESET tablet */
}





/********************************************************************
**
**
**
********************************************************************* */

int chkcom()  /* check if tablet is attached to the COM port specified */

{
long j;
	outp(comport + 3, 0x80);
	outp(comport , 12);
	outp(comport + 1, 0);
	outp(comport + 3, 0x0b); /* 1 stop, 8 data bits, odd parity */
	outp(comport + 1, 0);	/* disable serial interrupts */
	outp(comport + 4, 0x0b);
	j = 0;
	do
	{
	putser((unsigned char) 0x00);
	inpoint = get_coord();
	j++;
	}
	while(j < 5);

/** hand shake with tablet **/

	j = 0;
	do
	{
	/* Request maximum coordinate */
	putser((unsigned char) 'a');
	inpoint = get_coord();
	j++;
	}
	while (inpoint.button == 255 && j < 5);
	if ( inpoint.x == 9000 || inpoint.x == 5850)
	return(1);		/** tablet is conected **/
		else
	return(0);		/** tablet is not connected **/
	}

/********************************************************************
**
**
**
********************************************************************* */

int chk_test_result()

{
long wait;
int i, j, initialized, loop_none;
unsigned int itemp;
unsigned char temp;

initialized = loop_none = TRUE;
/** reset tablet at least 3 times **/
j = 0;
do
{
	(void)putser(0x00);
	for (wait = 0; wait < 4000; wait++)
		;
	itemp = getser();
	j++;
}
	while ( j < 10);
  	loop:
  	;
	for (wait = 0; wait < 8000; wait++)
		;
	(void)putser((unsigned char) 't');
	for (wait = 0; wait < 8000; wait++)
		;
	itemp = getser();

	i = 0;
	do
	{
	for (wait = 0; wait < 8000; wait++);
		;
	(void)putser((unsigned char) 'w');
	itemp = getser();
	i++;
	}while ( (itemp == 0xffff) && i < 10);

	if ( (itemp & 0xff00) == 0)
	{
	temp = (unsigned char)(itemp);
	if ((temp & 0xff) != 0x8f )
	{
		if (loop_none)
		{
	if ((temp & 0x01) == 0x00 )
	{
	printf("\n   Analog circuitry test failure!");
	initialized = FALSE;
	}
	if ((temp & 0x02) == 0x00 )
	{
	printf("\n   Cursor/Stylus Operation Failure!");
	printf("\n   Please Check Cursor/Stylus Cord Connection");
	return(FALSE);
	}
	if ((temp & 0x04) == 0x00 )
	{
	printf("\n    Digital Circuit failure!");
	initialized = FALSE;
	}
		}
	if (( temp & 0x08) == 0x00 )
	{
	if (loop_none)
	{
		printf("\n E R R O R   D E T E C T E D || Either #1 or #2 is true:\n\n");
		printf(" 1.    The cursor/stylus is NOT in proximity");
		printf(" 2.    Grid operation/connection failure.\n\n");
	}
	loop_none = FALSE;
	goto loop;
	}
	}
	}
	else
	{
	initialized = FALSE;
	printf("\n    No Test Response From Digitizer!");
	}
	return(initialized);
	}








                                                            