FileDownload.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.IO;
  8. using System.IO.Ports;
  9. namespace RF_TRIO_PLL_ZIG
  10. {
  11. enum DataSeq
  12. {
  13. UpdateResetOK = 0,
  14. UpdateStartOK,
  15. UpdateSendingOK,
  16. UpdateEndOK,
  17. }
  18. class FileDownload
  19. {
  20. Bluecell_BootProtocol Bluecell_BootProtocol = new Bluecell_BootProtocol();
  21. byte[] All_update_data;
  22. /***
  23. *Data File open
  24. */
  25. OpenFileDialog ofd;
  26. Serial serial;
  27. public bool Update_ready = false;
  28. public string ShowFileOpenDialog(ref object serial,object ofd)
  29. {
  30. this.ofd = (OpenFileDialog)ofd;
  31. this.serial = (Serial)serial;
  32. this.ofd.Title = "업데이터 파일 탐색기";//파일오픈창 생성 및 설정
  33. this.ofd.FileName = "*.bin";
  34. //ofd.Filter = "bin 파일 (*.bin) | *.bin; | 모든 파일 (*.*) | *.*";
  35. this.ofd.Filter = "bin 파일 (*.bin) | *.bin;";
  36. DialogResult dr = this.ofd.ShowDialog(); //파일 오픈창 로드
  37. if (dr == DialogResult.OK)//OK버튼 클릭시
  38. {
  39. string fileName = this.ofd.SafeFileName; //File명과 확장자를 가지고 온다.
  40. string fileFullName = this.ofd.FileName;//File경로와 File명을 모두 가지고 온다.
  41. string filePath = fileFullName.Replace(fileName, "");//File경로만 가지고 온다.
  42. this.serial.FileDownloadClass_Get(this);
  43. All_update_data = File.ReadAllBytes(this.ofd.FileName);
  44. Bluecell_BootProtocol.Boot_Reset(this.serial, All_update_data);
  45. //byte[] updatedata = Bluecell_BootProtocol.Boot_DataSending(All_update_data, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)0);
  46. //this.serial.Serial_DataSend(updatedata);
  47. Update_ready = true;
  48. return fileFullName;
  49. }
  50. else if (dr == DialogResult.Cancel)//취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
  51. {
  52. return "";
  53. }
  54. return "";
  55. }
  56. #if false
  57. public void Recv_dataCheck(byte[] data)
  58. {
  59. Boolean HeaderCheck = Serial_HeaderCheck(data);
  60. int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1];
  61. if (HeaderCheck == false)
  62. {
  63. return;
  64. }
  65. else
  66. {
  67. DataSeq seq = DataSeq.UpdateResetOK;
  68. switch (seq)
  69. {
  70. case DataSeq.UpdateSendingOK:
  71. UpdateFileSend(data, DataAckcnt);
  72. break;
  73. case DataSeq.UpdateEndOK:
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. }
  80. #endif
  81. int sourceindex = 0;
  82. public void UpdateFileSend(byte[] data,int cnt)
  83. {
  84. if (this.serial == null)
  85. return;
  86. /*Define*/
  87. int Quotient = 0, remainder = 0;
  88. byte[] updatedata = new byte[Bluecell_BootProtocol.bluecell_Firmupdate_sendlength];
  89. /*Filse size */
  90. Quotient = All_update_data.Length / (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
  91. remainder = All_update_data.Length % (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
  92. /*file copy*/
  93. // Header : 4byte + Type : 1byte + Length : 2byte + update_cnt : 2byte = 9byte /* CRC :2byte */ : Total : 11byte
  94. Array.Copy(All_update_data, sourceindex, updatedata, Bluecell_BootProtocol.bluecell_nessarybyte - 2, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11));
  95. /*file write*/
  96. if (remainder > 0 && cnt > Quotient)
  97. {
  98. this.serial.Serial_DataSend(data, remainder);
  99. sourceindex = 0;
  100. All_update_data = null;
  101. }
  102. else
  103. {
  104. data = Bluecell_BootProtocol.Boot_DataSending(updatedata, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)cnt);
  105. this.serial.Serial_DataSend(data,Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
  106. /*send index modify*/
  107. sourceindex = (cnt + 1) * (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
  108. }
  109. }
  110. public static string AsciiToHex(string asciiString)
  111. {
  112. StringBuilder builder = new StringBuilder();
  113. foreach (char c in asciiString)
  114. {
  115. builder.Append(Convert.ToInt32(c).ToString("X"));
  116. }
  117. return builder.ToString();
  118. }
  119. public Boolean Serial_HeaderCheck(byte[] data)
  120. {
  121. Boolean ret = false;
  122. if (data[(int)Bluepro_t.bluecell_header0] == Bluecell_BootProtocol.Bluecell_Header0
  123. && data[(int)Bluepro_t.bluecell_header1] == Bluecell_BootProtocol.Bluecell_Header1
  124. && data[(int)Bluepro_t.bluecell_header2] == Bluecell_BootProtocol.Bluecell_Header2
  125. && data[(int)Bluepro_t.bluecell_header3] == Bluecell_BootProtocol.Bluecell_Header3
  126. )/*모든 Header OK */
  127. {
  128. ret = true;
  129. }
  130. return ret;
  131. }
  132. }
  133. }