BaseUtil.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraEditors.Repository;
  3. using System;
  4. using System.Data;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using UAS_DeviceMonitor.DataOperate;
  9. namespace UAS_DeviceMonitor.PublicMethod
  10. {
  11. class BaseUtil
  12. {
  13. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  14. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  15. public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
  16. {
  17. combo.Items.Clear();
  18. for (int i = 0; i < dt.Rows.Count; i++)
  19. {
  20. ComboBoxData item = new ComboBoxData();
  21. item.Value = dt.Rows[i][ValueField].ToString();
  22. item.Text = dt.Rows[i][TextField].ToString();
  23. combo.Items.Add(item);
  24. }
  25. }
  26. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  27. {
  28. combo.Properties.Items.Clear();
  29. for (int i = 0; i < dt.Rows.Count; i++)
  30. {
  31. ComboBoxData item = new ComboBoxData();
  32. item.Value = dt.Rows[i][ValueField].ToString();
  33. item.Text = dt.Rows[i][TextField].ToString();
  34. combo.Properties.Items.Add(item);
  35. }
  36. combo.SelectedIndex = 0;
  37. }
  38. public static DataTable ToDataTable(DataRow[] rows)
  39. {
  40. if (rows == null || rows.Length == 0) return new DataTable();
  41. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  42. foreach (DataRow row in rows)
  43. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  44. return tmp;
  45. }
  46. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  47. {
  48. if (ComBox.SelectedItem == null)
  49. return ComBox.Text;
  50. else
  51. return (ComBox.SelectedItem as ComboBoxData).Value;
  52. }
  53. /// <summary>
  54. /// 获取LRC
  55. /// </summary>
  56. /// <param name="SQL"></param>
  57. public static string getLRC(string SENDMESSAGE)
  58. {
  59. string message = SENDMESSAGE.Trim();
  60. if (message.Length % 2 != 0)
  61. {
  62. message = message + "0";
  63. }
  64. int LRC = 0x0;
  65. for (int i = 0; i < message.Length; i = i + 2)
  66. {
  67. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  68. LRC += inside;
  69. }
  70. string _LRC = string.Format("{0:X2}", LRC);
  71. string LRCre = "";
  72. for (int i = 0; i < _LRC.Length; i++)
  73. {
  74. int index;
  75. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  76. LRCre += string.Format("{0:X}", index);
  77. }
  78. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  79. return LRCre;
  80. }
  81. public static string ByteToHexadecimalString(byte[] b, int length)
  82. {
  83. string returnStr = "";
  84. if (b != null)
  85. {
  86. for (int i = 0; i < length; i++)
  87. {
  88. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  89. }
  90. }
  91. return returnStr;
  92. }
  93. public static void CleanMemory()
  94. {
  95. GC.Collect();
  96. GC.WaitForPendingFinalizers();
  97. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  98. {
  99. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  100. }
  101. }
  102. }
  103. }