/// <summary> /// Sends data to the calculator /// </summary> /// <param name="file">Data to send</param> public void Send(PrimeUsbData file) { if (IsNotReady()) return; foreach(var c in file.Chunks) _calculator.Write(c); }
/// <summary> /// Sends data to the calculator /// </summary> /// <param name="file">Data to send</param> public void Send(PrimeUsbData file) { if (IsNotReady()) { return; } foreach (var c in file.Chunks) { _calculator.Write(c); } }
private static void SaveFile(PrimeUsbData primeFile, String output, bool isFolder=true) { var f = isFolder ? Path.Combine(output, primeFile.Name + ".hpprgm") : output; Console.WriteLine(); Console.WriteLine("Saving the file to: " + f); primeFile.Save(f); }
/// <summary> /// Receives data, file or anything valid as PrimeUsbData /// </summary> /// <param name="calculator">Calculator instance, null to avoid controlling the device</param> /// <returns></returns> private static PrimeUsbData ReceiveData(PrimeCalculator calculator) { PrimeUsbData d = null; var keepReceiving = true; if (calculator!=null) { calculator.DataReceived += calculator_DataReceived; calculator.StartReceiving(); } do { if (ReceivedBytes.Count > 0) { // Check for valid structure if (d == null) { if (calculator != null) Console.WriteLine("Receiving data..."); d = new PrimeUsbData(ReceivedBytes.Peek(),null); } if (d.IsValid) { ReceivedBytes.Dequeue(); while (ReceivedBytes.Count > 0) { var tmp = ReceivedBytes.Dequeue(); d.Chunks.Add(tmp.SubArray(1, tmp.Length - 1)); } if (d.IsComplete) { if (calculator != null) { calculator.StopReceiving(); calculator.DataReceived -= calculator_DataReceived; Console.WriteLine("... done."); } keepReceiving = false; } } else { // Discard and try with next chunk ReceivedBytes.Dequeue(); d = null; } } Thread.Sleep(500); } while (keepReceiving); return d; }
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(); }
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; }
private void StartReceiving() { _receivedData = new Queue<byte[]>(); _receivingData = true; _receivedFile = null; _calculator.StartReceiving(); UpdateGui(); }
private void CheckForDataToSave() { if (!_receivingData || _receivedData.Count == 0) return; // Check for valid structure if (_receivedFile == null) _receivedFile = new PrimeUsbData(_receivedData.Peek(), Parameters); if (_receivedFile.IsValid) { _receivedData.Dequeue(); while (_receivedData.Count > 0) { var tmp = _receivedData.Dequeue(); _receivedFile.Chunks.Add(tmp.SubArray(1, tmp.Length - 1)); } if (_receivedFile.IsComplete) StopReceiving(); else ScheduleCheck(); } else { // Discard and try with next chunk _receivedData.Dequeue(); _receivedFile = null; } }
private void ResetProgram() { _calculator.StopReceiving(); _receivedFile = null; IsBusy = false; _receivedData.Clear(); UpdateGui(); }
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); } }