#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <fcntl.h>
#include <io.h>
#include <dos.h>

#define TRUE 1
#define FALSE 0

#define port_putch(port, sta) 	printf("\n%c", sta); async_putch(port, sta)

#include "async.h"

#include "pfkey.h"

#include "idexx.h"

void query_reader(int, char);
void help();
int read_line(int);

extern  struct async_portS async_port[NUMBEROFPORTS];
extern  int UART_ports[];

int     main(int argc, char *argv[])
{
    char    ch;
    int     done = 0, nrd;
    char    s[200];
    long    speed;
        int     comport;

        comport = COM2;
        if (argc > 1) {
                comport = atoi(argv[1])-1;
        }

        speed = 4800L;
        if (argc > 2) {
                speed = atol(argv[2]);
    }

/*
    clrscr();
*/

    printf("##############################################################################\n");
    printf("##  COM PORT:   %d                          SPEED:  %-6lu                   ##\n", comport+1, speed);
    printf("##  WORDLENGTH: 7                          PARITY: EVEN                     ##\n");
    printf("##############################################################################\n");

    help();

    open_port(comport, speed, LCR_PEVEN, 7, 1);

    set_parity(comport, LCR_PEVEN);
    set_baudrate(comport, 4800);
    set_stopbits(comport, 2);
    set_wordlength(comport, 7);
    init_fifo(comport, TRUE);

    async_set_DTR(comport, TRUE);
    async_set_RTS(comport, TRUE);

    set_handshaking(comport, 0, FALSE);

    while (!done) {

        if (async_gets(comport, s, 59)) {
            printf(s);
        }

        if (kbhit()) {
            ch = getch();
            if (ch>0) {
            printf("\n((%d))", ch);
            nrd=1;

            switch(ch) {
                case ESC:
                   done=1;
                   nrd=0;
                   break;

                case F1:
                   printf("Connecting to Microwell Reader 210\n");
                   query_reader(comport, '!');
                   nrd=0;
                   break;
                case F2:
                   printf("\nStart of measurement\n");
                   query_reader(comport, 'S');
                   nrd=0;
                   break;
                case F3:
                   printf("\nStop measurement and/or data transfer.\n");
                   nrd=0;
                   query_reader(comport, 'H');
                   break;
                case F4:
                   printf("\nRequest for data ... ROW-WISE\n");
                   s[0]=0;
                   while (1) {
                      query_reader(comport, 'r');
                      if (read_line(comport)>0) break;
                      }
                   nrd=0;
                   break;
                case F5:
                   printf("\nRequest for data ... COLUMN-WISE\n");
                   query_reader(comport, 'c');
                   nrd=0;
                   break;
                case F10:
                   printf("\nEnd of remote control mode.\n");
                   query_reader(comport, 'E');
                   nrd=0;
                   break;
                case F7:
                   help();
                   nrd=0;
                   break;

                default:
                   break;

                } /* endswitch */


            if (nrd==1) {
               printf("--->");
               port_putch(comport, ch);
               printf("%d %c\n", ch, ch);
               }
  
        }

    }
   }
   printf("\nExiting from program ...\n");

    close_port(comport, 0, 0);

    return (0);
}

void query_reader(int comport, char cc) {

async_putch_timeout(comport, 0x02, 100);
async_putch_timeout(comport, ' ' , 100);
async_putch_timeout(comport, cc  , 100);
async_putch_timeout(comport, ' ' , 100);
async_putch_timeout(comport, 13  , 100);

}


int read_line(int comport) {
char ch, dd[100], col[20][100];
char help[200];
int i=0, j, l, w;

for(;(ch=async_getch(comport))!=13;) {
   if (ch>0) {
      help[i++]=ch;
      } 
   }

if(help[1]==' ' && help[2]=='.') return 1;

help[i]=0;

for(i=0; help[i]>0 && help[i]!=':'; i++);

w=0;
l=i+2;
for(j=l; help[j]>0 && help[j]!=13; j++)
    if (help[j]==' ') {
       strncpy(col[w], help+l, j-l);
       col[w][j-l]=0;
       printf("%s ", col[w]);
       w++; l=j+1;      
       }
printf("\n");

return 0;
}




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

void help() {
printf("--------------------------------------\n");
printf(" F1  - Connect to reader\n");
printf(" F2  - Start of measurement\n");
printf(" F3  - Stop measurement\n");
printf(" F4  - Transmit data ROW-WISE\n");
printf(" F5  - Transmit data COLUMN-WISE\n");
printf(" F10 - End communication with reader\n");
printf(" F12 - This help\n");

   }

/*
                        case 61: //F3 -- Toggle XONXOFF
                            set_handshaking(comport, HSHAKE_XONXOFF, !async_xonxoff_enabled(comport));
                            printf("XONXOFF: %d\n", async_xonxoff_enabled(comport));
                            break;
                        case 62: //F4
                            set_handshaking(comport, HSHAKE_RTSCTS, !async_rtscts_enabled(comport));
                            printf("RTSCTS: %d\n", async_rtscts_enabled(comport));
                            break;
                        case 63: //F5
                            set_handshaking(comport, HSHAKE_DTRDSR, !async_dtrdsr_enabled(comport));
                            printf("DTRDSR: %d\n", async_dtrdsr_enabled(comport));
                            break;
                        case 66: //F8
                            async_set_DTR(comport, !async_DTR_status(comport));
                            printf("DTR: %d\n", async_DTR_status(comport));
*/

