#include  <stdio.h>
#include  <conio.h>
#include  <stdlib.h>
#include  <dos.h>

#define    TIMEOUT     12000L

#include  "ISODA.H"

WORD wBase=0x220,wActive=0,wIrq=15,wDelay1,wBuf[512];

int iComPort,iChksum;
long int lBaudRate;
WORD wMinV[16], wMaxV[16], BASE1,MaxChannel;
WORD wMinI[16], wMaxI[16], BASE2, BASE3;
float rr[16];
char  str1[40],str2[40],str3[40];

WORD compute_hex(WORD mmin, WORD mmax, float in);
WORD wChan;

/* --------------------------------------------------------------------- */

init()
{
rr[ 0]=125.10; /* used for ISODA calibration kits */
rr[ 1]=124.27;
rr[ 2]=125.14;
rr[ 3]=125.02;
rr[ 4]=125.13;
rr[ 5]=125.11;
rr[ 6]=125.13;
rr[ 7]=125.04;
rr[ 8]=125.16;
rr[ 9]=125.14;
rr[10]=125.13;
rr[11]=125.10;
rr[12]=125.10;
rr[13]=125.13;
rr[14]=125.10;
rr[15]=125.13;
}

/* --------------------------------------------------------------------- */

main()
{
char c;

iComPort=1; iChksum=0; lBaudRate=9600L;
OPEN_COM(iComPort,lBaudRate);		/* default */

load_configuration();
init();
for(;;)
{
show_configuration();
printf("\n* 0 : Machine dependent Timer Test                       *");
printf("\n* 1 : ISO_DA16/DA8 D/I/O Test                            *");
printf("\n* 2 : Machine Independent Timer Test                     *");
printf("\n* 3 : Write DA EEPROM                                    *");
printf("\n* 4 : Read  DA EEPROM                                    *");
printf("\n* 5 : Set   DA Power-On Value (without calibration)      *");
printf("\n* 6 : Read  DA Power-On Value (without calibration)      *");
printf("\n*--------------------------------------------------------*");
printf("\n* 7 : DA -10V  calibration (need ISODA calibration kits) *");
printf("\n* 8 : DA  10V  calibration (need ISODA calibration kits) *");
printf("\n* 9 : DA  0mA  calibration (need ISODA calibration kits) *");
printf("\n* A : DA  20mA calibration (need ISODA calibration kits) *");
printf("\n* B : Load Software Calibration data from EEPROM         *");
printf("\n* C : measure all voltage output                         *");
printf("\n* D : measure all current output                         *");
printf("\n* E : send to D/A                                        *");
printf("\n*--------------------------------------------------------*");
printf("\n* F : DA Voltage Output Test (after calibration)         *");
printf("\n* G : DA Current Output Test (after calibration)         *");
printf("\n* H : Save    EEPROM into File                           *");
printf("\n* I : Load    EEPROM from File                           *");
printf("\n* Q : Quit                                               *");
printf("\n*-----Press any key to select function-------------------*");

c=getch();
switch	(c)
	{
	case '0' : fun_0(); break;
	case '1' : fun_1(); break;
	case '2' : fun_2(); break;
	case '3' : fun_3(); break;
	case '4' : fun_4(); break;
	case '5' : fun_5(); break;
	case '6' : fun_6(); break;
	case '7' : fun_7(); break;
	case '8' : fun_8(); break;
	case '9' : fun_9(); break;
	case 'a' :
	case 'A' : fun_a(); break;
	case 'b' :
	case 'B' : fun_b(); break;
	case 'c' :
	case 'C' : fun_c(); break;
	case 'd' :
	case 'D' : fun_d(); break;
	case 'e' :
	case 'E' : fun_e(); break;
	case 'f' :
	case 'F' : fun_f(); break;
	case 'g' :
	case 'G' : fun_g(); break;
	case 'h' :
	case 'H' : fun_h(); break;
	case 'i' :
	case 'I' : fun_i(); break;
	case 'q' :
	case 'Q' : goto ret_label;
	}
}
ret_label:
return;
}

