/* Test with I-87084 Module  of LinPAC.
   
  Default settings for the I-87084W modules are as follows:
# Protocol: DCON
# Module Address: 01 
# Type: Type 50, up counter
# Baud Rate: 115200 bps
# To communicate with I-87084W, user need to send address as 0 for the module in LinPAC.
  ==> $AAM: getsendreceive 1 1 1 '$00M' 

 */

#include<stdio.h>
#include<stdlib.h>
#include "msw.h"

char szSend[128], szReceive[128];
DWORD dwBuf[12];
float fBuf[64];

int main()
{
	int id, wRetVal, quit=0, channel=0;
	DWORD temp;

	//Check Open_Slot
	wRetVal = Open_Slot(0);
	if (wRetVal > 0) {
		printf("open Slot failed!\n");
		return (-1);
	}
	
	//Check Open_Com1
	wRetVal = Open_Com(COM1, 115200, Data8Bit, NonParity, OneStopBit);
	if (wRetVal > 0) {
		printf("open port failed!\n");
		return (-1);
	}
	
	//--- digital output ----  **(DigitalOut_87K()**)
	dwBuf[0] = 1;				         // COM Port
	dwBuf[1] = 00;				       // Address
	dwBuf[2] = 0x87084;		       // module ID: 0x87084
	dwBuf[3] = 0;				         // CheckSum disable
	dwBuf[4] = 100;				       // TimeOut , 100 msecond
	dwBuf[6] = 1;                // flag: 0=no save, 1=save send/receive string
	//dwBuf[5] = channel;			     // channel number
	dwBuf[7] = 4;                // wSlot: Slot number
	dwBuf[8] = 0;	               // wReadMode = 0 read counter 
  	
  ChangeToSlot(dwBuf[7]);      // wSlot: Slot number 
  printf("Please choose the channel and press Enter key:  ");  
  scanf("%d",&channel);   		
  dwBuf[5] = channel;                 
  
  while (quit!=1)
  {
    printf("\n");
    printf("            LinPAC with  I-87084     \n");
    printf("============================================ \n\r");
    printf(" 0) ReadCounter            3) ClearCounter   \n\r");
    printf(" 1) ReadCounter_All        9) Exit           \n\r");
    printf(" 2) ReadCNTOverflow                          \n\r");
    printf("============================================ \n\r");
       
    printf("Please choose the item and press Enter key:  ");  
    scanf("%d",&id);   		
    switch (id)
    {		
       case 0:   // #AAN
            wRetVal =ReadCounter(dwBuf, fBuf, szSend, szReceive);
            if (wRetVal)
			           printf("ReadCounter of I-87084W error! Error Code=%d\n", wRetVal);
		        else	
	               printf("Read Ch%d Counter of I-87084W: %s\n", dwBuf[5],szReceive);
            break;
      
       case 1:   // #AA
            wRetVal =ReadCounter_All(dwBuf, fBuf, szSend, szReceive);
  	        if (wRetVal)
			           printf("ReadCounter_All of I-87084W error! Error Code=%d\n", wRetVal);
		        else	
	               printf("ReadCounter_All of I-87084W: %s\n", szReceive);
            break;
      
       case 2:   //$AA7
  	        wRetVal =ReadCNTOverflow(dwBuf, fBuf, szSend, szReceive);
  	        if (wRetVal)
			           printf("Reads the counter overflow status of each channel error! Error Code=%d\n", wRetVal);
		        else	
	               printf("eads the counter overflow status of Ch%d: %s\n", dwBuf[5],szReceive);
            break;
    		
  		case 3:  //$AA6N
  	        wRetVal =ClearCounter(dwBuf,  fBuf, szSend, szReceive);
  	        if (wRetVal)
			           printf("Clear Counter of I-87084W error! Error Code=%d\n", wRetVal);
		        else	
	               printf("Clear Ch%d Counter of I-87084W: %s\n", dwBuf[5], szReceive);
  	        break;
  	  case 9:   //exit
      default:
            quit=1;
            printf("Exit the program.\n");
            break;
     }  
  }//while
 	Close_Com(COM1);
                     Close_Slot(0);
	Close_Slot(dwBuf[7]);
	return 0;
}
