コード例 #1
0
        static int Main(string[] args)
        {
            //Console.SetWindowSize(40, 15);  causes crash when redirected
            const string FFID = "99";
            const string FMID = "19";
            const string XLID = "10";
            const string FMLID = "15";
            const bool PING = true;
            const bool DESCR = !PING;
            bool motorized = false;

            int pause = 30; //3 second delay before closing console

            string Assy_version =
                Assembly.GetExecutingAssembly().GetName().Version.ToString();

            //quit if no command line commands
            if (args.Length == 0)
            {
                Console.WriteLine("WARNING No COM port\r\n "
                                 + "specified or incorrect format.\r\n");
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
                return -1;
            }

            //create new instance of Flip-Flat Library
            FlatFieldLibrary FF = new FlatFieldLibrary();

            //temporary storage of messages to be displayed or logged
            string tempmsg = string.Empty;
            //set up a stringbuilder for messages
            //to send to the console and log
            StringBuilder fullmsg = new StringBuilder(); //if no S cmd
            StringBuilder tersemsg = new StringBuilder();//
            StringBuilder logmsg = new StringBuilder();

            //display this message if could not connect to flip-flat
            string failedMsg = "WARNING Could not \r\n"
                                +"connect to Alnitak Product.\r\n";

            //milliseconds to wait between status reports from Flip_Flat
            int delay = 500;

            //initial stringbuilder string
            tempmsg="\r\nAlnitak Astro Flip-Flat/Flat-Man Controller\r\n";
            fullmsg.Append(tempmsg);
            logmsg.Append(tempmsg);
            tempmsg = string.Empty;
            //add date, time
            tempmsg = DateTime.Now.ToString() + "\r\n";
            fullmsg.Append(tempmsg);
            logmsg.Append(tempmsg);
            tempmsg = string.Empty;
            string waitFor = string.Empty;

            // get com port that Flip-Flat is connected to
            // (must be the first argument and must be numeric)
            string cp = string.Empty;
            int num = 0;
            int ic = 0;
            bool test = int.TryParse(args[0], out num);
            if (test == true)
            {
                cp = args[0];
            }
            else if (args[0].ToUpper() == "A")
            {
                try
                {
                    FF.popAADevListUsing(DESCR);
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }

                StringBuilder builder = new StringBuilder();
                if (FF.AaDevices != null)
                {
                    builder.Append(FF.AaDevices.Count.ToString() + " Alnitak Device(s) Found:\n");
                    foreach (device d in FF.AaDevices)
                    {
                        builder.Append(d.AAProdIDName);
                        builder.Append(" on ");
                        builder.Append(d.ComPort);
                        builder.Append(",\n");
                    }
                }
                else
                    builder.Append("No Devices Found");

                tempmsg = builder.ToString();
                Console.WriteLine(tempmsg);
                pause = 30;
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
                return 0;
            }
            else
            {
                tempmsg = args[0].ToString()
                    + " WARNING No COM port specified \r\n"
                    + "or incorrect format.\r\n";

                Console.WriteLine(tempmsg);
                pause = 30;
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
                return -1;
            }

            fullmsg.Append(tempmsg);
            logmsg.Append(tempmsg);
            tempmsg = string.Empty;

               tempmsg="COMPORT:" + cp + "\r\n";
               fullmsg.Append(tempmsg);
               logmsg.Append(tempmsg);
               tempmsg = string.Empty;

               try
               {
               FF.popAADevListUsing(DESCR);
               }
               catch (Exception ex)
               {
               Console.Write(ex.Message);
               }

               string comName = "COM" + cp;

               bool ok = false;

            //find Index for this comport name
               if (FF.AaDevices != null)
               {
               if (FF.AaDevices.Count != 0)
               {
                   foreach (device d in FF.AaDevices)
                   {
                       if (d.ComPort == comName)
                       {
                           FF.DevIndex = d.DevIndex;
                           FF.ProdIDNum = d.AAProdIDNum;
                           ok = true;
                       }
                   }
               }
               }
               else  //device count is zero
               {
               tempmsg = "No AA devices connected";
               Console.WriteLine(tempmsg);
               pause = 30;
               while (pause != 0)
               {
                   Thread.Sleep(100);
                   pause--;
               }
               return -1;
               }

            if(!ok)
             {
               tempmsg = "No AA device on " + comName;
               Console.WriteLine(tempmsg);
               pause = 30;
               while (pause != 0)
               {
                   Thread.Sleep(100);
                   pause--;
               }
               return -1;
               }

            //try to open port
            try
            {
                for (int i = 0; i < 5; i++)
                {
                    if (ok = FF.ConnectOnPort(FF.DevIndex))
                        break;
                    Thread.Sleep(100);
                    ic++;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine( ex.Message + "\r\n\r\n" + failedMsg);
                pause = 30;
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
                return -1;
            }

            if (!ok)
            {
                fullmsg.Append(failedMsg);
                logmsg.Append(failedMsg);
                Console.Write(fullmsg);
                pause = 30;
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
                return -1;
            }

            fullmsg.Append(tempmsg);
            logmsg.Append(tempmsg);
            tempmsg = string.Empty;

            //variable used for turning off messages
            bool suppressMsg = false;

            //get product ID to see if manual or motorized
            string ProdID = FF.ProdIDNum;

            switch (ProdID)
            {
                case FFID:
                    motorized = true ;
                   break;
                case FMID:
                case XLID:
                case FMLID:
                    motorized = false;
                    break;
                default:
                    tempmsg = "Device ID not recognized";
                    break;
            }

            //parse each command line character or string
            // This is where the library is used to control
            // Flip-Flat
            string S = string.Empty;
            string brt = string.Empty;
            bool rtn = false;
            int ibrt = 0;
            //parse each command string
            foreach (string cmd in args)
            {
                try
                {
                    //just look at the first letter of command (forced to upper case)
                    //skip if char is number
                    S = cmd.Substring(0, 1).ToUpper();
                    test = int.TryParse(S, out num);
                    if (test == false)  //S is not a numeric
                    {
                        switch (S)
                        {
                            case "O":
                                if (motorized)
                                {
                                    rtn = FF.OpenCover();     //open cover
                                    waitFor = "open";
                                }
                                else
                                    tempmsg="This product is not motorized\r\n";
                                break;
                            case "C":
                                if (motorized)
                                {
                                    rtn = FF.CloseCover();    //close cover
                                    waitFor = "close";
                                }
                                else
                                    tempmsg = "This product is not motorized\r\n";
                                break;
                            case "L":
                                waitFor = "lighton";
                                break;
                            case "D":
                                rtn = FF.TurnLightOff();  //turn light off
                                waitFor = "lightoff";
                                break;
                            case "B":
                                int len = 0;
                                //format of this command is "Bxxx",
                                //using the last chars (digits)
                                len = cmd.Length - 1;
                                brt = (cmd.Substring(1, len));
                                //convert to int and send to Flip-Flat
                                ibrt = Convert.ToInt16(brt);
                                //limit to 0-255
                                if (ibrt > 255) ibrt = 255;
                                if (ibrt < 0) ibrt = 0;
                                FF.Brightness = ibrt; //set brightness
                                tempmsg = "Set brightness to (0-255): " + ibrt.ToString() + "\r\n";

                                break;
                            case "G":
                                tempmsg = "Brightness is " + FF.Brightness + "\r\n";
                                break;
                            case "V":
                                tempmsg = "AACmd Version: " + Assy_version + "\r\n";
                                break;
                            case "F":
                                tempmsg = "Firmware Version: " + FF.FwVersion + "\r\n";
                                break;
                            case "S":
                                suppressMsg = true;
                                break;
                            case "P":
                                tempmsg = "ProductID: " + FF.ProdIDNum;
                                break;
                            case "A":
                                StringBuilder builder = new StringBuilder(FF.AaDevices.Count.ToString() + " Alnitak Device(s) Found:\n");
                                foreach (device d in FF.AaDevices)
                                {
                                    builder.Append(d.AAProdIDName);
                                    builder.Append(" on ");
                                    builder.Append(d.ComPort);
                                    builder.Append(",\n");
                                }
                                tempmsg = builder.ToString();
                                break;
                            default:
                                tempmsg = S + " Command not recognized\r\n";
                                break;
                        } //end switch
                    } // test
                } //end try
                catch (Exception ex)
                {
                    try
                    {
                        FF.Disconnect();
                        Console.WriteLine("WARNING\r\n" + ex.Message + "\r\n");
                        pause = 30;
                        while (pause != 0)
                        {
                            Thread.Sleep(100);
                            pause--;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    return -1;
                }
                //display and log messages
                //tersemsg.Append(tempmsg);  //commented out in v 1109
                fullmsg.Append(tempmsg);
                logmsg.Append(tempmsg);
                tempmsg = string.Empty;
            #if(DEBUG)
                //for debug, log the return value from
                // the library commands except com port and
                //brightness (which is a property and doesn't
                //return anyting
                test = int.TryParse(S, out num);
                if ( S != "B" && S!= "L" && !test )
                {
                    tempmsg = S + " returned " + rtn.ToString() + "\r\n";
                }
                logmsg.Append(tempmsg);
                tempmsg = string.Empty;
            #endif
            } //end foreach

            if (suppressMsg)
            {
                Console.Write(tersemsg);
            }
            else
                Console.Write(fullmsg);

            //wait here if cover is being moved
            //timeout if not open or closed in 14 seconds
             pause=0;
            switch (waitFor)
            {
                case "close":
                    tempmsg = "Closing Flip-Flat." + "\r\n";
                    if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;

                    //wait here until Flip_Flat cover is closed or timeout
                    pause = 30;
                    while (!FF.IsCoverClosed && pause != 0)
                    {
                        Thread.Sleep(delay);
                        pause--;
                    }
                    if (pause != 0)
                        tempmsg = "Flip-Flat closed." + "\r\n";
                    else tempmsg = "Timedout while closing." + "\r\n";
                    //if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;
                    break;
                case "open":
                    tempmsg = "Opening Flip-Flat." + "\r\n";
                    if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;

                    //wait here until Flip_Flat cover is open or timeout
                    pause = 30;
                    while (!FF.IsCoverOpen && pause != 0)
                    {
                        Thread.Sleep(delay);
                        pause--;
                    }
                    if (pause != 0)
                        tempmsg = "Flip-Flat open." + "\r\n";
                    else tempmsg = "Timedout while opening." + "\r\n";
                    //if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;
                    break;
                case "lighton":
                    tempmsg = "Turning on Light." + "\r\n";
                    if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;

                    //first check that cover is closed
                    // if motorized
                    if (motorized)
                    {
                        if (FF.IsCoverClosed)
                            tempmsg = "Flip-Flat closed." + "\r\n";
                        else //cover is not closed so try closing it
                        {
                            tempmsg = "Flip-Flat not closed." + "\r\n";
                            if (!suppressMsg)
                                Console.Write(tempmsg);
                            logmsg.Append(tempmsg);
                            tempmsg = string.Empty;
                            //close the cover
                            rtn = FF.CloseCover();
            #if (DEBUG)
                            logmsg.Append("Close returned " + rtn.ToString() + "\r\n");
            #endif
                            tempmsg = "Closing Flip-Flat." + "\r\n";
                            if (!suppressMsg)
                                Console.Write(tempmsg);
                            logmsg.Append(tempmsg);
                            tempmsg = string.Empty;
                            pause = 30;
                            //wait here until Flip_Flat cover is closed or timeout
                            while (!FF.IsCoverClosed && pause != 0)
                            {
                                Thread.Sleep(delay);
                                pause--;
                            }
                            if (pause != 0)
                                tempmsg = "Flip-Flat closed." + "\r\n";
                            else tempmsg = "Timedout while Closing." + "\r\n";
                        }
                    } //end if(motorized)

                    if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);

                    //get brightness
                    ibrt = FF.Brightness;

                    rtn = FF.TurnLightOn();
            #if (DEBUG)
                        logmsg.Append("Light returned " + rtn.ToString() + "\r\n");
            #endif
                    //wait 3 seconds for light to turn on.
                    pause = 6;
                    while (!FF.IsLightOn && pause != 0)
                    {
                        Thread.Sleep(delay);
                        pause--;
                    }
                    if (pause != 0)
                        tempmsg = "Light is on "
                            + "at brightness: " + ibrt.ToString() + "\r\n";
                    else tempmsg = "Timedout while waiting for light." + "\r\n";
                    //if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;
                    break;
                case "lightoff":
                    tempmsg = "Turning off light." + "\r\n";
                    if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;
                    //wait 3 seconds for light to turn off.
                    pause = 6;
                    while (FF.IsLightOn && pause != 0)
                    {
                        Thread.Sleep(delay);
                        pause--;
                    }
                    if (pause != 0)
                        tempmsg = "Light is off." + "\r\n";
                    else tempmsg = "Timedout while waiting for light." + "\r\n";
                    //if (!suppressMsg)
                        Console.Write(tempmsg);
                    logmsg.Append(tempmsg);
                    tempmsg = string.Empty;
                    break;
                default:
                    break;
            }

            //close com port, if open
            FF.Disconnect();

            if (!suppressMsg)
            {
                //create a log, write messages, close log
                StreamWriter log = StreamWriter.Null;
                try
                {
                    FileStream sb = new FileStream("FFLog.txt", FileMode.Append);
                    log = new StreamWriter(sb);
                    log.Write(logmsg);
                    log.Close();
                }
                catch (Exception ex)
                {
            #if (DEBUG)
                    Console.WriteLine(ex.Message);
            #endif
                }
            }
            //display messages for 3 seconds then exit if not suppressed
            if (!suppressMsg)
            {
                pause = 30;
                while (pause != 0)
                {
                    Thread.Sleep(100);
                    pause--;
                }
            }

            return 0;
        }
コード例 #2
0
ファイル: Test.cs プロジェクト: OptecInc/FlatFieldSoftware
        static int Main(string[] args)
        {
            //create new instance of Flip-Flat Library
            FlatFieldLibrary FF = new FlatFieldLibrary();
            //int pause = 30; //3 second delay before closing console
            bool ret = false;

            //quit if no command line commands
            if (args.Length == 0)
            {
                Console.WriteLine("WARNING No COM port specified\r\n"
                                 + "or incorrect format.");
                Console.ReadLine();
                return -1;
            }

            // get com port that Flip-Flat is connected to
            // (must be the first argument and must be numeric)
            string cp = string.Empty;
            int num = 0;
            //int ic = 0;
            bool test = int.TryParse(args[0], out num);
            if (test)
            {
                cp = args[0];
                try
                {
                    ret = FF.ConnectOnPort(cp);
                    //Console.WriteLine(ret.ToString());
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }
            else if (args[0].ToUpper() == "A")
            {
                StringBuilder builder = new StringBuilder();
                FF.popAADevListUsing(false);
                if (FF.AaDevices != null)
                {
                    builder.Append(FF.AaDevices.Count.ToString() + " Alnitak Device(s) Found:\n");
                    foreach (device d in FF.AaDevices)
                    {
                        builder.Append(d.AAProdIDName);
                        builder.Append(" on ");
                        builder.Append(d.ComPort);
                        builder.Append(",\n");
                    }
                }
                else
                    builder.Append("NO AA Devices Found");
                Console.WriteLine(builder.ToString());
                Console.ReadLine();
                return 0;
            }
            else
            {
                Console.WriteLine("No COM specified");
                Console.ReadLine();
                return -1;
            }

            if (!ret)
            {
                Console.WriteLine("Could not open COM" + cp);
                Console.ReadLine();
                return -1;
            }

            //parse each command line character or string
            // This is where the library is used to control
            // Flip-Flat
            string tempmsg = "COM" + cp + "\n\r";
            StringBuilder sb = new StringBuilder(tempmsg);
            string S = string.Empty;
            string brt = string.Empty;
            bool rtn = false;
            int ibrt = 0;
            //parse each command string
            foreach (string cmd in args)
            {
                try
                {
                    //just look at the first letter of command (forced to upper case)
                    //skip if char is number
                    S = cmd.Substring(0, 1).ToUpper();
                    test = int.TryParse(S, out num);
                    if (test == false)
                    {
                        switch (S)
                        {
                            case "O":
                                    rtn = FF.OpenCover();     //open cover
                                    sb.Append ("Opening");
                                break;
                            case "C":
                                rtn = FF.CloseCover();    //close cover
                                sb.Append ("Closing");
                                break;
                            case "L":
                                rtn = FF.TurnLightOn();
                                sb.Append ("Turning ON");
                                break;
                            case "D":
                                rtn = FF.TurnLightOff();  //turn light off
                                sb.Append ("Turning OFF");
                                break;
                            case "B":
                                int len = 0;
                                //format of this command is "Bxxx",
                                //using the last chars (digits)
                                len = cmd.Length - 1;
                                brt = (cmd.Substring(1, len));
                                //convert to int and send to Flip-Flat
                                ibrt = Convert.ToInt16(brt);
                                //limit to 0-255
                                if (ibrt > 255) ibrt = 255;
                                if (ibrt < 0) ibrt = 0;
                                FF.Brightness = ibrt; //set brightness
                                sb.Append ("Set Brightness to: " + FF.Brightness);
                                break;
                            case "G":
                                sb.Append ("Brightness is " + FF.Brightness + "\r\n");
                                break;
                            case "V":
                                //mpmsg = "AACmd Version: " + Assy_version + "\r\n";
                                break;
                            case "F":
                                sb.Append ("Firmware Version: " + FF.FwVersion + "\r\n");
                                break;
                            case "S":
                                //ppressMsg = true;
                                break;
                            case "P":
                                sb.Append ("ProductID: " + FF.ProdIDNum);
                                break;
                            case "A":
                                StringBuilder builder = new StringBuilder(FF.AaDevices.Count.ToString() + " Alnitak Device(s) Found:\n");
                                foreach (device d in FF.AaDevices)
                                {
                                    builder.Append(d.AAProdIDName);
                                    builder.Append(" on ");
                                    builder.Append(d.ComPort);
                                    builder.Append(",\n");
                                }
                                sb.Append (builder.ToString());
                                break;
                            default:
                                sb.Append (S + " Command not recognized\r\n");
                                break;
                        } //end switch
                    } // test
                } //end try
                catch (Exception ex)
                {
                    try
                    {
                        FF.Disconnect();
                        Console.WriteLine("WARNING\r\n" + ex.Message + "\r\n");
                        Console.ReadLine();
                    }
                    catch (Exception)
                    {
                    }
                    return -1;
                }
            } //end foreach
            sb.Append("\n\rHit Enter key");
            Console.WriteLine(sb.ToString());
            Console.ReadLine();
            return 0;
        }