public static void Upload(string firmwarebin, ICommsSerial comPort) { var fs = new FileStream(firmwarebin, FileMode.Open); var len = (int)fs.Length; len = (len % 128) == 0 ? len / 128 : (len / 128) + 1; var startlen = len; int a = 1; while (len > 0) { if (LogEvent != null) { LogEvent("Uploading block " + a + "/" + startlen); } XModem.SendBlock(fs, comPort, a); // responce ACK var ack = comPort.ReadByte(); while (ack == 'C') { ack = comPort.ReadByte(); } if (ack == ACK) { len--; a++; if (ProgressEvent != null) { ProgressEvent(len / startlen); } } else if (ack == NAK) { CustomMessageBox.Show("Corrupted packet. Please power cycle and try again.\r\n", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); len = 0; } } // boot comPort.Write("b"); }
bool upload_xmodem(ICommsSerial comPort) { // try xmodem mode // xmodem - short cts to ground try { uploader_LogEvent("Trying XModem Mode"); //comPort.BaudRate = 57600; comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate; comPort.ReadTimeout = 1000; Thread.Sleep(2000); var tempd = comPort.ReadExisting(); Console.WriteLine(tempd); comPort.Write("U"); Thread.Sleep(1000); var resp1 = Serial_ReadLine(comPort); // echo var resp2 = Serial_ReadLine(comPort); // echo 2 var tempd2 = comPort.ReadExisting(); // posibly bootloader info / use to sync // identify comPort.Write("i"); // responce is rfd900.... var resp3 = Serial_ReadLine(comPort); //echo var resp4 = Serial_ReadLine(comPort); // newline var resp5 = Serial_ReadLine(comPort); // bootloader info uploader_LogEvent(resp5); if (resp5.Contains("RFD900")) { // start upload comPort.Write("u"); var resp6 = Serial_ReadLine(comPort); // echo var resp7 = Serial_ReadLine(comPort); // Ready if (resp7.Contains("Ready")) { comPort.ReadTimeout = 3500; // responce is C var isC = comPort.ReadByte(); var temp = comPort.ReadExisting(); if (isC == 'C') { if (getFirmwareLocal()) { XModem.LogEvent += uploader_LogEvent; XModem.ProgressEvent += uploader_ProgressEvent; // start file send XModem.Upload(firmwarefile, comPort); XModem.LogEvent -= uploader_LogEvent; XModem.ProgressEvent -= uploader_ProgressEvent; return(true); } return(false); } } } } catch (Exception ex2) { log.Error(ex2); } return(false); }