Пример #1
0
        static bool RunReadLoopCmd(string[] args)
        {
            byte deviceAddress, registerAddress;
            uint bytesToTransfer, numLoops;

            if (args.Length < 4 || args.Length > 5)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Invalid number of arguments arguments");
                Console.ResetColor();
                return(false);
            }

            if (!TryParseByte(args[1], out deviceAddress))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse device address");
                Console.ResetColor();
                return(false);
            }

            if (!uint.TryParse(args[2], out bytesToTransfer))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse number of bytes to read");
                Console.ResetColor();
                return(false);
            }

            if (!uint.TryParse(args[3], out numLoops))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleManager.WriteLine("Could not parse number of loops");
                Console.ResetColor();
                return(false);
            }

            if (args.Length == 4)
            {
                for (int i = 0; i < numLoops; i++)
                {
                    if (cmdCancelSource.Token.IsCancellationRequested)
                    {
                        return(false);
                    }

                    uint       bytesTransferred;
                    FtdiStatus status = channel.Read(deviceAddress, bytesToTransfer, buffer, out bytesTransferred, readOptions | TransferOptions.NoAddress);
                    if (status != FtdiStatus.Ok)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        ConsoleManager.WriteLine("FTDI Error: " + status);
                        Console.ResetColor();
                        return(false);
                    }

                    ConsoleManager.WriteLine("Loop " + i + ": " + BitConverter.ToString(buffer, 0, (int)bytesTransferred));
                }
            }
            else if (args.Length == 5)
            {
                if (!TryParseByte(args[4], out registerAddress))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    ConsoleManager.WriteLine("Could not parse register address");
                    Console.ResetColor();
                    return(false);
                }

                for (int i = 0; i < numLoops; i++)
                {
                    if (cmdCancelSource.Token.IsCancellationRequested)
                    {
                        return(false);
                    }

                    uint       bytesTransferred;
                    FtdiStatus status = channel.ReadFromRegister(deviceAddress, registerAddress, bytesToTransfer, buffer, out bytesTransferred, writeOptions, readOptions);
                    if (status != FtdiStatus.Ok)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        ConsoleManager.WriteLine("FTDI Error: " + status);
                        Console.ResetColor();
                        return(false);
                    }

                    //TODO format output
                    ConsoleManager.WriteLine("Loop " + i + ": " + BitConverter.ToString(buffer, 0, (int)bytesTransferred));
                }
            }

            return(true);
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += (s, e) =>
            {
                if (e.SpecialKey == ConsoleSpecialKey.ControlC)
                {
                    if (cmdCancelSource != null && !cmdCancelSource.IsCancellationRequested)
                    {
                        e.Cancel = true;
                        cmdCancelSource.Cancel();
                        Console.WriteLine("Cancelled");
                    }
                }
            };

            //TODO command line args to select channel and config
            FtdiStatus status;
            uint       channels = Channel.GetNumChannels();

            if (channels == 0)
            {
                ConsoleManager.WriteLine("No channels detected");
                return;
            }

            channel = new Channel(0);
            if (channel.IsDisposed)
            {
                ConsoleManager.WriteLine("Error initializing channel");
                return;
            }

            ChannelConfig con = new ChannelConfig(ClockRate.FastMode, 5, 0);

            status = channel.Initialize(ref con);
            if (status != FtdiStatus.Ok)
            {
                ConsoleManager.WriteLine("Error Initialize: " + status);
                return;
            }

            ConsoleManager.WriteLine("Initialized channel 0");

            //main loop
            running = true;
            while (running)
            {
                //write input string, read line
                ConsoleManager.Write("MPSSE >");
                string line = ConsoleManager.ReadLine();

                ConsoleManager.WriteCommand(line);

                //Handle user pressing enter without typing anything
                if (string.IsNullOrWhiteSpace(line))
                {
                    ConsoleManager.WriteLine("Type h or help to view a list of commands.");
                    continue;
                }

                //strip comments
                int commentIndex = line.IndexOf('#');
                if (commentIndex != -1)
                {
                    line = line.Substring(0, commentIndex);
                }

                //split into args
                string[] lineWords = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                //comment-only lines get skipped
                if (lineWords.Length == 0)
                {
                    continue;
                }

                //find the command
                Command cmd;
                if (!Commands.TryGetValue(lineWords[0], out cmd))
                {
                    ConsoleManager.WriteLine("Unknown command \"" + lineWords[0] + "\".");
                    continue;
                }

                //reset cancellation token
                cmdCancelSource = new CancellationTokenSource();

                //create task to run cmd async
                Task cmdRunTask = new Task(() => cmd.Run(lineWords), cmdCancelSource.Token);

                //run and wait for the task to complete
                cmdRunTask.Start();
                cmdRunTask.Wait(Timeout.Infinite);
            }

            //cleanly exit
            channel.Dispose();
        }