BaseUtil.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. //不对比产量和产出
  162. if (A.ContainsKey(item.Key) && item.Key != "INQTY" && item.Key != "OUTQTY")
  163. {
  164. if (A[item.Key] != item.Value)
  165. {
  166. return true;
  167. }
  168. }
  169. }
  170. return false;
  171. }
  172. /// <summary>
  173. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  174. /// </summary>
  175. /// <param name="collection"></param>
  176. /// <param name="dt"></param>
  177. public static void SetFormValue(Control.ControlCollection collection, DataTable dt)
  178. {
  179. //DataTable存在数据才进行赋值操作
  180. if (dt.Rows.Count > 0)
  181. {
  182. for (int i = 0; i < collection.Count; i++)
  183. {
  184. //如果含有子控件则进行递归调用
  185. if (collection[i].Controls.Count > 0)
  186. SetFormValue(collection[i].Controls, dt);
  187. string controlName = collection[i].Name;
  188. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  189. //默认给TextBox和Label赋值
  190. if (collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is NumericUpDown)
  191. {
  192. for (int j = 0; j < dt.Columns.Count; j++)
  193. {
  194. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  195. {
  196. //字段含有Status内容的才进行转换
  197. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  198. {
  199. //对审批状态进行判断
  200. switch (dt.Rows[0][j].ToString().ToUpper())
  201. {
  202. case "ENTERING":
  203. collection[i].Text = "在录入";
  204. break;
  205. case "UNAPPROVED":
  206. collection[i].Text = "未批准";
  207. break;
  208. case "COMMITED":
  209. collection[i].Text = "已提交";
  210. break;
  211. case "APPROVE":
  212. collection[i].Text = "已批准";
  213. break;
  214. case "AUDITED":
  215. collection[i].Text = "已审核";
  216. break;
  217. case "STARTED":
  218. collection[i].Text = "已下放";
  219. break;
  220. case "UNCHECK":
  221. collection[i].Text = "待检验";
  222. break;
  223. case "CHECKING":
  224. collection[i].Text = "检验中";
  225. break;
  226. default:
  227. collection[i].Text = dt.Rows[0][j].ToString();
  228. break;
  229. }
  230. }
  231. else
  232. collection[i].Text = dt.Rows[0][j].ToString();
  233. }
  234. }
  235. }
  236. //对封装在GroupBox的和Panel的控件进行批量赋值
  237. if (collection[i] is GroupControl || collection[i] is Panel)
  238. {
  239. for (int j = 0; j < collection[i].Controls.Count; j++)
  240. {
  241. controlName = collection[i].Controls[j].Name;
  242. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is ValueLabel)
  243. {
  244. for (int k = 0; k < dt.Columns.Count; k++)
  245. {
  246. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  247. {
  248. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. //如果没有记录的话,将dt中含有的列的对应值设置为空
  257. else
  258. {
  259. for (int i = 0; i < collection.Count; i++)
  260. {
  261. if (collection[i] is TextBox || collection[i] is ValueLabel)
  262. {
  263. for (int j = 0; j < dt.Columns.Count; j++)
  264. {
  265. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  266. {
  267. collection[i].Text = "";
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. public static string ASCIIToString(string ASCII)
  275. {
  276. string ReturnStr = "";
  277. ASCII = ASCII.Replace(" ", "").Replace("3A", "").Replace("0D", "").Replace("0A", "");
  278. for (int i = 0; i < ASCII.Length; i = i + 2)
  279. {
  280. if (i + 2 < ASCII.Length)
  281. {
  282. switch (ASCII.Substring(i, 2))
  283. {
  284. case "30":
  285. ReturnStr += "0";
  286. break;
  287. case "31":
  288. ReturnStr += "1";
  289. break;
  290. case "32":
  291. ReturnStr += "2";
  292. break;
  293. case "33":
  294. ReturnStr += "3";
  295. break;
  296. case "34":
  297. ReturnStr += "4";
  298. break;
  299. case "35":
  300. ReturnStr += "5";
  301. break;
  302. case "36":
  303. ReturnStr += "6";
  304. break;
  305. case "37":
  306. ReturnStr += "7";
  307. break;
  308. case "38":
  309. ReturnStr += "8";
  310. break;
  311. case "39":
  312. ReturnStr += "9";
  313. break;
  314. case "41":
  315. ReturnStr += "A";
  316. break;
  317. case "42":
  318. ReturnStr += "B";
  319. break;
  320. case "43":
  321. ReturnStr += "C";
  322. break;
  323. case "44":
  324. ReturnStr += "D";
  325. break;
  326. case "45":
  327. ReturnStr += "E";
  328. break;
  329. case "46":
  330. ReturnStr += "F";
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. }
  337. return ReturnStr.ToUpper();
  338. }
  339. public static int[] GetDecimalData(string HexStr, int DataSize)
  340. {
  341. List<int> ReturnData = new List<int>();
  342. try
  343. {
  344. //去除前面的指令字符和数据长度字符
  345. HexStr = HexStr.Replace(":", "");
  346. if (HexStr != "")
  347. {
  348. string RealData = HexStr.Substring(6);
  349. //每个地址位存的数据是DataSize个
  350. for (int i = 0; i < RealData.Length; i = i + DataSize)
  351. {
  352. if (i + DataSize < RealData.Length)
  353. {
  354. if (Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16) / 1 != 0)
  355. ReturnData.Add(Convert.ToInt32("0x" + (RealData.Substring(i, DataSize).Substring(0, 4)), 16));
  356. else
  357. break;
  358. }
  359. }
  360. }
  361. }
  362. catch (Exception)
  363. {
  364. }
  365. return ReturnData.ToArray();
  366. }
  367. public static void SetCacheData(string ParamName, object Value)
  368. {
  369. try
  370. {
  371. //根据地址读取xml文件
  372. XmlDocument doc = new XmlDocument();
  373. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  374. //忽略文档里面的注释
  375. settings.IgnoreComments = true;
  376. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  377. doc.Load(reader);
  378. //先得到根节点
  379. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  380. //再由根节点去找制定的节点
  381. XmlNodeList nodeList = rootNode.ChildNodes;
  382. bool flag = false;
  383. foreach (XmlNode node in nodeList)
  384. {
  385. //找到了这个节点名字
  386. if (node.Name == ParamName)
  387. {
  388. //就直接赋值
  389. node.InnerText = Value.ToString();
  390. flag = true;
  391. }
  392. }
  393. //如果没有该节点,就创建节点保存结果
  394. if (!flag)
  395. {
  396. //创建节点
  397. XmlElement newNode = doc.CreateElement(ParamName);
  398. XmlAttribute attr = doc.CreateAttribute("Type");
  399. attr.InnerText = Value.GetType().ToString();
  400. newNode.InnerText = Value.ToString();
  401. newNode.SetAttributeNode(attr);
  402. //讲新建的节点挂到根节点上
  403. rootNode.AppendChild(newNode);
  404. }
  405. //关闭Reader
  406. reader.Close();
  407. doc.Save(SystemInf.CacheFilePath);
  408. }
  409. catch (Exception e)
  410. {
  411. LogManager.DoLog(e.Message);
  412. }
  413. }
  414. public static Object GetCacheData(string ParamName)
  415. {
  416. try
  417. {
  418. Object o = null;
  419. //根据地址读取xml文件
  420. XmlDocument doc = new XmlDocument();
  421. XmlReaderSettings settings = new XmlReaderSettings { CheckCharacters = false };
  422. //忽略文档里面的注释
  423. settings.IgnoreComments = true;
  424. XmlReader reader = XmlReader.Create(SystemInf.CacheFilePath, settings);
  425. doc.Load(reader);
  426. //先得到根节点
  427. XmlNode rootNode = doc.SelectSingleNode("cacheInfo");
  428. //再由根节点去找制定的节点
  429. XmlNodeList nodeList = rootNode.ChildNodes;
  430. foreach (XmlNode node in nodeList)
  431. {
  432. //找到了这个节点名字
  433. if (node.Name == ParamName)
  434. {
  435. //返回节点的内容
  436. switch (((XmlElement)node).GetAttribute("Type"))
  437. {
  438. case "System.String":
  439. o = node.InnerText;
  440. break;
  441. case "System.Int32":
  442. o = int.Parse(node.InnerText);
  443. break;
  444. case "System.Boolean":
  445. o = node.InnerText == "True" ? true : false;
  446. break;
  447. case "System.Decimal":
  448. o = decimal.Parse(node.InnerText);
  449. break;
  450. default:
  451. break;
  452. }
  453. break;
  454. }
  455. }
  456. //关闭reader
  457. reader.Close();
  458. if (o == null)
  459. return "";
  460. else
  461. return o;
  462. }
  463. catch (Exception e)
  464. {
  465. LogManager.DoLog(e.Message);
  466. return "";
  467. }
  468. }
  469. /// <summary>
  470. ///
  471. /// </summary>
  472. /// <param name="Minutes"></param>
  473. /// <returns></returns>
  474. public static string MinutesToDayTime(int Minutes)
  475. {
  476. int Day = Minutes / 1440;
  477. int Hour = Minutes % 1440 / 60;
  478. int Mins = Minutes % 60;
  479. return Day + "天" + Hour + "小时" + Mins + "分钟";
  480. }
  481. }
  482. }