/* -------------------------------------------------------------------- */

set_pos(int x, int y)
{
union REGS regs;
regs.h.ah=2;
regs.h.dh=y;
regs.h.dl=x;
regs.h.bh=0;
int86(0x10,&regs,&regs);
}

/* -------------------------------------------------------------------- */

clear_screen(void)
{
union REGS regs;
regs.h.ah=15;
int86(0x10,&regs,&regs);
regs.h.ah=0;
int86(0x10,&regs,&regs);
}

/* --------------------------------------------------------------------- */

load_configuration()
{
FILE *stream;
int wRetVal;

wActive=0;
stream=fopen("ISODA.DAT","r");
if (stream)
   {
   fscanf(stream,"%x",&wBase);
   fscanf(stream,"%d",&wIrq);
   fscanf(stream,"%d",&MaxChannel);
   if ((MaxChannel != 16) && (MaxChannel != 8))
      {
      printf("\nISODA.DAT data error, MaxChannel=%d",MaxChannel);
      printf("\nMaxChannel must be 16 or 8");
      exit(0);
      }
   fscanf(stream,"%d",&wDelay1);
   printf("\nLOAD ISODA.DAT OK");

   wRetVal=ISODA_CheckBoard(0,wBase,wIrq);
   if (wRetVal==NoError)
      {
      wActive=1;
      printf("\nSet Configuration OK");
      }
   else printf("\nSet Configuration Error, Error Code=%d",wRetVal);

   fclose(stream);
   }
else printf("\nOPEN ISODA.DAT ERROR");
}

/* --------------------------------------------------------------------- */

show_configuration()
{
printf("\n");
printf("*----Base=%3x",wBase);
printf(",Irq=%2d",wIrq);
if (MaxChannel==16) printf(",Delay1=%5d,Version 1.2,ISO-DA16---*",wDelay1);
else if (MaxChannel==8) printf(",Delay1=%5d,Version 1.2,ISO-DA8----*",wDelay1);
}

/* --------------------------------------------------------------------- */

fun_0()
{
int i;
float f;

i=ISODA_ShortSub2(1,2);
printf("\nLibrary Test : ISODA_ShortSub2(1,2)=%d",i);
if (i==-1) printf(" --> OK"); else printf(" --> Error");
f=ISODA_FloatSub2(1.0,2.0);
printf("\nLibrary Test : ISODA_FloatSub2(1.0,2.0)=%f",f);
if (f==-1.0) printf(" --> OK"); else printf(" --> Error");
i=ISODA_GetVersion();
printf("\nSoftware Version=%x",i);

printf("\n*******************************************************************************");
printf("\n* Machine Dependent Timer Test, the @ will appear every second in Pentium-120 *");
printf("\n* If it is not true, edit the ISODA.DAT until OK.                             *");
printf("\n* This timer is used for delay only, therefore it does not need very precise  *");
printf("\n* Press any key to stop this test                                             *");
printf("\n*******************************************************************************");
printf("\nNow DELAY=%d\n",wDelay1);
for (;;)
    {
    delay_ms(1000); printf("@");
    if (kbhit()!=0) {getch(); return;}
    }
}

delay_ms(int t)
{
int i,j,k;
float p,q,r;

for (i=0; i<t; i++)
for (k=0; k<wDelay1; k++)
    {
    p=i+t;
    q=i+k;
    r=p*q;
    }
}

/* --------------------------------------------------------------------- */

