using System;
namespace HMS_MiCom2022
{
 internal class HMS_MiCom2022_DMAC23
    {
        static void Main()
        {
            string[] Arg = Environment.GetCommandLineArgs();
            string ComName = "COM18";
            int status;
         
            Display("HMS_MiCom2022_DMAC23.exe   ==> how to use MiCom_v2022 for DMAC ");
            Display("Usage : HMS_MiCom2022_DMAC \"comName\"  ");

            // contrôle / recherche du COMPORT sur la ligne d'appel de l'éxécutable
            if (Arg.Length == 1)
            {
                Display("no com name speficied in the argument list , so " + ComName + " is use");
            }
            else
            {
                ComName = Arg[1];
                Display(ComName + " will be use as COM");
            }

            // usage du myCom en tant qu'instance du type MiCom_v2022
            // the MiCom_v2022.dll assembly is referenced  project:
            // so the next line is valid and 'myCom' will be the instance of the MiCom_v2022 type
            Midi_Ingenierie.MiComm_2022 myCom = new Midi_Ingenierie.MiComm_2022();
            Display("myCom is the instance of MiCom_v2022 type , from C# new Midi_Ingenierie.MiComm_2022() ");
            Display();

            // identification de l'assembly
            Display("MiComm_2022.Identity ==> " + Midi_Ingenierie.MiComm_2022.Identity);


            // initialisation d'une ligne de com sur un driver MAC23
            Display("Open Com Line        ==> myCom.Open('DMAC','" + ComName +" ','38400')");
            status = myCom.Open("DMAC", ComName, "38400");
            //status MUST be 0x0000
            if (status != 0)
            {
                Display("Status :" + status.ToString("X4"));
                Display("Bad Status !! check ComPort");
                Quit();
                return;
            }

            //Request_VErsion
            Display("Request_VErsion      ==> myCom.Send(\"00RVE\")");
            myCom.Send("00RVE");
            Display("myCom.Reponse        ==> "+myCom.Reponse);



            //MODULE_RESET            
            Display("Module_REset         ==> myCom.Send(\"00MRE\")");
            myCom.Send("00MRE");
            status = myCom.Status;
            if (status > 0x8000)
            {
                if (status != 0xA000)
                {
                    Display("Status :" + status.ToString("X4"));
                    Display("Bad Status !! check Com , alimentation, connection ....");
                    Quit();
                    return;
                }
            }
            Delay(1000);

            ConsoleKey key_ = Ask("Start motor, only if motor is FREE  [Y/N] ?");
            if (key_ != ConsoleKey.Y)
            {
                Quit();
                return;
            }

            Display();
            Display("Torque_RAtio         ==> myCom.Send(\"00#TRA := 50\")");
            myCom.Send("00#TRA :=50");

            Display("commande POSition    ==> myCom.Send(\"00#POS := 0\")");
            myCom.Send("00#POS := 0");

            Display("Move_SPeed 300 tr/mn ==> myCom.Send(\"00MSP 30000\")");
            myCom.Send("00MSP 30000");
            Delay(2000);

            Display("Move_SPeed 100 tr/mn ==> myCom.Send(\"00MSP 10000\")");
            myCom.Send("00MSP 10000");
            Delay(2000);

            Display("Move_SPeed 400 tr/mn ==> myCom.Send(\"00MSP 40000\")");
            myCom.Send("00MSP 40000");
            Delay(2000);

            Display("Move_SPeed 200 tr/mn ==> myCom.Send(\"00MSP 20000\")");
            myCom.Send("00MSP 20000");
            Delay(2000);

            Display("STOP                 ==> myCom.Send(\"00STOP\")");
            myCom.Send("00STOP");
            Delay(2000);

            Display("POWer  OFF           ==> myCom.Send(\"00POW OFF\")");
            myCom.Send("00POW OFF");
           
            Quit();
        }

         

        private static void Delay(int delay) => System.Threading.Thread.Sleep(delay);
        private static void Display(string Texte = "") => Console.WriteLine(Texte);
        private static void Quit() => Ask("[ESC] = All is done ,Quit the program");

        private static ConsoleKey Ask(string Texte)
        {
                Console.WriteLine();
                Console.Write(Texte);
                return Console.ReadKey().Key;
        }
       
 }
}
    

