BaseUtil.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. using System.Xml;
  13. using UAS_PLCDataReader.Entity;
  14. namespace UAS_PLCDataReader.PublicMethod
  15. {
  16. class BaseUtil
  17. {
  18. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  19. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  20. public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
  21. {
  22. combo.Items.Clear();
  23. for (int i = 0; i < dt.Rows.Count; i++)
  24. {
  25. ComboBoxData item = new ComboBoxData();
  26. item.Value = dt.Rows[i][ValueField].ToString();
  27. item.Text = dt.Rows[i][TextField].ToString();
  28. combo.Items.Add(item);
  29. }
  30. }
  31. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  32. {
  33. combo.Properties.Items.Clear();
  34. for (int i = 0; i < dt.Rows.Count; i++)
  35. {
  36. ComboBoxData item = new ComboBoxData();
  37. item.Value = dt.Rows[i][ValueField].ToString();
  38. item.Text = dt.Rows[i][TextField].ToString();
  39. combo.Properties.Items.Add(item);
  40. }
  41. combo.SelectedIndex = 0;
  42. }
  43. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt, bool AddAll)
  44. {
  45. combo.Properties.Items.Clear();
  46. if (AddAll)
  47. {
  48. ComboBoxData item = new ComboBoxData();
  49. item.Value = "全部";
  50. item.Text = "全部";
  51. combo.Properties.Items.Add(item);
  52. }
  53. for (int i = 0; i < dt.Rows.Count; i++)
  54. {
  55. ComboBoxData item = new ComboBoxData();
  56. item.Value = dt.Rows[i][ValueField].ToString();
  57. item.Text = dt.Rows[i][TextField].ToString();
  58. combo.Properties.Items.Add(item);
  59. }
  60. combo.SelectedIndex = 0;
  61. }
  62. public static DataTable ToDataTable(DataRow[] rows)
  63. {
  64. if (rows == null || rows.Length == 0) return new DataTable();
  65. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  66. foreach (DataRow row in rows)
  67. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  68. return tmp;
  69. }
  70. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  71. {
  72. if (ComBox.SelectedItem == null)
  73. return ComBox.Text;
  74. else
  75. return (ComBox.SelectedItem as ComboBoxData).Value;
  76. }
  77. /// <summary>
  78. /// 获取LRC
  79. /// </summary>
  80. /// <param name="SQL"></param>
  81. public static string getLRC(string SENDMESSAGE)
  82. {
  83. string message = SENDMESSAGE.Trim();
  84. if (message.Length % 2 != 0)
  85. {
  86. message = message + "0";
  87. }
  88. int LRC = 0x0;
  89. for (int i = 0; i < message.Length; i = i + 2)
  90. {
  91. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  92. LRC += inside;
  93. }
  94. string _LRC = string.Format("{0:X2}", LRC);
  95. string LRCre = "";
  96. for (int i = 0; i < _LRC.Length; i++)
  97. {
  98. int index;
  99. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  100. LRCre += string.Format("{0:X}", index);
  101. }
  102. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  103. return LRCre;
  104. }
  105. public static string ByteToHexadecimalString(byte[] b, int length)
  106. {
  107. string returnStr = "";
  108. if (b != null)
  109. {
  110. for (int i = 0; i < length; i++)
  111. {
  112. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  113. }
  114. }
  115. return returnStr.ToUpper();
  116. }
  117. public static void CleanMemory()
  118. {
  119. GC.Collect();
  120. GC.WaitForPendingFinalizers();
  121. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  122. {
  123. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  124. }
  125. }
  126. private static ToolTipControllerShowEventArgs args;
  127. public static ToolTipController MyToolTipClt { get; private set; }
  128. public static void NewToolTip(Control ctl, string title, string content, int showTime, ToolTipType toolTipType, ToolTipLocation tipLocation, bool isAutoHide, ToolTipIconType tipIconType, ImageList imgList, int imgIndex)
  129. {
  130. try
  131. {
  132. MyToolTipClt = new ToolTipController();
  133. args = MyToolTipClt.CreateShowArgs();
  134. content = (string.IsNullOrEmpty(content) ? "???" : content);
  135. title = string.IsNullOrEmpty(title) ? "温馨提示" : title;
  136. MyToolTipClt.ImageList = imgList;
  137. MyToolTipClt.ImageIndex = (imgList == null ? 0 : imgIndex);
  138. args.AutoHide = isAutoHide;
  139. MyToolTipClt.ShowBeak = true;
  140. MyToolTipClt.ShowShadow = true;
  141. MyToolTipClt.Rounded = true;
  142. MyToolTipClt.AutoPopDelay = (showTime == 0 ? 2000 : showTime);
  143. MyToolTipClt.SetToolTip(ctl, content);
  144. MyToolTipClt.SetTitle(ctl, title);
  145. MyToolTipClt.SetToolTipIconType(ctl, tipIconType);
  146. MyToolTipClt.Active = true;
  147. MyToolTipClt.HideHint();
  148. MyToolTipClt.ShowHint(content, title, ctl, tipLocation);
  149. }
  150. catch (Exception)
  151. {
  152. }
  153. }
  154. /// <summary>
  155. /// 检测键值对是否发生值的变化
  156. /// </summary>
  157. public static bool CheckDicDiff(Dictionary<string, string> A, Dictionary<string, string> B)
  158. {
  159. foreach (var item in B)
  160. {
  161. if (A.ContainsKey(item.Key))
  162. {
  163. if (A[item.Key] != item.Value)
  164. {
  165. return true;
  166. }
  167. }
  168. }
  169. return false;
  170. }
  171. /// <summary>
  172. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  173. /// </summary>
  174. /// <param name="collection"></param>
  175. /// <param name="dt"></param>
  176. public static void SetFormValue(Control.ControlCollection collection, DataTable dt)
  177. {
  178. //DataTable存在数据才进行赋值操作
  179. if (dt.Rows.Count > 0)
  180. {
  181. for (int i = 0; i < collection.Count; i++)
  182. {
  183. //如果含有子控件则进行递归调用
  184. if (collection[i].Controls.Count > 0)
  185. SetFormValue(collection[i].Controls, dt);
  186. string controlName = collection[i].Name;
  187. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  188. //默认给TextBox和Label赋值
  189. if (collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is NumericUpDown)
  190. {
  191. for (int j = 0; j < dt.Columns.Count; j++)
  192. {
  193. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  194. {
  195. //字段含有Status内容的才进行转换
  196. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  197. {
  198. //对审批状态进行判断
  199. switch (dt.Rows[0][j].ToString().ToUpper())
  200. {
  201. case "ENTERING":
  202. collection[i].Text = "在录入";
  203. break;
  204. case "UNAPPROVED":
  205. collection[i].Text = "未批准";
  206. break;
  207. case "COMMITED":
  208. collection[i].Text = "已提交";
  209. break;
  210. case "APPROVE":
  211. collection[i].Text = "已批准";
  212. break;
  213. case "AUDITED":
  214. collection[i].Text = "已审核";
  215. break;
  216. case "STARTED":
  217. collection[i].Text = "已下放";
  218. break;
  219. case "UNCHECK":
  220. collection[i].Text = "待检验";
  221. break;
  222. case "CHECKING":
  223. collection[i].Text = "检验中";
  224. break;
  225. default:
  226. collection[i].Text = dt.Rows[0][j].ToString();
  227. break;
  228. }
  229. }
  230. else
  231. collection[i].Text = dt.Rows[0][j].ToString();
  232. }
  233. }
  234. }
  235. //对封装在GroupBox的和Panel的控件进行批量赋值
  236. if (collection[i] is GroupControl || collection[i] is Panel)
  237. {
  238. for (int j = 0; j < collection[i].Controls.Count; j++)
  239. {
  240. controlName = collection[i].Controls[j].Name;
  241. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is ValueLabel)
  242. {
  243. for (int k = 0; k < dt.Columns.Count; k++)
  244. {
  245. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  246. {
  247. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. //如果没有记录的话,将dt中含有的列的对应值设置为空
  256. else
  257. {
  258. for (int i = 0; i < collection.Count; i++)
  259. {
  260. if (collection[i] is TextBox || collection[i] is ValueLabel)
  261. {
  262. for (int j = 0; j < dt.Columns.Count; j++)
  263. {
  264. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  265. {
  266. collection[i].Text = "";
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. public static string ASCIIToString(string ASCII)
  274. {
  275. string ReturnStr = "";
  276. ASCII = ASCII.Replace(" ", "").Replace("3A", "").Replace("0D", "").Replace("0A", "");
  277. for (int i = 0; i < ASCII.Length; i = i + 2)
  278. {
  279. if (i + 2 < ASCII.Length)
  280. {
  281. switch (ASCII.Substring(i, 2))
  282. {
  283. case "30":
  284. ReturnStr += "0";
  285. break;
  286. case "31":
  287. ReturnStr += "1";
  288. break;
  289. case "32":
  290. ReturnStr += "2";
  291. break;
  292. case "33":
  293. ReturnStr += "3";
  294. break;
  295. case "34":
  296. ReturnStr += "4";
  297. break;
  298. case "35":
  299. ReturnStr += "5";
  300. break;
  301. case "36":
  302. ReturnStr += "6";
  303. break;
  304. case "37":
  305. ReturnStr += "7";
  306. break;
  307. case "38":
  308. ReturnStr += "8";
  309. break;
  310. case "39":
  311. ReturnStr += "9";
  312. break;
  313. case "41":
  314. ReturnStr += "A";
  315. break;
  316. case "42":
  317. ReturnStr += "B";
  318. break;
  319. case "43":
  320. ReturnStr += "C";
  321. break;
  322. case "44":
  323. ReturnStr += "D";
  324. break;
  325. case "45":
  326. ReturnStr += "E";
  327. break;
  328. case "46":
  329. ReturnStr += "F";
  330. break;
  331. default:
  332. break;
  333. }
  334. }
  335. }
  336. return ReturnStr.ToUpper();
  337. }
  338. public static int[] GetDecimalData(string HexStr, int DataSize)
  339. {
  340. List<int> ReturnData = new List<int>();
  341. try
  342. {
  343. //去除前面的指令字符和数据长度字符
  344. HexStr = HexStr.Replace(":", "");
  345. if (HexStr != "")
  346. {
  347. string RealData = HexStr.Substring(6);
  348. //每个地址位存的数据是DataSize个
  349. for (int i = 0; i < RealData.Length; i = i + DataSize)
  350. {
  351. if (i + DataSize < RealData.Length)
  352. {
  353. if (Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16) / 1 != 0)
  354. ReturnData.Add(Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16));
  355. else
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. catch (Exception)
  362. {
  363. }
  364. return ReturnData.ToArray();
  365. }
  366. public static void SetCacheData(string ParamName, object Value)
  367. {
  368. try
  369. {
  370. //根据地址读取xml文件
  371. XmlDocument doc = new XmlDocument();
  372. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  373. //忽略文档里面的注释
  374. settings.IgnoreComments = true;
  375. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  376. doc.Load(reader);
  377. //先得到根节点
  378. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  379. //再由根节点去找制定的节点
  380. XmlNodeList nodeList = rootNode.ChildNodes;
  381. bool flag = false;
  382. foreach (XmlNode node in nodeList)
  383. {
  384. //找到了这个节点名字
  385. if (node.Name == ParamName)
  386. {
  387. //就直接赋值
  388. node.InnerText = Value.ToString();
  389. flag = true;
  390. }
  391. }
  392. //如果没有该节点,就创建节点保存结果
  393. if (!flag)
  394. {
  395. //创建节点
  396. XmlElement newNode = doc.CreateElement(ParamName);
  397. XmlAttribute attr = doc.CreateAttribute("Type");
  398. attr.InnerText = Value.GetType().ToString();
  399. newNode.InnerText = Value.ToString();
  400. newNode.SetAttributeNode(attr);
  401. //讲新建的节点挂到根节点上
  402. rootNode.AppendChild(newNode);
  403. }
  404. //关闭Reader
  405. reader.Close();
  406. doc.Save(SystemInf.CacheFilePath);
  407. }
  408. catch (Exception e)
  409. {
  410. LogManager.DoLog(e.Message);
  411. }
  412. }
  413. public static Object GetCacheData(string ParamName)
  414. {
  415. try
  416. {
  417. Object o = null;
  418. //根据地址读取xml文件
  419. XmlDocument doc = new XmlDocument();
  420. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  421. //忽略文档里面的注释
  422. settings.IgnoreComments = true;
  423. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  424. doc.Load(reader);
  425. //先得到根节点
  426. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  427. //再由根节点去找制定的节点
  428. XmlNodeList nodeList = rootNode.ChildNodes;
  429. foreach (XmlNode node in nodeList)
  430. {
  431. //找到了这个节点名字
  432. if (node.Name == ParamName)
  433. {
  434. //返回节点的内容
  435. switch (((XmlElement)node).GetAttribute("Type"))
  436. {
  437. case "System.String":
  438. o = node.InnerText;
  439. break;
  440. case "System.Int32":
  441. o = int.Parse(node.InnerText);
  442. break;
  443. case "System.Boolean":
  444. o = node.InnerText == "True" ? true : false;
  445. break;
  446. default:
  447. break;
  448. }
  449. break;
  450. }
  451. }
  452. //关闭reader
  453. reader.Close();
  454. if (o == null)
  455. return "";
  456. else
  457. return o;
  458. }
  459. catch (Exception e)
  460. {
  461. LogManager.DoLog(e.Message);
  462. return "";
  463. }
  464. }
  465. }
  466. }