示例#1
0
        private void exportAsUSBDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                // TODO: Move to external file
                var f = new SaveFileDialog {
                    Filter = "bin|*.bin"
                };

                if (f.ShowDialog() == DialogResult.OK)
                {
                    var n    = 0;
                    var name = Path.Combine(Path.GetDirectoryName(f.FileName),
                                            Path.GetFileNameWithoutExtension(f.FileName));
                    var ext  = Path.GetExtension(f.FileName);
                    var prog = new PrimeProgramFile(Utilities.CreateTemporalFileFromText(editor.Text));
                    var p    = new PrimeUsbData(prog.SafeName, prog.Data);
                    p.GenerateChunks(p.Data.ToList(), 64);
                    foreach (var c in p.Chunks)
                    {
                        File.WriteAllBytes(name + (n++ > 0 ? n + "" : String.Empty) + ext, c);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error exporting the data", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
示例#2
0
        private void convertFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialogProgram.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var b = new PrimeProgramFile(openFileDialogProgram.FileName, Settings.Default);

                    if (b.IsValid)
                    {
                        saveFileDialogProgram.FileName = b.Name;

                        // Select the oposite filetype
                        saveFileDialogProgram.FilterIndex = openFileDialogProgram.FileName.EndsWith(".hpprgm",
                                                                                                    StringComparison.OrdinalIgnoreCase)
                            ? 2
                            : 1;

                        if (saveFileDialogProgram.ShowDialog() == DialogResult.OK)
                        {
                            new PrimeUsbData(b.Name, b.Data, 0, Parameters).Save(saveFileDialogProgram.FileName);
                        }
                    }
                }
                catch
                {
                }
            }
        }
示例#3
0
        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;
        }
示例#4
0
        private void OpenFile(string fileName)
        {
            if (AskForSave())
            {
                try
                {
                    if (!File.Exists(fileName))
                    {
                        MessageBox.Show("Can't load '" + fileName + "'", "File does not exist", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                        Utilities.UpdateRecentFiles();
                        Close();
                    }
                    else
                    {
                        var tmp = new PrimeProgramFile(fileName, _parent.Parameters);
                        _currentFile = String.Empty;
                        _currentName = String.Empty;
                        editor.Text  = new PrimeUsbData(tmp.SafeName, tmp.Data).ToString();
                        _dirty       = false;

                        if (tmp.IsConversion)
                        {
                            if (Settings.Default.AddCommentOnConversion)
                            {
                                editor.InsertText(0, "// Converted by PrimeComm from " + fileName + Environment.NewLine);
                            }
                        }
                        else
                        {
                            _currentFile = fileName;
                            _currentName = String.Empty;
                        }

                        // Check file length
                        AdjustScrollbarWidth();
                        editor.UndoRedo.EmptyUndoBuffer();

                        // Recent files
                        Utilities.AppendToRecentFiles(fileName);
                    }
                }
                catch
                {
                    MessageBox.Show("Error loading '" + fileName + "'", "Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

                UpdateGui();
            }
        }
示例#5
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();
                }
            }
        }