// 7K87K_ai.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "PACSDK.h"


int main(int argc, char* argv[])
{
	if(argc < 5)
	{		
		printf("ICPDAS (R) SDK Demo for XP-8000\n\n");
		printf("This demo is for remote 87K and 7K modules.\n");
		printf("usage: 7K87K_ai.exe  ConnectionString   Address   iChannel   iAI_TotalCh \n\n");
		printf("where\n");
		printf("   ConnectionString:\n");
		printf("      -the COM port, baud rate, parity bits,\n");
		printf("       data bits, and stop bits.\n\n"); 	
		printf("   Address:\n");
		printf("      - number of slot for remote modules\n");
		printf("   iChannel:\n");
		printf("      - channel that is written the AO value\n");
		printf("   iAI_TotalCh:\n");
		printf("      - number of AI's channel\n");
	}
	else
	{		
		BOOL err;
		char strErr[32];
		float fValue = 0;
		char connectionstring[32];
		sprintf(connectionstring, "%s",argv[1]);
		//Open com port except COM1,COM1 is for local.
		//This demo is for remote 87K and 7K modules.
		HANDLE hPort = uart_Open(connectionstring);
		if(hPort == INVALID_HANDLE_VALUE)
		{
			pac_GetErrorMessage(pac_GetLastError(), strErr);
			printf("Open Error: %s. The error code is %x\n", strErr, pac_GetLastError());
			return 0;
		}
		//Read AI value
		err = pac_ReadAI(hPort, PAC_REMOTE_IO(atoi(argv[2])), atoi(argv[3]), atoi(argv[4]), &fValue);
		if(err == FALSE)
		{
			pac_GetErrorMessage(pac_GetLastError(), strErr);
			printf("Read AI's Error: %s. The error code is %x\n", strErr, pac_GetLastError());
			return 0;
		}		
		printf("The AI's value is %f\n", fValue);
		//Close com port
		err = uart_Close(hPort);	
		if(err == FALSE)
		{
			pac_GetErrorMessage(pac_GetLastError(), strErr);
			printf("Close Error: %s. The error code is %x\n", strErr, pac_GetLastError());
			return 0;
		}	
	}
	return 0;
}
