Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //byte[] data = { 0x07, 0x5B, 0xCD, 0x15 };
            ////byte[] data = { 0x01, 0x02 };
            //ShiftRegister register = new ShiftRegister(data, true, 0x1021);
            //ushort output = register.CalcCRC_16();
            //Console.WriteLine("Crc output: " + output);
            //byte[] decodeData = { 0x07, 0x5B, 0xCD, 0x15, (byte)(output >> 8), (byte)output };
            //register = new ShiftRegister(decodeData, false, 0x1021);
            //ushort decode = register.CalcCRC_16();
            //Console.WriteLine("Decoded: " + decode);

            int protocolMode = 0;

            string[] transmissionModes = { "Send Data", "Receive Data" };
            int      transmissionMode  = SmartConsoleInput.ListSelectIndex(" <  > Select transmission mode: ", transmissionModes,
                                                                           ">< Make a selection, then press enter ><");

            if (transmissionMode == 1)
            {
                string[] protocolModes = { "Xmodem", "CRC-Xmodem" };
                protocolMode = SmartConsoleInput.ListSelectIndex(" <  > Select protocol mode: ", protocolModes,
                                                                 ">< Make a selection, then press enter ><");
            }

            SerialPortHandler portHandler = SerialPortHandler.InitializeSerialPort();

            if (transmissionMode == 0)
            {
                XModem.TransmitData(portHandler, "inputData.txt");
            }
            else if (transmissionMode == 1)
            {
                XModem.ReceiveData(portHandler, "outputData.txt", protocolMode);
            }
            else
            {
                throw new Exception("Bad transmission mode selected.");
            }
        }
        public static SerialPortHandler InitializeSerialPort()
        {
            string   portName;
            int      baudRate;
            Parity   parity;
            int      dataBits;
            StopBits stopBits;

            Console.WriteLine(" ++++ |-------------------------------------------------------| ++++");
            Console.WriteLine(" /\\/\\ | Serial Port Initializer Invoked! Prepare for takeoff! | /\\/\\");
            Console.WriteLine(" \\/\\/ | Initializing serial port ...                          | \\/\\/");
            Console.WriteLine(" ++++ |-------------------------------------------------------| ++++");

            string[] portNames = SerialPort.GetPortNames();
            portName = SmartConsoleInput.ListSelect <string>(" <  > Available ports: ", portNames,
                                                             ">< Make a selection, then press enter ><");
            Console.WriteLine();

            if (portName != null)
            {
                int[] baudRatesAvailable = { 9600, 115200 };
                baudRate = SmartConsoleInput.ListSelect <int>(" <  > Available transmission rates: ", baudRatesAvailable,
                                                              ">< Make a selection, then press enter ><");
                Console.WriteLine();
                Console.WriteLine(" $$$ Hit 'c' to auto configure selected port for XModem communication. $$$");
                Console.WriteLine(" $$$ Press any key to continue...                                      $$$");
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);

                if (keyInfo.Key == ConsoleKey.C)
                {
                    Console.WriteLine(" $$$ Auto-config complete!                                             $$$");
                    return(new SerialPortHandler(portName, baudRate, Parity.None, 8, StopBits.One));
                }

                if (baudRate > 0)
                {
                    int paritySelection = SmartConsoleInput.ListSelectIndex(" <  > Available parity modes: ", Enum.GetNames(typeof(Parity)), ">< Make a selection, then press enter ><");
                    Console.WriteLine();
                    if (paritySelection >= 0)
                    {
                        parity = (Parity)paritySelection;

                        Console.WriteLine(" How many data bits to use? ");
                        Console.Write(" Input: ");
                        dataBits = int.Parse(Console.ReadLine());
                        if (dataBits >= 5 && dataBits <= 8)
                        {
                            int stopBitsSelection = SmartConsoleInput.ListSelectIndex(" <  > Available stop bits: ", Enum.GetNames(typeof(StopBits)), ">< Make a selection, then press enter ><");
                            Console.WriteLine();
                            if (stopBitsSelection >= 0)
                            {
                                stopBits = (StopBits)stopBitsSelection;

                                return(new SerialPortHandler(portName, baudRate, parity, dataBits, stopBits));
                            }
                            else
                            {
                                throw new IndexOutOfRangeException();
                            }
                        }
                        else
                        {
                            throw new IndexOutOfRangeException();
                        }
                    }
                    else
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
                else
                {
                    throw new IndexOutOfRangeException();
                }
            }
            else
            {
                throw new IndexOutOfRangeException();
            }
        }