fun_1()
{
WORD i,j,k;
float f;
char c;

printf("\nPress any key to start --> [DO,DI] "); getch();
for (i=0; i<256; i++)
    {
    k=i+i*256;
    ISODA_Do(k);
    ISODA_Di(&j); printf("[%x,%x] ",k,j);
    if (k!=j)
       {
       printf("\7\7\7\7");
       }
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_2()
{
WORD wRetVal;

printf("\n****************************************************************************");
printf("\n* Machine Independent Timer Test, the @ will appear every second in any PC *");
printf("\n* Press any key to stop this test                                          *");
printf("\n****************************************************************************");
printf("\n");
for (;;)
    {
    wRetVal=ISODA_DelayMs(1000);
    if (wRetVal == NoError) printf("@");
    else
	{
	printf(" Error code=%d, press any key to continue",wRetVal); getch();
	return;
	}
    if (kbhit()!=0) {getch(); return;}
    }
}

/* --------------------------------------------------------------------- */

fun_3()
{
WORD i,k;
char c;

printf("\nYou can edit the TEST.C and recompile to write data into EEPROM");
printf("\nPress any key to continue"); getch();
/*
printf("\ninput value > 16383 can stop this test");
printf("\nStart value ="); scanf("%d",&k);
if (k>16383) return;

printf("\nAre you Sure (press 1) ="); c=getche();
if (c != '1') return;
printf("\nAre you Sure (press 5) ="); c=getche();
if (c != '5') return;
printf("\nAre you Sure (press 3) ="); c=getche();
if (c != '3') return;

for (i=0; i<64; i++)
    {
    printf("\nEEPROM Address_%2d : %d", i,k);
    if (ISODA_WriteRom(i,k) == NoError)
	 printf(" OK");
    else printf("Write Error");
    k++;
    }
printf("\nPress any key to continue"); getch();
*/
}

/* --------------------------------------------------------------------- */

fun_4()
{
WORD i,j,k,low,high;

for (k=0; k<64; k++)
    {
    printf("\nAddress_%2d : ", k);
    if (ISODA_ReadRom(k,&j) == NoError)
       {
       i=j;
       low=i&0xff;
       i=i>>8;
       high=i&0xff;
       printf(" %x, high=0x%x, low=0x%x",j,high,low);
       }
    else printf(" Read Error");
    if (k==15) {printf("\npress any key to continue"); getch();}
    if (k==31) {printf("\npress any key to continue"); getch();}
    if (k==47) {printf("\npress any key to continue"); getch();}
    }
printf("\nPress any key to continue"); getch();
}


/* --------------------------------------------------------------------- */

fun_5()
{
WORD i,j,k;
float v,delta;

printf("\ninput value > 10 or <-10 can stop this test");
printf("\nPower-on value (about ? volt, without calibration)= "); scanf("%f",&v);
if ((v>10) || (v<-10)) return;

delta=20.2/16384.0;	 /* 0=about -10.1V, 16383=about 10.1V */
k=(int)(v/delta)+0x2000; /* compute from volt to HEX */
for (i=0; i<MaxChannel; i++)
    {
    printf("\n14-bit Hex data of Channel_%2d : ", i);
    if (ISODA_SetPowerOnValue(i, k) == NoError)
	 printf("%4d , without calibration", k);
    else printf("Error Setting");
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_6()
{
WORD i,j,k;

for (k=0; k<MaxChannel; k++)
    {
    printf("\nChannel %2d : ", k);
    if (ISODA_ReadPowerOnValue(k, &i) == NoError)
	 printf("%4d, about %f Volt, without calibration", i, i*20.0/16384-10.0);
    else printf("Error Reading");
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

check_isoda_calibrator()
{

SEND_CMD(iComPort,"~**",TIMEOUT,iChksum);       /* send for initial state */
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);

SEND_CMD(iComPort,"$012",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
strcpy(str2,"!01080600");
if (memcmp(str2,str1,strlen(str2))!=0) return(CheckError);

SEND_CMD(iComPort,"$022",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
strcpy(str2,"!02080600");
if (memcmp(str2,str1,strlen(str2))!=0) return(CheckError);

SEND_CMD(iComPort,"$032",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
strcpy(str2,"!03090600");
if (memcmp(str2,str1,strlen(str2))!=0) return(CheckError);

SEND_CMD(iComPort,"$042",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
strcpy(str2,"!04090600");
if (memcmp(str2,str1,strlen(str2))!=0) return(CheckError);

return(NoError);
}

from_str_to_float1(char str[], float *f)
{
WORD k;
float val;

    k=str[2]-'0'; val=k*10.0;
    k=str[3]-'0'; val+=k;
    k=str[5]-'0'; val+=k*0.1;
    k=str[6]-'0'; val+=k*0.01;
    k=str[7]-'0'; val+=k*0.001;
*f=val*(-1.0);
}

compute_now_neg_voltage(WORD wChannel, float *fVal)
{
WORD i;
float val;

if (wChannel<8)
   {
   strcpy(str1,"#010"); i=wChannel;
   }
else
   {
   strcpy(str1,"#020"); i=wChannel-8;
   }

str1[3]=i+'0';
SEND_CMD(iComPort,str1,TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str2,TIMEOUT,iChksum);
from_str_to_float1(str2,&val);
/* printf("\nstr1=%s --> str2=%s ",str1,str2); */
*fVal=val;
}

goto_neg_10v(WORD wChannel, WORD wStart, WORD *wMinV)
{
WORD i,wNow;
float fVal;

wNow=wStart;
for (i=0; i<40; i++)
    {
    printf("\nwNow=%d",wNow);
    ISODA_AnalogOutput(wChannel,wNow);
    delay_ms(1000);
    compute_now_neg_voltage(wChannel,&fVal);
    printf(" --> %.4f",fVal);
    if (fVal>-10.0)
       {
       *wMinV=wNow-1;
       return(NoError);
       }
    if (kbhit()!=0) {getch(); return;}
    wNow++;
    }

return(CheckError);
}

fun_7()
{
WORD i,j,k,wRetVal,wStart,wOffset,wBegin1,wBegin2;
char c,str1[80];
float fVal,fDelta;

if (check_isoda_calibrator() != NoError)
   {
   printf("\nISODA Calibrator not found, call for order"); getch();
   return;
   }
printf("\nCheck ISODA Calibrator OK");

wStart=140;
for (i=0; i<MaxChannel; i++)
    {
    if (ISODA_AnalogOutput(i,wStart) != NoError)
       {
       printf("\nWriting initial -10V voltage error"); getch();
       return;
       }
    }
delay_ms(1000);
printf("\nWriting initial -10V voltage OK");

fDelta=20.2/16384.0;
for (i=0; i<MaxChannel; i++)
    {
    printf("\n-------------------------------------");
    printf("\n-10v START chan_%d",i);
    compute_now_neg_voltage(i,&fVal);
    wOffset=(int) ((fVal+10.0)/fDelta);
    wBegin1=wStart-wOffset-4;
    printf(" --> %.4f %d",fVal,wOffset);
    printf(" --> wBegin1=%d",wBegin1);
    goto_neg_10v(i,wBegin1,&wBegin2);
    printf(" : wBegin2=%d",wBegin2);
    wMinV[i]=wBegin2;

    if (kbhit()!=0) {getch(); return;}
    }

save_neg_10v_to_eeprom();
for (i=0; i<100; i++) printf("\7");
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

from_str_to_float2(char str[], float *f)
{
WORD k;
float val;

    k=str[2]-'0'; val=k*10.0;
    k=str[3]-'0'; val+=k;
    k=str[5]-'0'; val+=k*0.1;
    k=str[6]-'0'; val+=k*0.01;
    k=str[7]-'0'; val+=k*0.001;
*f=val;
}

compute_now_pos_voltage(WORD wChannel, float *fVal)
{
WORD i;
float val;

if (wChannel<8)
   {
   strcpy(str1,"#010"); i=wChannel;
   }
else
   {
   strcpy(str1,"#020"); i=wChannel-8;
   }

str1[3]=i+'0';
SEND_CMD(iComPort,str1,TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str2,TIMEOUT,iChksum);
from_str_to_float2(str2,&val);
/* printf("\nstr1=%s --> str2=%s ",str1,str2); */
*fVal=val;
}

goto_pos_10v(WORD wChannel, WORD wStart, WORD *wMinV)
{
WORD i,wNow;
float fVal;

wNow=wStart;
for (i=0; i<40; i++)
    {
    printf("\nwNow=%d",wNow);
    ISODA_AnalogOutput(wChannel,wNow);
    delay_ms(1000);
    compute_now_pos_voltage(wChannel,&fVal);
    printf(" --> %.4f",fVal);
    if (fVal<10.0)
       {
       *wMinV=wNow+1;
       return(NoError);
       }
    if (kbhit()!=0) {getch(); return;}
    wNow--;
    }

return(CheckError);
}

fun_8()
{
WORD i,j,k,wRetVal,wStart,wOffset,wBegin1,wBegin2;
char c,str1[80];
float fVal,fDelta;

if (check_isoda_calibrator() != NoError)
   {
   printf("\nISODA Calibrator not found, call for order"); getch();
   return;
   }
printf("\nCheck ISODA Calibrator OK");

wStart=16000;
for (i=0; i<MaxChannel; i++)
    {
    if (ISODA_AnalogOutput(i,wStart) != NoError)
       {
       printf("\nWriting initial +10V voltage error"); getch();
       return;
       }
    }
delay_ms(1000);
printf("\nWriting initial +10V voltage OK");

fDelta=20.2/16384.0;
for (i=0; i<MaxChannel; i++)
    {
    printf("\n-------------------------------------");
    printf("\n+10v START chan_%d",i);
    compute_now_pos_voltage(i,&fVal);
    wOffset=(int) ((10.0-fVal)/fDelta);
    wBegin1=wStart+wOffset+4;
    printf(" --> %.4f %d",fVal,wOffset);
    printf(" --> wBegin1=%d",wBegin1);
    goto_pos_10v(i,wBegin1,&wBegin2);
    printf(" : wBegin2=%d",wBegin2);
    wMaxV[i]=wBegin2;
    printf("=(%d)",wMaxV[i]-wMinV[i]);

    if (kbhit()!=0) {getch(); return;}
    }

save_pos_10v_to_eeprom();
for (i=0; i<100; i++) printf("\7");
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

from_str_to_float3(char str[], float *f)
{
WORD k;
float val;

    k=str[2]-'0'; val+=k;
    k=str[4]-'0'; val+=k*0.1;
    k=str[5]-'0'; val+=k*0.01;
    k=str[6]-'0'; val+=k*0.001;
    k=str[7]-'0'; val+=k*0.0001;
*f=val;
}

compute_now_current(WORD wChannel, float *fVal)
{
WORD i;
float val,r;

if (wChannel<8)
   {
   strcpy(str1,"#030"); i=wChannel;
   }
else
   {
   strcpy(str1,"#040"); i=wChannel-8;
   }

str1[3]=i+'0';
SEND_CMD(iComPort,str1,TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str2,TIMEOUT,iChksum);
from_str_to_float3(str2,&val);
r=rr[wChannel];
*fVal=val*1000.0/r;
/* printf("\nstr1=%s --> str2=%s ,val=%f,fval=%f",str1,str2,val,(*fVal)); */
}

goto_0_ma(WORD wChannel, WORD wStart, WORD *wMinV)
{
WORD i,wNow;
float fVal;

wNow=wStart;
for (i=0; i<40; i++)
    {
    printf("\nwNow=%d",wNow);
    ISODA_AnalogOutput(wChannel,wNow);
    delay_ms(1000);
    compute_now_current(wChannel,&fVal);
    printf(" --> %.4f",fVal);
    if (fVal>=0.001)
       {
       *wMinV=wNow-1;
       return(NoError);
       }
    if (kbhit()!=0) {getch(); return;}
    wNow++;
    }

return(CheckError);
}

fun_9()
{
WORD i,j,k,wRetVal,wStart,wOffset,wBegin1,wBegin2;
char c,str1[80];
float fVal,fDelta;

if (check_isoda_calibrator() != NoError)
   {
   printf("\nISODA Calibrator not found, call for order"); getch();
   return;
   }
printf("\nCheck ISODA Calibrator OK");

wStart=(wMaxV[0]+wMinV[0])/2+50;
for (i=0; i<16; i++)
    {
    if (ISODA_AnalogOutput(i,wStart) != NoError)
       {
       printf("\nWriting initial 0mA Current error"); getch();
       return;
       }
    }
delay_ms(2000);
printf("\nWriting initial 0mA Current OK");

fDelta=21.0/8192.0;
for (i=0; i<MaxChannel; i++)
    {
    printf("\n-------------------------------------");
    printf("\n0mA START chan_%d",i);
    compute_now_current(i,&fVal);
    wOffset=(fVal/fDelta);
    wBegin1=wStart-wOffset-4;
    printf(" --> %.4f %d",fVal,wOffset);
    printf(" --> wBegin1=%d",wBegin1);
    goto_0_ma(i,wBegin1,&wBegin2);
    printf(" : wBegin2=%d",wBegin2);
    wMinI[i]=wBegin2;

    if (kbhit()!=0) {getch(); return;}
    }

save_0mA_to_eeprom();
for (i=0; i<100; i++) printf("\7");
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

goto_20_ma(WORD wChannel, WORD wStart, WORD *wMinV)
{
WORD i,wNow;
float fVal;

wNow=wStart;
for (i=0; i<40; i++)
    {
    printf("\nwNow=%d",wNow);
    ISODA_AnalogOutput(wChannel,wNow);
    delay_ms(1000);
    compute_now_current(wChannel,&fVal);
    printf(" --> %.4f",fVal);
    if (fVal>=20.0)
       {
       *wMinV=wNow-1;
       return(NoError);
       }
    if (kbhit()!=0) {getch(); return;}
    wNow++;
    }

return(CheckError);
}

fun_a()
{
WORD i,j,k,wRetVal,wStart,wOffset,wBegin1,wBegin2;
char c,str1[80];
float fVal,fDelta;

if (check_isoda_calibrator() != NoError)
   {
   printf("\nISODA Calibrator not found, call for order"); getch();
   return;
   }
printf("\nCheck ISODA Calibrator OK");

wStart=wMinI[0]+7700;
for (i=0; i<16; i++)
    {
    if (ISODA_AnalogOutput(i,wStart) != NoError)
       {
       printf("\nWriting initial 0mA Current error"); getch();
       return;
       }
    }
delay_ms(2000);
printf("\nWriting initial 20mA Current OK");

fDelta=21.0/8192.0;
for (i=0; i<MaxChannel; i++)
    {
    printf("\n-------------------------------------");
    printf("\n20mA START chan_%d",i);
    compute_now_current(i,&fVal);
    wOffset=(20.0-fVal)/fDelta;
    wBegin1=wStart+wOffset-10;

    if (ISODA_AnalogOutput(i,wBegin1) != NoError)
       {
       printf("\nWriting initial 0mA Current error"); getch();
       return;
       }
    delay_ms(1000);

    compute_now_current(i,&fVal);
    wOffset=(20.0-fVal)/fDelta;
    wBegin1=wBegin1+wOffset-4;

    printf(" --> %.4f %d",fVal,wOffset);
    printf(" --> wBegin1=%d",wBegin1);
    goto_20_ma(i,wBegin1,&wBegin2);
    printf(" : wBegin2=%d",wBegin2);
    wMaxI[i]=wBegin2;
    wStart=wBegin2-40;
    fDelta=20.0/(float)(wMaxI[i]-wMinI[i]);
    if (kbhit()!=0) {getch(); return;}
    }

save_20mA_to_eeprom();
for (i=0; i<100; i++) printf("\7");
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */
/* 0  to 15 : power-on	value	       */
/* 16 to 31 : voltage calibration data */
/* 32 to 47 : current calibration data */
/* 48 to 63 : reserved for safe value  */

fun_b()
{
WORD i,j,k,high,low;

k=32;
if (ISODA_ReadRom(k,&i)==NoError)
   {
   BASE1=(i&0xff)+16050;
   printf("\nBASE1=%d",BASE1);
   }
else
   {
   printf("\nEEPROM read error"); getch(); return;
   }

for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+32,&i) == NoError)
       {
       printf("\nChannel_%d, EEPROM=%x, ",k,i);
       low=i&0xff;  wMinV[k]=low; i=i>>8;
       high=i&0xff;  wMaxV[k]=high+BASE1;
       printf("low=%x high=%x",low,high);
       printf(" --> -10V=%d, +10V=%d",wMinV[k],wMaxV[k]);
       }
    else printf(" Read Error");
    }
printf("\nPress any key to continue"); getch();

BASE2=(wMinV[0]+wMaxV[0])/2-100;
for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+48,&i) == NoError)
       {
       printf("\nChannel_%d, EEPROM=%x, ",k,i);
       low=i&0xff;  wMinI[k]=low+BASE2; i=i>>8;
       BASE3=wMinI[0]+7670;	/* for version 1.1 */

       high=i&0xff;  wMaxI[k]=high+BASE3;
       printf("low=%x high=%x",low,high);
       printf(" --> 0mA=%d, 20mA=%d",wMinI[k],wMaxI[k]);
       }
    else printf(" Read Error");
    }
printf("\nPress any key to continue"); getch();
}

save_neg_10v_to_eeprom()
{
WORD i,j,k,high,low;

for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+32,&i) == NoError)
       {
       i=i&0xff00;
       i=i+wMinV[k];
       if (ISODA_WriteRom(k+32,i) == NoError)
	  printf("\nWrite EEPROM_%d [%d] OK",k,wMinV[k]);
       else return(CheckError);
       }
    else return(CheckError);
    }
}

save_pos_10v_to_eeprom()
{
WORD i,j,k,high,low;

BASE1=wMinV[0]+16050;
printf("\nBASE1=%d",BASE1);
for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+32,&i) == NoError)
       {
       low=i&0xff;
       high=wMaxV[k]-BASE1;
       i=low+high*256;
       if (ISODA_WriteRom(k+32,i) == NoError)
	  printf("\nWrite EEPROM_%d [%d] OK",k,wMaxV[k]);
       else return(CheckError);
       }
    else return(CheckError);
    }
}

save_0mA_to_eeprom()
{
WORD i,j,k,high,low;

BASE2=(wMinV[0]+wMaxV[0])/2-100;
printf("\nBASE2=%d",BASE2);
for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+48,&i) == NoError)
       {
       low=wMinI[k]-BASE2;
       high=i&0xff00;
       i=low+high;
       if (ISODA_WriteRom(k+48,i) == NoError)
	  printf("\nWrite EEPROM_%d [%d] OK",k,wMinI[k]);
       else return(CheckError);
       }
    else return(CheckError);
    }
}

