BaseUtil.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_PLCDataReader.DataOperate;
  9. using DevExpress.Utils;
  10. using UAS_PLCDataReader.CustomerControl.ValueLabel;
  11. using System.Collections.Generic;
  12. namespace UAS_PLCDataReader.PublicMethod
  13. {
  14. class BaseUtil
  15. {
  16. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  17. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  18. public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
  19. {
  20. combo.Items.Clear();
  21. for (int i = 0; i < dt.Rows.Count; i++)
  22. {
  23. ComboBoxData item = new ComboBoxData();
  24. item.Value = dt.Rows[i][ValueField].ToString();
  25. item.Text = dt.Rows[i][TextField].ToString();
  26. combo.Items.Add(item);
  27. }
  28. }
  29. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  30. {
  31. combo.Properties.Items.Clear();
  32. for (int i = 0; i < dt.Rows.Count; i++)
  33. {
  34. ComboBoxData item = new ComboBoxData();
  35. item.Value = dt.Rows[i][ValueField].ToString();
  36. item.Text = dt.Rows[i][TextField].ToString();
  37. combo.Properties.Items.Add(item);
  38. }
  39. combo.SelectedIndex = 0;
  40. }
  41. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt, bool AddAll)
  42. {
  43. combo.Properties.Items.Clear();
  44. if (AddAll)
  45. {
  46. ComboBoxData item = new ComboBoxData();
  47. item.Value = "全部";
  48. item.Text = "全部";
  49. combo.Properties.Items.Add(item);
  50. }
  51. for (int i = 0; i < dt.Rows.Count; i++)
  52. {
  53. ComboBoxData item = new ComboBoxData();
  54. item.Value = dt.Rows[i][ValueField].ToString();
  55. item.Text = dt.Rows[i][TextField].ToString();
  56. combo.Properties.Items.Add(item);
  57. }
  58. combo.SelectedIndex = 0;
  59. }
  60. public static DataTable ToDataTable(DataRow[] rows)
  61. {
  62. if (rows == null || rows.Length == 0) return new DataTable();
  63. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  64. foreach (DataRow row in rows)
  65. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  66. return tmp;
  67. }
  68. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  69. {
  70. if (ComBox.SelectedItem == null)
  71. return ComBox.Text;
  72. else
  73. return (ComBox.SelectedItem as ComboBoxData).Value;
  74. }
  75. /// <summary>
  76. /// 获取LRC
  77. /// </summary>
  78. /// <param name="SQL"></param>
  79. public static string getLRC(string SENDMESSAGE)
  80. {
  81. string message = SENDMESSAGE.Trim();
  82. if (message.Length % 2 != 0)
  83. {
  84. message = message + "0";
  85. }
  86. int LRC = 0x0;
  87. for (int i = 0; i < message.Length; i = i + 2)
  88. {
  89. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  90. LRC += inside;
  91. }
  92. string _LRC = string.Format("{0:X2}", LRC);
  93. string LRCre = "";
  94. for (int i = 0; i < _LRC.Length; i++)
  95. {
  96. int index;
  97. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  98. LRCre += string.Format("{0:X}", index);
  99. }
  100. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  101. return LRCre;
  102. }
  103. public static string ByteToHexadecimalString(byte[] b, int length)
  104. {
  105. string returnStr = "";
  106. if (b != null)
  107. {
  108. for (int i = 0; i < length; i++)
  109. {
  110. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  111. }
  112. }
  113. return returnStr.ToUpper();
  114. }
  115. public static void CleanMemory()
  116. {
  117. GC.Collect();
  118. GC.WaitForPendingFinalizers();
  119. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  120. {
  121. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  122. }
  123. }
  124. private static ToolTipControllerShowEventArgs args;
  125. public static ToolTipController MyToolTipClt { get; private set; }
  126. public static void NewToolTip(Control ctl, string title, string content, int showTime, ToolTipType toolTipType, ToolTipLocation tipLocation, bool isAutoHide, ToolTipIconType tipIconType, ImageList imgList, int imgIndex)
  127. {
  128. try
  129. {
  130. MyToolTipClt = new ToolTipController();
  131. args = MyToolTipClt.CreateShowArgs();
  132. content = (string.IsNullOrEmpty(content) ? "???" : content);
  133. title = string.IsNullOrEmpty(title) ? "温馨提示" : title;
  134. MyToolTipClt.ImageList = imgList;
  135. MyToolTipClt.ImageIndex = (imgList == null ? 0 : imgIndex);
  136. args.AutoHide = isAutoHide;
  137. MyToolTipClt.ShowBeak = true;
  138. MyToolTipClt.ShowShadow = true;
  139. MyToolTipClt.Rounded = true;
  140. MyToolTipClt.AutoPopDelay = (showTime == 0 ? 2000 : showTime);
  141. MyToolTipClt.SetToolTip(ctl, content);
  142. MyToolTipClt.SetTitle(ctl, title);
  143. MyToolTipClt.SetToolTipIconType(ctl, tipIconType);
  144. MyToolTipClt.Active = true;
  145. MyToolTipClt.HideHint();
  146. MyToolTipClt.ShowHint(content, title, ctl, tipLocation);
  147. }
  148. catch (Exception)
  149. {
  150. }
  151. }
  152. /// <summary>
  153. /// 检测键值对是否发生值的变化
  154. /// </summary>
  155. public static bool CheckDicDiff(Dictionary<string, string> A, Dictionary<string, string> B)
  156. {
  157. foreach (var item in B)
  158. {
  159. if (A.ContainsKey(item.Key))
  160. {
  161. if (A[item.Key] != item.Value)
  162. {
  163. return true;
  164. }
  165. }
  166. }
  167. return false;
  168. }
  169. /// <summary>
  170. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  171. /// </summary>
  172. /// <param name="collection"></param>
  173. /// <param name="dt"></param>
  174. public static void SetFormValue(Control.ControlCollection collection, DataTable dt)
  175. {
  176. //DataTable存在数据才进行赋值操作
  177. if (dt.Rows.Count > 0)
  178. {
  179. for (int i = 0; i < collection.Count; i++)
  180. {
  181. //如果含有子控件则进行递归调用
  182. if (collection[i].Controls.Count > 0)
  183. SetFormValue(collection[i].Controls, dt);
  184. string controlName = collection[i].Name;
  185. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  186. //默认给TextBox和Label赋值
  187. if (collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is NumericUpDown)
  188. {
  189. for (int j = 0; j < dt.Columns.Count; j++)
  190. {
  191. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  192. {
  193. //字段含有Status内容的才进行转换
  194. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  195. {
  196. //对审批状态进行判断
  197. switch (dt.Rows[0][j].ToString().ToUpper())
  198. {
  199. case "ENTERING":
  200. collection[i].Text = "在录入";
  201. break;
  202. case "UNAPPROVED":
  203. collection[i].Text = "未批准";
  204. break;
  205. case "COMMITED":
  206. collection[i].Text = "已提交";
  207. break;
  208. case "APPROVE":
  209. collection[i].Text = "已批准";
  210. break;
  211. case "AUDITED":
  212. collection[i].Text = "已审核";
  213. break;
  214. case "STARTED":
  215. collection[i].Text = "已下放";
  216. break;
  217. case "UNCHECK":
  218. collection[i].Text = "待检验";
  219. break;
  220. case "CHECKING":
  221. collection[i].Text = "检验中";
  222. break;
  223. default:
  224. collection[i].Text = dt.Rows[0][j].ToString();
  225. break;
  226. }
  227. }
  228. else
  229. collection[i].Text = dt.Rows[0][j].ToString();
  230. }
  231. }
  232. }
  233. //对封装在GroupBox的和Panel的控件进行批量赋值
  234. if (collection[i] is GroupControl || collection[i] is Panel)
  235. {
  236. for (int j = 0; j < collection[i].Controls.Count; j++)
  237. {
  238. controlName = collection[i].Controls[j].Name;
  239. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is ValueLabel)
  240. {
  241. for (int k = 0; k < dt.Columns.Count; k++)
  242. {
  243. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  244. {
  245. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. //如果没有记录的话,将dt中含有的列的对应值设置为空
  254. else
  255. {
  256. for (int i = 0; i < collection.Count; i++)
  257. {
  258. if (collection[i] is TextBox || collection[i] is ValueLabel)
  259. {
  260. for (int j = 0; j < dt.Columns.Count; j++)
  261. {
  262. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  263. {
  264. collection[i].Text = "";
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. public static string ASCIIToString(string ASCII)
  272. {
  273. string ReturnStr = "";
  274. ASCII = ASCII.Replace(" ", "").Replace("3A", "").Replace("0D", "").Replace("0A", "");
  275. for (int i = 0; i < ASCII.Length; i = i + 2)
  276. {
  277. if (i + 2 < ASCII.Length)
  278. {
  279. switch (ASCII.Substring(i, 2))
  280. {
  281. case "30":
  282. ReturnStr += "0";
  283. break;
  284. case "31":
  285. ReturnStr += "1";
  286. break;
  287. case "32":
  288. ReturnStr += "2";
  289. break;
  290. case "33":
  291. ReturnStr += "3";
  292. break;
  293. case "34":
  294. ReturnStr += "4";
  295. break;
  296. case "35":
  297. ReturnStr += "5";
  298. break;
  299. case "36":
  300. ReturnStr += "6";
  301. break;
  302. case "37":
  303. ReturnStr += "7";
  304. break;
  305. case "38":
  306. ReturnStr += "8";
  307. break;
  308. case "39":
  309. ReturnStr += "9";
  310. break;
  311. case "41":
  312. ReturnStr += "A";
  313. break;
  314. case "42":
  315. ReturnStr += "B";
  316. break;
  317. case "43":
  318. ReturnStr += "C";
  319. break;
  320. case "44":
  321. ReturnStr += "D";
  322. break;
  323. case "45":
  324. ReturnStr += "E";
  325. break;
  326. case "46":
  327. ReturnStr += "F";
  328. break;
  329. default:
  330. break;
  331. }
  332. }
  333. }
  334. return ReturnStr.ToUpper();
  335. }
  336. public static int[] GetDecimalData(string HexStr, int DataSize)
  337. {
  338. List<int> ReturnData = new List<int>();
  339. try
  340. {
  341. //去除前面的指令字符和数据长度字符
  342. HexStr = HexStr.Replace(":", "");
  343. if (HexStr != "")
  344. {
  345. string RealData = HexStr.Substring(6);
  346. //每个地址位存的数据是DataSize个
  347. for (int i = 0; i < RealData.Length; i = i + DataSize)
  348. {
  349. if (i + DataSize < RealData.Length)
  350. {
  351. if (Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16) / 1 != 0)
  352. ReturnData.Add(Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16));
  353. else
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. catch (Exception)
  360. {
  361. }
  362. return ReturnData.ToArray();
  363. }
  364. }
  365. }