// 7K87K_do.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_do.exe  ConnectionString   Address   total_channel   DO_value \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("   total_channel:\n");
		printf("      - number of DO's channel\n");
		printf("   DO_value:\n");
		printf("      - DO value to write to the DO module.\n");
	}
	else
	{		
		BOOL err;
		char strErr[32];
		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;
		}
		//Write DO value
		err = pac_WriteDO(hPort, PAC_REMOTE_IO(atoi(argv[2])), atoi(argv[3]), atoi(argv[4]));	
		if(err == FALSE)
		{
			pac_GetErrorMessage(pac_GetLastError(), strErr);
			printf("Write DO'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;
		}	
	}
	return 0;
}
