static void TestSendFile(SerialPort port) { //Initiate a ZModem object var zmodem = new ZModem_Protocol.ZModem(port); if (zmodem.ZModemSend(@"data\version.txt", 1000)) { Console.WriteLine("Succeeded to send file."); } else { Console.WriteLine("Failed to send file."); } }
static void TestReceiveFile(SerialPort port) { //Initiate a ZModem object var zmodem = new ZModem_Protocol.ZModem(port); MemoryStream ms = null; zmodem.FileInfoEvent += (sender, args) => { Console.WriteLine("File Name: " + args.FileName + ", file size: " + args.Size); }; zmodem.BytesTransferedEvent += (sender, args) => { Console.Write(args.Size + " Bytes transfered" + "\r"); }; zmodem.TransfertStateEvent += (sender, args) => { Console.WriteLine("Current Status: " + args.State); }; //download file "toto" //Make sure you unsuscribe all DAtaReceived event from port before calling following line. //for (int i = 0; i < 10; i++) //zmodem.ZModemTransfert("Test.jpg", 2000); //backgroundWorker1.RunWorkerAsync(); ////download file ////Make sure you unsuscribe all DAtaReceived event from port before calling following line. ms = zmodem.ZModemTransfert("test.txt", 10); if (ms != null) { var basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"); if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } File.WriteAllBytes(Path.Combine(basePath, $"Verison-{DateTime.Now:yyyyMMdd-HHmmss-fff}.txt"), ms.ToArray()); } Console.WriteLine("Press a key to Exit..."); Console.ReadKey(); }
private void cmdInit_Click(object sender, EventArgs e) { if (sp != null) { sp.Dispose(); // Close previously opened serial Port? sp = null; } try { SerialPort_Fix.SerialPortFixer.Execute((string)cmbCOMPort.SelectedItem); Console.WriteLine("Bug fixer executed successfully!"); } catch (Exception ex) { Console.WriteLine("Impossible to open this serial port. Is it still existing? is it opened sowhere else?"); Console.WriteLine(ex.Message); tabgrp.Enabled = false; return; } try { sp = new SerialPort((string)cmbCOMPort.SelectedItem, (int)cmbBaud.SelectedItem, (Parity)cmbParity.SelectedItem, (int)cmbDataBit.SelectedItem, (StopBits)cmbStopBit.SelectedItem); sp.Open(); zmodem = new ZModem_Protocol.ZModem(sp); zmodem.TransfertStateEvent += zmodem_TransfertStateEvent; zmodem.FileInfoEvent += zmodem_FileInfoEvent; zmodem.BytesTransferedEvent += zmodem_BytesTransferedEvent; Console.WriteLine("Serial port setuped and opened successfully!"); } catch (Exception ex) { Console.WriteLine("Impossible to open this serial port with this settings!"); Console.WriteLine(ex.Message); tabgrp.Enabled = false; return; } tabgrp.Enabled = true; }