// SerialInterfaceEpm3032.cpp : Defines the entry point for the console application.
//



#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include "mxsp.h"


#define MAX_SERIAL_PORT_NUMBER			2
#define MAX_SLOT_NUMBER			2

const int PCI_PORT_INDEX=4;

int _tmain(int argc, _TCHAR* argv[])
{
	char	*interface_array[] = { "RS232", "RS485-2W", "RS422/RS485-4W" }; //{RS232, RS485-2W, RS485-4W, RS485-4W}
	
	HANDLE	hSerial;
	int		port_no;
	int		nRet = 0;
	int		port = 0, mode = 0;
	int		n=0;

	int port_list;
	int port_index = 0 ,port_interface = 0;
	WCHAR sin,smode;
	bool AccessToSetFlag = false;

	printf("Serial Interface Test Program\n");
    printf("\t (0) Exit Program\n");
    printf("\t (1) Display Serial Interface\n");
   	printf("\t (2) Set Serial Interface\n");

	sin=getwchar();
	n=_wtoi(&sin);
	int portsNameLength = 0; 
	int* PortsIndex = (int*)malloc( (MAX_SERIAL_PORT_NUMBER*MAX_SLOT_NUMBER) *sizeof(int));
	for(int i=0; i<(MAX_SERIAL_PORT_NUMBER*MAX_SLOT_NUMBER) ;i++){
		PortsIndex[i] = -1;
	}

	do
    {
        switch (n)
        {
            // if char == '1', display the serial interface
            case 1:

				port_list = msxp_list_interface(PortsIndex);
				hSerial = mxsp_open();

				for(int i = 0;i<(MAX_SERIAL_PORT_NUMBER *MAX_SLOT_NUMBER) ;i++){
					//printf("     %d\n",PortsIndex[i]);
					if(PortsIndex[i]!=-1){
						port_interface = mxsp_get_interface(hSerial , PortsIndex[i]);
						printf("COM%d = %s\n",PortsIndex[i] , interface_array[port_interface]);
					}
				}
				
				//Close DIO
                mxsp_close(hSerial);
				break;

			case 2:
				//hSerial=mxsp_open();
				bool flag = false;
				int port_no = 0;
				getwchar();
				printf("The exist ports:  \n");
				port_list = msxp_list_interface(PortsIndex);
				hSerial = mxsp_open();

				for(int i = 0;i<(MAX_SERIAL_PORT_NUMBER *MAX_SLOT_NUMBER) ;i++){
					if(PortsIndex[i]!=-1){
						port_interface = mxsp_get_interface(hSerial , PortsIndex[i]);
						printf("COM%d = %s\n",PortsIndex[i] , interface_array[port_interface]);
					}
				}

				printf("\n");
				
				printf("Please input the Port Number, COM=    \n");
				smode = getwchar();
				port_index =_wtoi(&smode);
				for(int i = 0;i<(MAX_SERIAL_PORT_NUMBER *MAX_SLOT_NUMBER) ;i++){
					if(port_index == PortsIndex[i]){
						flag = true;
					}
				}
				if(flag == false){
					printf("The Port Number is out of range.\n");
					return -1;
				}

				getwchar();
				printf("Input the value (0:RS232, 1:RS485-2W, 2:RS422/RS485-4W ) = ");
				smode = getwchar();
				port_interface = _wtoi(&smode);

				printf("%d\n",port_interface);

				if((port_interface != 0) && (port_interface != 1) && (port_interface != 2) && (port_interface != 3) ){
					printf("Input value must be : (0:RS232, 1:RS485-2W, 2:RS422/RS485-4W ) \n");
					system("pause");
					return -1;
				} else {
		
					//Open serial port
					hSerial = mxsp_open();
				
					//Set serial interface
					nRet = mxsp_set_interface(hSerial,port_index ,port_interface);
				
				
					if(nRet == -1)
					{
						printf("Set serial interface fail!\n");
					}
					else
					{
						printf("Set serial interface success!\n");
					}

					//Close serial port
					mxsp_close(hSerial);
					break;
				}

		}
		
		printf("\n");
		printf("Serial Interface Test Program\n");
		printf("\t (0) Exit Program\n");
		printf("\t (1) Display Serial Interface\n");
   		printf("\t (2) Set Serial Interface\n");
		getwchar();
        sin = getwchar();
        n = _wtoi(&sin);
    } while (n != 0);
	//HANDLE	hSerial;
	
	system("pause");
	return 0;
}

