Etc_Function.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 static System.Net.Mime.MediaTypeNames;
  8. namespace DataRequest.Func
  9. {
  10. class Etc_Function
  11. {
  12. public bool OverLap_Window_Find(string str)
  13. {
  14. bool ret = false;
  15. foreach (Form frm in System.Windows.Forms.Application.OpenForms)
  16. {
  17. if (frm.Name == str)
  18. {
  19. // frm.Activate();
  20. ret = false;
  21. return ret;
  22. }
  23. }
  24. ret = true;
  25. return ret;
  26. }
  27. public string[] ProtocolSplit(string str)
  28. {
  29. string[] retstr = null;
  30. if (str.Contains(",") == true)
  31. {
  32. retstr = str.Split(',');
  33. }
  34. else if (str.Contains(" ") == true)
  35. {
  36. retstr = str.Split(' ');
  37. }
  38. return retstr;
  39. }
  40. public byte[] StrToConvertByteArray(string[] str)
  41. {
  42. byte[] retbyte = new byte[str.Length];
  43. //string dec = null;//Convert.ToInt32(hex, 10).ToString();
  44. for (int i = 0; i < str.Length; i++)
  45. {
  46. if(str[i].Contains("0x") == true)
  47. {
  48. str[i] = str[i].Substring(2);
  49. retbyte[i] = Convert.ToByte(str[i],16);
  50. }
  51. else
  52. {
  53. retbyte[i] = Convert.ToByte(str[i]);
  54. }
  55. // dec = Convert.ToInt32(str[i], 10).ToString();
  56. }
  57. return retbyte;
  58. }
  59. public string Str2hex(string strData, Boolean Compotable)
  60. {
  61. string resultHex = string.Empty;
  62. byte[] arr_byteStr = Encoding.Default.GetBytes(strData);
  63. foreach (byte byteStr in arr_byteStr)
  64. {
  65. if (Compotable == true)
  66. resultHex += string.Format("{0:X2}", byteStr) + " ";
  67. else
  68. resultHex += string.Format("{0:X2}", byteStr);
  69. }
  70. return resultHex;
  71. }
  72. public enum NessLab_Protocol_CMD_Index
  73. {
  74. NessLab_STATUS_REQ = 101,
  75. NessLab_STATUS_RES = 102,
  76. NessLab_Table_Req = 201,
  77. NessLab_Table_Res ,
  78. NessLab_TableSet_Req ,
  79. NessLab_TableSet_Res ,
  80. };
  81. public enum NessLab_Protocol_Index
  82. {
  83. NessLab_Header0,
  84. NessLab_Header1,
  85. NessLab_MsgID0,
  86. NessLab_MsgSN0,
  87. NessLab_MsgSN1,
  88. NessLab_Reserve0,
  89. NessLab_DataLength,
  90. NessLab_Data_ADC1_H,
  91. NessLab_Data_ADC1_L,
  92. NessLab_Data_ADC1_Table_Value,
  93. NessLab_DC_FAIL_ALARM,
  94. NessLab_DownLink_Status,
  95. NessLab_Over_Power_Alarm,
  96. NessLab_VSWR_ALARM,
  97. NessLab_Over_Input_Alarm,
  98. NessLab_Over_Temp_Alarm,
  99. NessLab_Temp_Monitor,
  100. NessLab_ALC_ALARM,
  101. NessLab_ChecksumVal,
  102. NessLab_Tail0,
  103. NessLab_Tail1,
  104. NessLab_MAX_INDEX,
  105. };
  106. public byte[] NessLab_Protocol_Create(double[] data, int mode)
  107. {
  108. int Length;
  109. if (data == null)
  110. Length = 10;
  111. else
  112. Length = (data.Length * 2) + 10;
  113. byte[] tmpdata = new byte[Length];
  114. int index = 0;
  115. crc Nesslab_crc = new crc();
  116. tmpdata[index++] = 0x7E;
  117. tmpdata[index++] = 0x7E;
  118. tmpdata[index++] = (byte)mode; //MSG ID
  119. tmpdata[index++] = 0; //MSG SN0
  120. tmpdata[index++] = 0; //MSG SN1
  121. tmpdata[index++] = 0xFF;//Reserve
  122. tmpdata[index++] = (byte)(Length - 10);
  123. if (data != null)
  124. {
  125. //tmpdata[7] = (byte)(((UInt16)((double)data[0] * 1000) & 0xFF00) >> 8);
  126. for (int i = 0; i < data.Length; i++)
  127. {
  128. tmpdata[(i * 2) + 7] = (byte)(((UInt16)((double)data[i] * 1000) & 0xFF00) >> 8);
  129. tmpdata[(i * 2) + 7 + 1] = (byte)(((UInt16)((double)data[i] * 1000) & 0x00FF));
  130. index = (i * 2) + 7 + 1;
  131. }
  132. index++;
  133. }
  134. tmpdata[index++] = Nesslab_crc.NessLab_CheckSum(tmpdata, tmpdata[6] + 5);
  135. tmpdata[index++] = 0x7E;
  136. tmpdata[index] = 0x7E;
  137. return tmpdata;
  138. }
  139. }
  140. }