| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using DevExpress.XtraEditors;
- using DevExpress.XtraEditors.Repository;
- using System;
- using System.Data;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- using UAS_DeviceMonitor.DataOperate;
- namespace UAS_DeviceMonitor.PublicMethod
- {
- class BaseUtil
- {
- [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
- public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
- public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
- {
- combo.Items.Clear();
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- ComboBoxData item = new ComboBoxData();
- item.Value = dt.Rows[i][ValueField].ToString();
- item.Text = dt.Rows[i][TextField].ToString();
- combo.Items.Add(item);
- }
- }
- public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
- {
- combo.Properties.Items.Clear();
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- ComboBoxData item = new ComboBoxData();
- item.Value = dt.Rows[i][ValueField].ToString();
- item.Text = dt.Rows[i][TextField].ToString();
- combo.Properties.Items.Add(item);
- }
- combo.SelectedIndex = 0;
- }
- public static DataTable ToDataTable(DataRow[] rows)
- {
- if (rows == null || rows.Length == 0) return new DataTable();
- DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
- foreach (DataRow row in rows)
- tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
- return tmp;
- }
- public static string GetComboxEditValue(ComboBoxEdit ComBox)
- {
- if (ComBox.SelectedItem == null)
- return ComBox.Text;
- else
- return (ComBox.SelectedItem as ComboBoxData).Value;
- }
- /// <summary>
- /// 获取LRC
- /// </summary>
- /// <param name="SQL"></param>
- public static string getLRC(string SENDMESSAGE)
- {
- string message = SENDMESSAGE.Trim();
- if (message.Length % 2 != 0)
- {
- message = message + "0";
- }
- int LRC = 0x0;
- for (int i = 0; i < message.Length; i = i + 2)
- {
- int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
- LRC += inside;
- }
- string _LRC = string.Format("{0:X2}", LRC);
- string LRCre = "";
- for (int i = 0; i < _LRC.Length; i++)
- {
- int index;
- index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
- LRCre += string.Format("{0:X}", index);
- }
- LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
- return LRCre;
- }
- public static string ByteToHexadecimalString(byte[] b, int length)
- {
- string returnStr = "";
- if (b != null)
- {
- for (int i = 0; i < length; i++)
- {
- returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
- }
- }
- return returnStr;
- }
- public static void CleanMemory()
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- {
- SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
- }
- }
- }
- }
|