/*  Copyright (C) MOXA Inc. All rights reserved.

    This software is distributed under the terms of the
    MOXA License.  See the file COPYING-MOXA for details.
*/

/*
	index[n]: 1  ; Port 1
			  2  ; Port 2
	          
			 ....
	mode[n]: 0  ; RS232
	         1  ; RS485-2W
			 2  ; RS422
			 3  ; RS485-4W
	         
*/

#include "stdafx.h"
#include <windows.h>
#include "..\release\devices.h"
char mode_array[][20]={"RS232", "RS485-2W", "RS422","RS485-4W"};

int _tmain(int argc, _TCHAR* argv[])
{
	int port=0,mode=0;
	int n=0;
	int baudrate=0;
	int databits=0;

	printf("UART Mode Test Program\n");
    printf("\t (0) Exit Program\n");
    printf("\t (1) Display UART Mode\n");
    printf("\t (2) Set UART Mode\n");
	
	scanf("%d",&n);
	
	unsigned int fd;
	
	
	do
    {
        switch (n)
        {
            // if char == '1', display the UART Mode
            case 1:
				printf("Input the Port Number = \n");
				scanf("%d",&port);

				//Open the serial port
				fd=mxsp_open(port);

				mode=mxsp_get_interface(fd);
				if(mode==(-1))
				{
					printf("Invalid value!!\n");
					break;
				}

				printf("COM%d=%s\n",port,mode_array[mode]);

				//Close the serial port
				mxsp_close(fd);
                break;
            // if char == '2', Set the UART Mode
            case 2:
             
				//Get Port Number
				printf("Input the Port Number = \n");
				scanf("%d",&port);

				//Open the serial port
				fd=mxsp_open(port);
			
				//Get Mode Value
                printf("Input the Mode value (0 ~ 3) = ");
				scanf("%d",&mode);
		
				//Set UART Mode
				if(mxsp_set_interface(fd,mode)==-1)
				{
					printf("Invalid value!!\n");
					printf("Set UART Mode Fail!!\n");
				}                
				else
				{
					printf("COM%d=%s\n",port,mode_array[mode]);
				}

				//Close the serial port
				mxsp_close(fd);
                break;
			case 3:
				//Get Port Number
				printf("Input the Port Number = \n");
				scanf("%d",&port);

				//Open the serial port
				fd=mxsp_open(port);
			
				//Get Baudrate Value
				baudrate=mxsp_get_baudrate(fd);
				printf("Current baudrate = %d\n",baudrate);
		
				//Close the serial port
				mxsp_close(fd);
				break;
			
        }
		getwchar();
       scanf("%d",&n);
    } while (n != 0);

	return 0;
}

