Ejemplo n.º 1
0
        private static String GetAsString(contactState state)
        {
            switch (state)
            {
            case contactState.active:
                return("Active");

            case contactState.terminated:
                return("Terminated");

            default:
                return("Unknown");
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // say hello
            Console.WriteLine(info);
            Console.WriteLine(intro);
            Console.WriteLine(hint, "your serial port");
            Console.WriteLine(disclaimer);
            Console.WriteLine();

            // init serial port
            port = new SerialPort();
            port.WriteTimeout = 100;
            port.ReadTimeout  = 100;

            // Ask user and try to open port
            while (true)
            {
                try
                {
                    port.PortName = AskPortName();
                    port.Open();
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error opening serial port: ");
                    Console.WriteLine(e.ToString());
                }
            }

            // do the magic
            while (true)
            {
                // perform the actual continuity test
                try
                {
                    port.WriteLine("42 ");
                    statistics[(int)contactState.UGLY]++;
                    try
                    {
                        string readstring = port.ReadLine();
                        if (readstring == "42 ")
                        {
                            // entire message received - good contact
                            state = contactState.GOOD;
                            statistics[(int)contactState.GOOD]++;
                        }
                        else
                        {
                            // something else received - bad contact
                            state = contactState.BAD;
                            statistics[(int)contactState.BAD]++;
                        }
                    }
                    catch (TimeoutException)
                    {
                        // nothing received - no contact
                        state = contactState.NONE;
                        statistics[(int)contactState.NONE]++;
                    }
                }
                catch
                {
                    // an error occurred, e.g. USB com adapter disconnected
                    state = contactState.ERROR;
                }

                // print new state to console
                if (state != lastState || statistics[(int)contactState.UGLY] % 100 == 0)
                {
                    switch (state)
                    {
                    case contactState.GOOD:
                        Console.BackgroundColor = ConsoleColor.White;
                        Console.ForegroundColor = ConsoleColor.Black;
                        ConsoleClear();
                        Console.WriteLine("GOOD CONTACT :)");
                        break;

                    case contactState.BAD:
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.ForegroundColor = ConsoleColor.Black;
                        ConsoleClear();
                        Console.WriteLine("BAD CONTACT :/");
                        break;

                    case contactState.NONE:
                        Console.BackgroundColor = ConsoleColor.Black;
                        Console.ForegroundColor = ConsoleColor.White;
                        ConsoleClear();
                        Console.WriteLine("NO CONTACT :(");
                        break;

                    default:
                        Console.BackgroundColor = ConsoleColor.DarkYellow;
                        Console.ForegroundColor = ConsoleColor.Black;
                        ConsoleClear();
                        Console.WriteLine("!!! COM PORT ERROR !!!");
                        return;
                    }
                    lastState = state;
                }
            }
        }