FileDownload.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Basic_Terminal
  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. /***
  22. *Data File open
  23. */
  24. OpenFileDialog ofd;
  25. Serial serial;
  26. public string ShowFileOpenDialog(object serial,object ofd)
  27. {
  28. this.ofd = (OpenFileDialog)ofd;
  29. this.serial = (Serial)serial;
  30. this.ofd.Title = "업데이터 파일 탐색기";//파일오픈창 생성 및 설정
  31. this.ofd.FileName = "*.bin";
  32. //ofd.Filter = "bin 파일 (*.bin) | *.bin; | 모든 파일 (*.*) | *.*";
  33. this.ofd.Filter = "bin 파일 (*.bin) | *.bin;";
  34. DialogResult dr = this.ofd.ShowDialog(); //파일 오픈창 로드
  35. if (dr == DialogResult.OK)//OK버튼 클릭시
  36. {
  37. string fileName = this.ofd.SafeFileName; //File명과 확장자를 가지고 온다.
  38. string fileFullName = this.ofd.FileName;//File경로와 File명을 모두 가지고 온다.
  39. string filePath = fileFullName.Replace(fileName, "");//File경로만 가지고 온다.
  40. Bluecell_BootProtocol.Boot_Reset(serial);
  41. //UpdateFileSend(File.ReadAllBytes(this.ofd.FileName));
  42. return fileFullName;
  43. }
  44. else if (dr == DialogResult.Cancel)//취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
  45. {
  46. return "";
  47. }
  48. return "";
  49. }
  50. #if false
  51. public void Recv_dataCheck(byte[] data)
  52. {
  53. Boolean HeaderCheck = Serial_HeaderCheck(data);
  54. int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1];
  55. if (HeaderCheck == false)
  56. {
  57. return;
  58. }
  59. else
  60. {
  61. DataSeq seq = DataSeq.UpdateResetOK;
  62. switch (seq)
  63. {
  64. case DataSeq.UpdateSendingOK:
  65. UpdateFileSend(data, DataAckcnt);
  66. break;
  67. case DataSeq.UpdateEndOK:
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. }
  74. #endif
  75. private void UpdateFileSend(byte[] data,int cnt)
  76. {
  77. /*Define*/
  78. int Quotient = 0,remainder = 0;
  79. int sourceindex = 0;
  80. byte[] updatedata = new byte[Bluecell_BootProtocol.bluecell_Firmupdate_sendlength];
  81. /*Filse size */
  82. Quotient = data.Length / Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
  83. remainder = data.Length % Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
  84. /*file copy*/
  85. Array.Copy(data, sourceindex, updatedata, 0,Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
  86. /*file write*/
  87. if (remainder > 0 && cnt > Quotient)
  88. {
  89. this.serial.Serial_DataSend(data, remainder);
  90. }
  91. else
  92. {
  93. this.serial.Serial_DataSend(data, cnt * Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
  94. /*send index modify*/
  95. sourceindex = cnt * Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
  96. }
  97. }
  98. public Boolean Serial_HeaderCheck(byte[] data)
  99. {
  100. Boolean ret = false;
  101. if (data[(int)Bluepro_t.bluecell_header0] == Bluecell_BootProtocol.Bluecell_Header0
  102. && data[(int)Bluepro_t.bluecell_header1] == Bluecell_BootProtocol.Bluecell_Header1
  103. && data[(int)Bluepro_t.bluecell_header2] == Bluecell_BootProtocol.Bluecell_Header2
  104. && data[(int)Bluepro_t.bluecell_header3] == Bluecell_BootProtocol.Bluecell_Header3
  105. )/*모든 Header OK */
  106. {
  107. ret = true;
  108. }
  109. return ret;
  110. }
  111. }
  112. }