BaseUtil.cs 3.4 KB

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