// 7K87K_di.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 < 4)
	{				
		printf("ICPDAS (R) SDK Demo for XP-8000\n\n");
		printf("This demo is for remote 87K and 7K modules.\n");
		printf("usage: 7K87K_di.exe  ConnectionString   Address   iDI_TotalCh \n\n");
		printf("where\n");
		printf("   ConnectionString:\n");
		printf("      - 87k/7k   -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("   iDI_TotalCh:\n");
		printf("      - number of DI's channel\n");
	}
	else
	{		
		BOOL err;
		char strErr[32];
		DWORD lDI_Value = 0;
		char connectionstring[32];

		//Open com port except COM1,COM1 is for local.
		//This demo is for remote 87K and 7K modules.
		sprintf(connectionstring, "%s",argv[1]);
		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 DI value
		err = pac_ReadDI(hPort, PAC_REMOTE_IO(atoi(argv[2])), atoi(argv[3]), &lDI_Value);
		if(err == FALSE)
		{
			pac_GetErrorMessage(pac_GetLastError(), strErr);
			printf("Read DI's Error: %s. The error code is %x\n", strErr, pac_GetLastError());
			return 0;
		}
		//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;
		}			
		//Show DI value
		printf("The DI's Value is %d\n", lDI_Value);
	}
	return 0;
}