save_20mA_to_eeprom()
{
WORD i,j,k,high,low;

BASE3=wMinI[0]+7670; /* for version 1.1 */

printf("\nBASE3=%d",BASE3);
for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_ReadRom(k+48,&i) == NoError)
       {
       low=i&0xff;
       high=wMaxI[k]-BASE3;
       i=low+high*256;
       if (ISODA_WriteRom(k+48,i) == NoError)
	  printf("\nWrite EEPROM_%d [%d] OK",k,wMaxI[k]);
       else return(CheckError);
       }
    else return(CheckError);
    }
}

/* --------------------------------------------------------------------- */

fun_c()
{
char str1[80],str2[80];
WORD k,j;

SEND_CMD(iComPort,"#010",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_0 : %s",str1);
SEND_CMD(iComPort,"#011",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_1 : %s",str1);
SEND_CMD(iComPort,"#012",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_2 : %s",str1);
SEND_CMD(iComPort,"#013",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_3 : %s",str1);
SEND_CMD(iComPort,"#014",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_4 : %s",str1);
SEND_CMD(iComPort,"#015",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_5 : %s",str1);
SEND_CMD(iComPort,"#016",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_6 : %s",str1);
SEND_CMD(iComPort,"#017",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_7 : %s",str1);

if (MaxChannel==16)
{
SEND_CMD(iComPort,"#020",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_8 : %s",str1);
SEND_CMD(iComPort,"#021",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_9 : %s",str1);
SEND_CMD(iComPort,"#022",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_10: %s",str1);
SEND_CMD(iComPort,"#023",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_11: %s",str1);
SEND_CMD(iComPort,"#024",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_12: %s",str1);
SEND_CMD(iComPort,"#025",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_13: %s",str1);
SEND_CMD(iComPort,"#026",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_14: %s",str1);
SEND_CMD(iComPort,"#027",TIMEOUT,iChksum);
RECEIVE_CMD(iComPort,str1,TIMEOUT,iChksum);
printf("\nChannel_15: %s",str1);
}
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_d()
{
WORD i;
float fVal;

for (i=0; i<MaxChannel; i++)
    {
    compute_now_current(i, &fVal);
    printf("\nChannel_%d=%f",i,fVal);
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_e()
{
WORD i,k;

printf("\nD/A value="); scanf("%d",&i);
for (k=0; k<MaxChannel; k++)
    {
    if (ISODA_AnalogOutput(k, i) == NoError) printf("\nChannel_%d : %d", k,i);
    else printf("\nChannel_%d Output Error");
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_f()
{
WORD i,j,k;
float v,zero,delta;

printf("\nval="); scanf("%f",&v);

for (k=0; k<MaxChannel; k++)
    {
    j=compute_hex(wMinV[k], wMaxV[k], v);
    if (ISODA_AnalogOutput(k, j) == NoError) printf("\nChannel_%d : %d", k,j);
    else printf("\nChannel_%d Output Error");
    }
printf("\nPress any key to continue"); getch();
}

WORD compute_hex(WORD mmin, WORD mmax, float in)
{
float delta;
WORD k;

delta=20.0/(float)(mmax-mmin);
k=(int)((in+10.0)/delta+0.5)+mmin;
return(k);
}

/* --------------------------------------------------------------------- */

fun_g()
{
WORD i,j,k;
float v,zero,delta;

printf("\nval="); scanf("%f",&v);
printf("\nV=%f",v);
for (k=0; k<MaxChannel; k++)
    {
    delta=20.0/(float)(wMaxI[k]-wMinI[k]);
    j=(int)(v/delta+0.5)+wMinI[k];
    if (ISODA_AnalogOutput(k, j) == NoError) printf("\nChannel_%d : %d", k,j);
    else printf("\nChannel_%d Output Error");
    }
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_h()
{
WORD i,j,k,ver,h,l;
WORD eeprom[64],high,low;
FILE *stream;
char filename[80];

for (k=0; k<64; k++)
    {
    if (ISODA_ReadRom(k,&j) == NoError) eeprom[k]=j;
    else
       {
       printf("\nRead EEPROM Error");
       printf("\nPress any key to continue"); getch();
       return;
       }
    }

printf("\nSave EEPROM to filename="); scanf("%s",filename);
stream=fopen(filename,"w");
if (stream != NULL)
   {
   k=0;
   for (j=0; j<8; j++)
       {
       for (i=0; i<8; i++)
	   {
	   fprintf(stream,"%x ",eeprom[k]);
	   k++;
	   }
       fprintf(stream,"\n");
       }
   fclose(stream);
   }
else
   {
   printf("\nOpen error");
   printf("\nPress any key to continue"); getch();
   return;
   }

printf("\nSave OK");
printf("\nPress any key to continue"); getch();
}

/* --------------------------------------------------------------------- */

fun_i()
{
WORD i,j,k,ver,h,l;
WORD eeprom[64],high,low;
FILE *stream;
char filename[80];

printf("\nload EEPROM from filename="); scanf("%s",filename);
stream=fopen(filename,"r");
if (stream != NULL)
   {
   for (i=0; i<64; i++)
       {
       fscanf(stream,"%x",&(eeprom[i]));
       }
   fclose(stream);
   }
else
   {
   printf("\nOpen error");
   printf("\nPress any key to continue"); getch();
   return;
   }

printf("\nLoad File OK");

for (k=0; k<64; k++)
    {
    j=eeprom[k];
    if (ISODA_WriteRom(k,j) != NoError)
       {
       printf("\nWrite EPPROM error");
       printf("\nPress any key to continue"); getch();
       return;
       }
    }
printf("\nLoad EEPROM OK");
printf("\nPress any key to continue"); getch();
}
