using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.IO.Ports; namespace RF_TRIO_PLL_ZIG { enum DataSeq { UpdateResetOK = 0, UpdateStartOK, UpdateSendingOK, UpdateEndOK, } class FileDownload { Bluecell_BootProtocol Bluecell_BootProtocol = new Bluecell_BootProtocol(); byte[] All_update_data; /*** *Data File open */ OpenFileDialog ofd; Serial serial; public bool Update_ready = false; public string ShowFileOpenDialog(ref object serial,object ofd) { this.ofd = (OpenFileDialog)ofd; this.serial = (Serial)serial; this.ofd.Title = "업데이터 파일 탐색기";//파일오픈창 생성 및 설정 this.ofd.FileName = "*.bin"; //ofd.Filter = "bin 파일 (*.bin) | *.bin; | 모든 파일 (*.*) | *.*"; this.ofd.Filter = "bin 파일 (*.bin) | *.bin;"; DialogResult dr = this.ofd.ShowDialog(); //파일 오픈창 로드 if (dr == DialogResult.OK)//OK버튼 클릭시 { string fileName = this.ofd.SafeFileName; //File명과 확장자를 가지고 온다. string fileFullName = this.ofd.FileName;//File경로와 File명을 모두 가지고 온다. string filePath = fileFullName.Replace(fileName, "");//File경로만 가지고 온다. this.serial.FileDownloadClass_Get(this); All_update_data = File.ReadAllBytes(this.ofd.FileName); Bluecell_BootProtocol.Boot_Reset(this.serial, All_update_data); //byte[] updatedata = Bluecell_BootProtocol.Boot_DataSending(All_update_data, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)0); //this.serial.Serial_DataSend(updatedata); Update_ready = true; return fileFullName; } else if (dr == DialogResult.Cancel)//취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우 { return ""; } return ""; } #if false public void Recv_dataCheck(byte[] data) { Boolean HeaderCheck = Serial_HeaderCheck(data); int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1]; if (HeaderCheck == false) { return; } else { DataSeq seq = DataSeq.UpdateResetOK; switch (seq) { case DataSeq.UpdateSendingOK: UpdateFileSend(data, DataAckcnt); break; case DataSeq.UpdateEndOK: break; default: break; } } } #endif int sourceindex = 0; public void UpdateFileSend(byte[] data,int cnt) { if (this.serial == null) return; /*Define*/ int Quotient = 0, remainder = 0; byte[] updatedata = new byte[Bluecell_BootProtocol.bluecell_Firmupdate_sendlength]; /*Filse size */ Quotient = All_update_data.Length / (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11); remainder = All_update_data.Length % (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11); /*file copy*/ // Header : 4byte + Type : 1byte + Length : 2byte + update_cnt : 2byte = 9byte /* CRC :2byte */ : Total : 11byte Array.Copy(All_update_data, sourceindex, updatedata, Bluecell_BootProtocol.bluecell_nessarybyte - 2, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11)); /*file write*/ if (remainder > 0 && cnt > Quotient) { this.serial.Serial_DataSend(data, remainder); sourceindex = 0; All_update_data = null; } else { data = Bluecell_BootProtocol.Boot_DataSending(updatedata, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)cnt); this.serial.Serial_DataSend(data,Bluecell_BootProtocol.bluecell_Firmupdate_sendlength); /*send index modify*/ sourceindex = (cnt + 1) * (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11); } } public static string AsciiToHex(string asciiString) { StringBuilder builder = new StringBuilder(); foreach (char c in asciiString) { builder.Append(Convert.ToInt32(c).ToString("X")); } return builder.ToString(); } public Boolean Serial_HeaderCheck(byte[] data) { Boolean ret = false; if (data[(int)Bluepro_t.bluecell_header0] == Bluecell_BootProtocol.Bluecell_Header0 && data[(int)Bluepro_t.bluecell_header1] == Bluecell_BootProtocol.Bluecell_Header1 && data[(int)Bluepro_t.bluecell_header2] == Bluecell_BootProtocol.Bluecell_Header2 && data[(int)Bluepro_t.bluecell_header3] == Bluecell_BootProtocol.Bluecell_Header3 )/*모든 Header OK */ { ret = true; } return ret; } } }