private void backgroundWorkerSend_DoWork(object sender, DoWorkEventArgs e)
        {
            var fs       = (PrimeFileSet)e.Argument;
            var res      = new SendResults(fs.Files.Length, fs.Destination);
            var nullFile = new PrimeUsbData(new byte[] { 0x00 }, null);

            foreach (var file in fs.Files)
            {
                try
                {
                    var b = new PrimeProgramFile(file, Settings.Default);

                    try
                    {
                        if (b.IsValid)
                        {
                            var primeFile = new PrimeUsbData(b.Name, b.Data,
                                                             fs.Destination == Destinations.Calculator ? _calculator.OutputChunkSize : 0, Parameters);

                            switch (fs.Destination)
                            {
                            case Destinations.Calculator:
                                _calculator.Send(nullFile);
                                _calculator.Send(primeFile);
                                _calculator.Send(nullFile);
                                res.Add(SendResult.Success);
                                break;

                            case Destinations.UserFolder:
                            case Destinations.Custom:
                                primeFile.Save(Path.Combine(fs.CustomDestination, primeFile.Name + ".hpprgm"));
                                res.Add(SendResult.Success);
                                break;
                            }
                        }
                        else
                        {
                            res.Add(SendResult.ErrorInvalidFile);
                        }
                    }
                    catch
                    {
                        res.Add(SendResult.ErrorSend);
                    }
                }
                catch
                {
                    res.Add(SendResult.ErrorReading);
                }

                backgroundWorkerSend.ReportProgress(0, res);
            }

            e.Result = res;
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            var options = new Options();
            var parser  = new Parser(with => with.HelpWriter = Console.Error);

            if (args.Length > 0)
            {
                var calculator = new PrimeCalculator();
                var p          = true;

                if (args.Length == 1 && File.Exists(args[0]))
                {
                    options.SendFile = args[0];
                    options.Timeout  = 5;
                }
                else
                {
                    try
                    {
                        p = parser.ParseArguments(args, options);
                    }
                    catch
                    {
                        p = false;
                        Console.WriteLine(options.GetUsage());
                    }
                }

                if (p)
                {
                    // Run
                    if (options.RemoteMode)
                    {
                        // Message server mode
                        WaitForDevice(calculator, options.Timeout);
                        if (calculator.IsConnected)
                        {
                            Console.WriteLine("... connected. Use the calculator messaging options");
                            Console.WriteLine("to interact with the PC.");
                            Console.WriteLine();
                            Console.WriteLine("Press Ctrl-C or send EXIT from the device to exit.");
                            Console.WriteLine();
                            var _continue = true;

                            // Header
                            var assembly = Assembly.GetExecutingAssembly();
                            calculator.Send(new PrimeUsbData(String.Format("{1} v{2}{0}{3}{0}", Environment.NewLine,
                                                                           assembly.GetName().Name, assembly.GetName().Version.ToString(3),
                                                                           ((AssemblyCopyrightAttribute)assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright), calculator.OutputChunkSize, null));

                            calculator.DataReceived += calculator_DataReceived;
                            calculator.StartReceiving();

                            // Modes

                            do
                            {
                                var d         = ReceiveData(null); // Blocking function to receive the data
                                var separator = new String('-', Console.WindowWidth);

                                if (d != null && d.Type == PrimeUsbDataType.Message)
                                {
                                    var cmd = d.ToString();
                                    Console.WriteLine("[{0}] Executing: '{1}'", DateTime.Now.ToShortTimeString(), cmd);

                                    if (cmd.ToLower() == "exit")
                                    {
                                        _continue = false;
                                    }
                                    else
                                    {
                                        // Evaluate cmd
                                        var response = ExecuteCommand(cmd);
                                        Console.WriteLine("{1}{2}{0}{1}{2}", response, separator, Environment.NewLine);

                                        // Echo the response to the HP
                                        calculator.Send(new PrimeUsbData(response, calculator.OutputChunkSize, null));
                                    }
                                }
                            } while (_continue);

                            calculator.StopReceiving();
                            calculator.DataReceived -= calculator_DataReceived;
                        }
                        else
                        {
                            Console.WriteLine("Error! Timeout.");
                        }
                    }
                    else if (options.SendFile == null && (options.ReceiveFile != null || options.OutputFolder != null))
                    {
                        WaitForDevice(calculator, options.Timeout);

                        if (calculator.IsConnected)
                        {
                            Console.WriteLine("... connected. Tap Send with after selecting an ");
                            Console.WriteLine("element in the device, or press Ctrl-C to cancel.");

                            var d = ReceiveData(calculator); // Blocking function to receive the data

                            if (d != null)
                            {
                                SaveFile(d, options.OutputFolder ?? options.ReceiveFile, options.OutputFolder != null);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error! Timeout.");
                        }
                    }
                    else if (options.SendFile != null)
                    {
                        if (File.Exists(options.SendFile))
                        {
                            // Process the destination
                            var destination = options.OutputFolder == null && options.ReceiveFile == null
                                ? Destinations.Calculator
                                : Destinations.Custom;

                            // Parse the file
                            var b = new PrimeProgramFile(options.SendFile, new PrimeParameters(new[] { new KeyValuePair <String, object>("IgnoreInternalName", options.IgnoreInternalName) }));

                            if (destination == Destinations.Calculator)
                            {
                                //f.Show();
                                WaitForDevice(calculator, options.Timeout);

                                if (calculator.IsConnected)
                                {
                                    var primeFile = new PrimeUsbData(b.Name, b.Data, calculator.OutputChunkSize, null);

                                    Console.WriteLine("... connected. Sending file");
                                    var nullFile = new PrimeUsbData(new byte[] { 0x00 }, null);
                                    calculator.Send(nullFile);
                                    calculator.Send(primeFile);
                                    calculator.Send(nullFile);
                                    Console.WriteLine("... done.");
                                }
                                else
                                {
                                    Console.WriteLine("Error! Problems connecting with the device.");
                                }
                            }
                            else
                            {
                                // Save
                                SaveFile(new PrimeUsbData(b.Name, b.Data, 0, null), options.OutputFolder ?? options.ReceiveFile, options.OutputFolder != null);
                            }
                        }
                    }
                }
            }
            else
            {
                // Show help
                Console.Write(options.GetUsage());
            }
            Console.WriteLine();

            if (args.Length > 0)
            {
                if (args.Any(i => i == "--wait"))
                {
                    Console.ReadKey();
                }
            }
        }