Bluecell_BootProtocol.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace RF_TRIO_PLL_ZIG
  7. {
  8. public enum Bluepro_t
  9. {
  10. bluecell_header0 = 0,
  11. bluecell_header1,
  12. bluecell_header2,
  13. bluecell_header3,
  14. bluecell_type,
  15. bluecell_length_h,
  16. bluecell_length_l,
  17. bluecell_updatecnt_h,
  18. bluecell_updatecnt_l,
  19. bluecell_data,
  20. };
  21. enum Updateseq
  22. {
  23. Bluecell_Reset = 0,
  24. Bluecell_Firmupdate_start,
  25. Bluecell_Firmupdate_sending,
  26. Bluecell_Firmupdate_end,
  27. };
  28. class Bluecell_BootProtocol
  29. {
  30. Serial serial;
  31. /*bluecell Header*/
  32. public const byte Bluecell_Header0 = 0x42;//ASCII : B
  33. public const byte Bluecell_Header1 = 0x4C;//ASCII : L
  34. public const byte Bluecell_Header2 = 0x55;//ASCII : U
  35. public const byte Bluecell_Header3 = 0x45;//ASCII : E
  36. /*bluecell type*/
  37. public const int bluecell_Firmupdate_sendlength = 1024;
  38. public const byte bluecell_header = 4;
  39. public const byte bluecell_type = 1;
  40. public const byte bluecell_length = 2;
  41. public const byte bluecell_updatecnt = 2;
  42. public const byte bluecell_crc16 = 2;
  43. public const byte bluecell_nessarybyte = bluecell_header + bluecell_type + bluecell_length + bluecell_updatecnt + bluecell_crc16;
  44. Crc16 crc16 = new Crc16();
  45. public int Bluecell_Firmupdate_sendlength() {
  46. return bluecell_Firmupdate_sendlength;
  47. }
  48. private void BootHeaderput(ref byte[] fix_data)
  49. {
  50. fix_data[(int)Bluepro_t.bluecell_header0] = Bluecell_Header0;
  51. fix_data[(int)Bluepro_t.bluecell_header1] = Bluecell_Header1;
  52. fix_data[(int)Bluepro_t.bluecell_header2] = Bluecell_Header2;
  53. fix_data[(int)Bluepro_t.bluecell_header3] = Bluecell_Header3;
  54. }
  55. public byte[] Boot_Reset(object serial,byte[] Updatedata)
  56. {
  57. this.serial = (Serial)serial;
  58. byte[] fix_data = new byte[11];
  59. //Array.Clear(data, 0, data.Length);
  60. BootHeaderput(ref fix_data);
  61. fix_data[(int)Bluepro_t.bluecell_type] = (byte)Updateseq.Bluecell_Reset;
  62. fix_data[(int)Bluepro_t.bluecell_length_h] = 0;
  63. fix_data[(int)Bluepro_t.bluecell_length_l] = 5;
  64. fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = Convert.ToByte((Updatedata.Length & 0xFF00) >> 8);
  65. fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = Convert.ToByte((Updatedata.Length & 0x00FF));
  66. fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length - 4) & 0xFF00) >> 8);
  67. fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length - 4) & 0x00FF));
  68. this.serial.Serial_DataSend(fix_data, fix_data.Length);
  69. return fix_data;
  70. }
  71. public byte[] Boot_DataSending(byte[] update_data,UInt16 length, UInt16 updatacnt)
  72. {
  73. //Array.Clear(data, 0, data.Length);
  74. byte[] fix_data = new byte[bluecell_Firmupdate_sendlength];
  75. //BootHeaderput(ref fix_data);
  76. //length += 5;
  77. Array.Copy(update_data, (int)Bluepro_t.bluecell_data, fix_data, (int)Bluepro_t.bluecell_data, length);
  78. BootHeaderput(ref fix_data);
  79. fix_data[(int)Bluepro_t.bluecell_type] = (byte)Updateseq.Bluecell_Firmupdate_sending;
  80. fix_data[(int)Bluepro_t.bluecell_length_h] = Convert.ToByte((length & 0xFF00) >> 8);
  81. fix_data[(int)Bluepro_t.bluecell_length_l] = Convert.ToByte((length & 0x00FF));
  82. fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = Convert.ToByte((updatacnt & 0xFF00) >> 8);
  83. fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = Convert.ToByte((updatacnt & 0x00FF));
  84. //Array.Copy(update_data, 0, fix_data, (int)Bluepro_t.bluecell_data, length-3);
  85. fix_data[length + 9] = Convert.ToByte((crc16.CRC16_Generate(fix_data, length + 5) & 0xFF00) >> 8);
  86. fix_data[length + 10] = Convert.ToByte(crc16.CRC16_Generate(fix_data, length + 5) & 0x00FF);
  87. return fix_data;
  88. }
  89. public byte[] Boot_DataEnd(byte[] update_data, int length)
  90. {
  91. //Array.Clear(data, 0, data.Length);
  92. byte[] fix_data = new byte[11];
  93. Array.Copy(update_data, (int)Bluepro_t.bluecell_data, fix_data, (int)Bluepro_t.bluecell_data, length);
  94. BootHeaderput(ref fix_data);
  95. fix_data[(int)Bluepro_t.bluecell_type] = (byte)Updateseq.Bluecell_Firmupdate_end;
  96. fix_data[(int)Bluepro_t.bluecell_length_h] = Convert.ToByte((length & 0xFF00) >> 8);
  97. fix_data[(int)Bluepro_t.bluecell_length_l] = Convert.ToByte((length & 0x00FF));
  98. fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = 0;
  99. fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = 0;
  100. //Array.Copy(update_data, 0, fix_data, (int)Bluepro_t.bluecell_data, length-3);
  101. fix_data[length + 9] = Convert.ToByte((crc16.CRC16_Generate(fix_data, length + 5) & 0xFF00) >> 8);
  102. fix_data[length + 10] = Convert.ToByte(crc16.CRC16_Generate(fix_data, length + 5) & 0x00FF);
  103. return fix_data;
  104. }
  105. }
  106. }