using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace UartMode_CSharp
{
    class Program
    {
        [DllImport("mxsp.dll")]
        public static extern int mxsp_open();

        [DllImport("mxsp.dll")]
        public static extern void mxsp_close();

        [DllImport("mxsp.dll")]
        public static extern int mxsp_get_interface(int fd, int port);

        [DllImport("mxsp.dll")]
        public static extern int mxsp_set_interface(int fd, int port, int mode);

        static private int _fd;
        static private int _port;

        public enum SerialMode : byte
        {
            RS485_2WIRE_MODE = 0x0,
            RS422_RS485_4WIRE_MODE = 0x1,
            RS232_MODE = 0x2
        }


        static void Main(string[] args)
        {

            int nPort;
            int nMode;
            nPort = 1;
            nMode = 1;
            string smode;

            System.Console.WriteLine("Serial Interface Test Program");
            System.Console.WriteLine("\t (0) Exit Program");
            System.Console.WriteLine("\t (1) Display Serial Interface");
            System.Console.WriteLine("\t (2) Set Serial Interface");

            //read a char.
            string sin = System.Console.ReadLine();
            int n = int.Parse(sin);


            do
            {
                switch (n)
                {
                    // if char == '1', display current mode
                    case 1:

                        //System.Console.Write("Input the Port Number (1~4) = ");
                        //smode = System.Console.ReadLine();
                        //nPort = int.Parse(smode);

                        //open port
                        Open();


                        for (int i = 0; i < 4; i++)
                        {
                            //Get current interface
                            nMode = GetInterface(i);

                            System.Console.WriteLine("COM{0} = {1}", i + 1, (SerialMode)nMode);
                        }

                        //close port
                        Close();


                        break;
                    // if char == '2', set uart mode
                    case 2:
                        System.Console.Write("Input the Port Number (1~4) = ");
                        smode = System.Console.ReadLine();
                        nPort = int.Parse(smode);

                        if ((nPort - 1) < 0 || (nPort - 1) > 3)
                        {
                            System.Console.Write("The port value you input must between 1 ~ 4 \n");
                            break;
                        }

                        //open port
                        Open();

                        // read port value from console
                        System.Console.Write("Input the Mode value (0:RS485-2WIRE, 1:RS422_RS485_4WIRE, 2:RS232) = ");
                        smode = System.Console.ReadLine();
                        nMode = int.Parse(smode);

                        //Set uart mode
                        SetInterface(nMode, nPort - 1);

                        //Check if we set uart mode sucessfully
                        nMode = GetInterface(nPort - 1);

                        //close port
                        Close();

                        System.Console.WriteLine("COM{0} = {1}", nPort, (SerialMode)nMode);
                        break;

                }
                System.Console.WriteLine("UART Mode Test Program");
                System.Console.WriteLine("\t (0) Exit Program");
                System.Console.WriteLine("\t (1) Display UART Mode");
                System.Console.WriteLine("\t (2) Set  UART Mode");
                sin = System.Console.ReadLine();
                n = int.Parse(sin);
            } while (n != 0);

        }


        static public bool Open()
        {
            _fd = mxsp_open();
            return true;
        }

        static public void Close()
        {
            mxsp_close();
        }

        static public int GetInterface(int port)
        {
            _port = port;
            //int nBytesReturn = 0;
            int nDIOBack = 0;

            if (_port < 0 || _port > 4)
            {
                return -1;
            }

            nDIOBack = mxsp_get_interface(_fd, _port);
            return nDIOBack;
        }

        static public int SetInterface(int mode, int port)
        {
            _port = port;
            //int nBytesReturn = 0;
            int nDIOBack = 0;

            if (_port < 0 || _port > 4)
            {
                return -1;
            }

            nDIOBack = mxsp_set_interface(_fd, _port, mode);
            return nDIOBack;
        }
    }
}
