private void sendYmodemInitialPacket(byte STX, int packetNumber, int invertedPacketNumber, byte[] data, int dataSize, string path, FileStream fileStream, byte[] CRC, int crcSize) { //string fileName = System.IO.Path.GetFileName(path); string fileName = "wifimcufw";//change the name to avoid errors string fileSize = fileStream.Length.ToString(); if (radioButtonFW.Checked) fileName="firmware"; else if (radioButtonBtloader.Checked) fileName="bootloader"; else if (radioButtonWlanDrv.Checked) fileName="wlandrv"; /* add filename to data */ int i; for (i = 0; i < fileName.Length && (fileName.ToCharArray()[i] != 0); i++) { data[i] = (byte)fileName.ToCharArray()[i]; } data[i] = 0; /* add filesize to data */ int j; for (j = 0; j < fileSize.Length && (fileSize.ToCharArray()[j] != 0); j++) { data[(i + 1) + j] = (byte)fileSize.ToCharArray()[j]; } data[(i + 1) + j] = 0; /* fill the remaining data bytes with 0 */ for (int k = ((i + 1) + j) + 1; k < dataSize; k++) { data[k] = 0; } /* calculate CRC */ Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros); CRC = crc16Ccitt.ComputeChecksumBytes(data); /* send the packet */ sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); }
private void douUploadingFirmware(int dt) { Console.WriteLine("douUploadingFirmware:uploadingFWStage:" + uploadingFWStage.ToString("X")+" d:" + dt.ToString("X")+":"+Environment.TickCount.ToString()); if (uploadingFWStage==(int)FWStage.WaitForStart1&& dt == C) { uploadingFirmwareTimeTick = Environment.TickCount; packetNumber = 0; invertedPacketNumber = 255; sendYmodemInitialPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, path, fileStream, CRC, crcSize); uploadingFWStage = (int)FWStage.WaitForStart2; Console.WriteLine("sendYmodemInitialPacket"); } else if (uploadingFWStage==(int)FWStage.WaitForStart2&& dt == ACK) { uploadingFirmwareTimeTick = Environment.TickCount; uploadingFWStage = (int)FWStage.WaitForStart3; Console.WriteLine("WaitForStart2"); } else if ((uploadingFWStage == (int)FWStage.WaitForStart3 &&dt == C)|| uploadingFWStage == (int)FWStage.SendingData && dt == ACK) { uploadingFirmwareTimeTick = Environment.TickCount; if (uploadingFWStage == (int)FWStage.SendingData&& dataSize != fileReadCount) {//finished uploadingFWStage = (int)FWStage.Closing3; serialPort1.Write(new byte[] { EOT }, 0, 1); Console.WriteLine("EOT1"); return; } uploadingFWStage = (int)FWStage.SendingData; fileReadCount = fileStream.Read(data, 0, dataSize); if (fileReadCount == 0) { uploadingFWStage = (int)FWStage.Closing1; serialPort1.Write(new byte[] { EOT }, 0, 1); Console.WriteLine("EOT2"); return; } if (fileReadCount != dataSize) for (int i = fileReadCount; i < dataSize; i++) data[i] = 0; /* calculate packetNumber */ packetNumber++; if (packetNumber > 255) packetNumber -= 256; Console.WriteLine(packetNumber); /* calculate invertedPacketNumber */ invertedPacketNumber = 255 - packetNumber; /* calculate CRC */ Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros); CRC = crc16Ccitt.ComputeChecksumBytes(data); /* send the packet */ sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); if (txtSP.Lines[txtSP.Lines.Length - 1].Length>30) { txtSP.AppendText("\r\n"); } txtSP.AppendText("."); //string pg = string.Format("{0:G}%", fileStream.Position * 100 / fileStream.Length); toolStripProgressBar1.Value = (int)(fileStream.Position * 100 / fileStream.Length); toolStripStatusLabel2.Text = string.Format("{0:G}%", fileStream.Position * 100 / fileStream.Length); return; } else if (uploadingFWStage == (int)FWStage.Closing3 && dt == C) { uploadingFirmwareTimeTick = Environment.TickCount; uploadingFWStage = (int)FWStage.Closing4; packetNumber = 0; invertedPacketNumber = 255; data = new byte[dataSize]; CRC = new byte[crcSize]; sendYmodemClosingPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); Console.WriteLine("sendYmodemClosingPacket"); return; } else if (uploadingFWStage == (int)FWStage.Closing4 && (dt == ACK||dt==C)) { uploadingFirmwareTimeTick = Environment.TickCount; bUploadingFirmware = false; serialPort1.Write(new byte[] { 0x0a,0x0d }, 0, 2); Console.WriteLine("Upload Firmware Success"); toolStripStatusLabel1.Text = "Upload Successfully"; toolStripStatusLabel2.Text = "100%"; toolStripProgressBar1.Value = 100; this.btnSendFW.Text = "Update"; MessageBox.Show(this, "Upload Firmware Success!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (dt==CA) { uploadingFirmwareTimeTick = Environment.TickCount; if (this.btnSendFW.Text == "Abort") { this.btnSendFW.PerformClick(); } MessageBox.Show(this, "Upload Firmware Abort!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void sendYmodemClosingPacket(byte STX, int packetNumber, int invertedPacketNumber, byte[] data, int dataSize, byte[] CRC, int crcSize) { /* calculate CRC */ Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros); CRC = crc16Ccitt.ComputeChecksumBytes(data); /* send the packet */ sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize); }