BaseUtil.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using DevExpress.XtraEditors;
  2. using System.Data;
  3. using UAS_DeviceMonitor.DataOperate;
  4. namespace UAS_DeviceMonitor.PublicMethod
  5. {
  6. class BaseUtil
  7. {
  8. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  9. {
  10. combo.Properties.Items.Clear();
  11. for (int i = 0; i < dt.Rows.Count; i++)
  12. {
  13. ComboBoxData item = new ComboBoxData();
  14. item.Value = dt.Rows[i][ValueField].ToString();
  15. item.Text = dt.Rows[i][TextField].ToString();
  16. combo.Properties.Items.Add(item);
  17. }
  18. combo.SelectedIndex = 0;
  19. }
  20. public static DataTable ToDataTable(DataRow[] rows)
  21. {
  22. if (rows == null || rows.Length == 0) return new DataTable();
  23. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  24. foreach (DataRow row in rows)
  25. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  26. return tmp;
  27. }
  28. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  29. {
  30. if (ComBox.SelectedItem == null)
  31. return ComBox.Text;
  32. else
  33. return (ComBox.SelectedItem as ComboBoxData).Value;
  34. }
  35. /// <summary>
  36. /// 获取LRC
  37. /// </summary>
  38. /// <param name="SQL"></param>
  39. public static string getLRC(string SENDMESSAGE)
  40. {
  41. string message = SENDMESSAGE.Trim();
  42. if (message.Length % 2 != 0)
  43. {
  44. message = message + "0";
  45. }
  46. int LRC = 0x0;
  47. for (int i = 0; i < message.Length; i = i + 2)
  48. {
  49. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  50. LRC += inside;
  51. }
  52. string _LRC = string.Format("{0:X2}", LRC);
  53. string LRCre = "";
  54. for (int i = 0; i < _LRC.Length; i++)
  55. {
  56. int index;
  57. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  58. LRCre += string.Format("{0:X}", index);
  59. }
  60. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  61. return LRCre;
  62. }
  63. }
  64. }