Form_PLCDB_ShowInfo.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using MainForm.ClassFile;
  2. using ModBusClientSimple.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace MainForm.FaForm
  15. {
  16. public partial class Form_PLCDB_ShowInfo : Form
  17. {
  18. #region ModbusTCP
  19. ModbusClientHelper plc1 = new ModbusClientHelper(GlobalContext.Machine1Address, GlobalContext.MachinePort, 499);
  20. #endregion ModbusTCP
  21. #region 变量
  22. /// <summary>
  23. /// 展示的工位名称
  24. /// </summary>
  25. private string _StationName;
  26. /// <summary>
  27. /// 记录信息方法的载体窗体
  28. /// </summary>
  29. private Form_PLCDB _Form_PLCDB;
  30. /// <summary>
  31. /// “工位可调试点位预览表.xlsx”文件的地址
  32. /// </summary>
  33. private string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\DBFile\\工位可调试点位预览表.xlsx";
  34. private DataTable dt;
  35. #endregion 变量
  36. #region 单例
  37. private static Form_PLCDB_ShowInfo _Form_PLCDB_ShowInfo;
  38. public static Form_PLCDB_ShowInfo Singleton(string stationName, Form_PLCDB form_PLCDB)
  39. {
  40. if (_Form_PLCDB_ShowInfo == null)
  41. {
  42. _Form_PLCDB_ShowInfo = new Form_PLCDB_ShowInfo();
  43. }
  44. _Form_PLCDB_ShowInfo._StationName = stationName;
  45. _Form_PLCDB_ShowInfo._Form_PLCDB = form_PLCDB;
  46. _Form_PLCDB_ShowInfo.OnLoadData(); // 加载点位数据
  47. return _Form_PLCDB_ShowInfo;
  48. }
  49. #endregion 单例
  50. #region 窗体事件
  51. public Form_PLCDB_ShowInfo()
  52. {
  53. InitializeComponent();
  54. }
  55. private void Form_PLCDB_ShowInfo_Load(object sender, EventArgs e)
  56. {
  57. try
  58. {
  59. this.Text = string.Concat(_StationName, " 工位DB详情");
  60. // dgv_S0
  61. dgv_DBInfo.ColumnHeadersDefaultCellStyle.BackColor = Color.WhiteSmoke;
  62. dgv_DBInfo.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
  63. dgv_DBInfo.RectColor = Color.Gainsboro;
  64. dgv_DBInfo.GridColor = Color.Gainsboro;
  65. dgv_DBInfo.BackgroundColor = Color.White;
  66. }
  67. catch (Exception ex)
  68. {
  69. string str = ex.StackTrace;
  70. MessageBox.Show("PLC交互工位DB详情页面初始化出错!异常信息:" + ex.Message);
  71. _Form_PLCDB.AddMessage(LogType.Error, "PLC交互工位DB详情页面初始化出错!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message.ToString());
  72. }
  73. }
  74. private void Form_PLCDB_ShowInfo_VisibleChanged(object sender, EventArgs e)
  75. {
  76. if (!this.IsDisposed && this.Visible)
  77. {
  78. SBtn_GetDBValue_LoopStart.Enabled = true;
  79. SBtn_GetDBValue_LoopStop.Enabled = false;
  80. }
  81. else if (!this.IsDisposed && !this.Visible)
  82. {
  83. if (cts != null)
  84. {
  85. cts.Cancel();
  86. }
  87. SBtn_GetDBValue_LoopStart.Enabled = true;
  88. SBtn_GetDBValue_LoopStop.Enabled = false;
  89. }
  90. }
  91. #endregion 窗体事件
  92. #region 方法
  93. /// <summary>
  94. /// dgv加载数据
  95. /// </summary>
  96. private void OnLoadData()
  97. {
  98. try
  99. {
  100. if (!File.Exists(filePath))
  101. {
  102. MessageBox.Show($"‘{filePath}’文件不存在!未读到调试点位数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  103. return;
  104. }
  105. dt = NPOIHelper.ReadExcel(filePath);
  106. if (dt != null && dt.Rows.Count > 0)
  107. {
  108. dt.Rows.RemoveAt(0);
  109. for (int i = 0; i < dt.Rows.Count; i++)
  110. {
  111. if (dt.Rows[i][1] == DBNull.Value || !dt.Rows[i][1].ToString().Trim().Equals(_StationName))
  112. {
  113. dt.Rows.RemoveAt(i);
  114. i--;
  115. }
  116. }
  117. dgv_DBInfo.DataSource = dt.DefaultView;
  118. }
  119. else
  120. {
  121. dgv_DBInfo.DataSource = new DataTable().DefaultView;
  122. MessageBox.Show($"‘{filePath}’文件未包含任何需要读取的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. string str = ex.StackTrace;
  128. MessageBox.Show("PLC交互工位DB详情页面表格加载数据出错!异常信息:" + ex.Message);
  129. _Form_PLCDB.AddMessage(LogType.Error, "PLC交互工位DB详情页面表格加载数据出错!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message.ToString());
  130. }
  131. }
  132. #endregion 方法
  133. // 刷新当前值
  134. private void SBtn_GetDBValue_One_Click(object sender, EventArgs e)
  135. {
  136. try
  137. {
  138. if (dt == null)
  139. {
  140. return;
  141. }
  142. if (!plc1.IsConnected)
  143. {
  144. plc1.Connect();
  145. }
  146. int rowNum = 0;
  147. foreach (DataRow row in dt.Rows)
  148. {
  149. if (row[5] != DBNull.Value && row[4] != DBNull.Value && !string.IsNullOrEmpty(row[5].ToString()) && !string.IsNullOrEmpty(row[4].ToString()))
  150. {
  151. string value = string.Empty;
  152. string plcType = row[4].ToString();
  153. int plcAddress = Convert.ToInt32(row[5].ToString());
  154. switch (plcType)
  155. {
  156. case "Bool":
  157. value = (plc1.ReadHoldingRegisters(plcAddress, 1).FirstOrDefault() == 1).ToString(); // 读取
  158. break;
  159. case "Int16":
  160. value = plc1.ReadHoldingRegisters<short>(plcAddress).ToString(); // 读取
  161. break;
  162. case "Int32":
  163. value = plc1.ReadHoldingRegisters<int>(plcAddress).ToString(); // 读取
  164. break;
  165. case "String<32>":
  166. value = plc1.ReadHoldingRegisters(plcAddress, 32, 32).ToString(); // 读取
  167. break;
  168. case "Float":
  169. value = plc1.ReadHoldingRegisters<float>(plcAddress).ToString(); // 读取
  170. break;
  171. case "Bype<3>": // 日期yyyyMMdd;yyyy、MM、dd共占用3个地址位
  172. string plcDate_Y = plc1.ReadHoldingRegisters(plcAddress, 1).FirstOrDefault().ToString("0000"); // 产品序列号的 当前日期_年
  173. string plcDate_M = plc1.ReadHoldingRegisters(plcAddress + 1, 1).FirstOrDefault().ToString("00"); // 产品序列号的 当前日期_月
  174. string plcDate_D = plc1.ReadHoldingRegisters(plcAddress + 2, 1).FirstOrDefault().ToString("00"); // 产品序列号的 当前日期_日
  175. value = string.Concat(plcDate_Y, plcDate_M, plcDate_D); // 产品序列号的 当前日期
  176. break;
  177. default:
  178. break;
  179. }
  180. dgv_DBInfo.Rows[rowNum].Cells[9].Value = value;
  181. }
  182. rowNum++;
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. string str = ex.StackTrace;
  188. MessageBox.Show("PLC交互工位DB详情页面刷新当前值出错!异常信息:" + ex.Message);
  189. _Form_PLCDB.AddMessage(LogType.Error, "PLC交互工位DB详情页面刷新当前值出错!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message.ToString());
  190. }
  191. }
  192. #region 循环刷新
  193. /// <summary>
  194. /// 循环线程结束标志
  195. /// </summary>
  196. private CancellationTokenSource cts;
  197. /// <summary>
  198. /// 循环刷新当前值
  199. /// </summary>
  200. private async void SBtn_GetDBValue_LoopStart_Click(object sender, EventArgs e)
  201. {
  202. cts = new CancellationTokenSource();
  203. SBtn_GetDBValue_LoopStart.Enabled = false;
  204. SBtn_GetDBValue_LoopStop.Enabled = true;
  205. await Task.Run(() => ThreadCode(cts.Token));
  206. }
  207. /// <summary>
  208. /// 停止循环刷新当前值
  209. /// </summary>
  210. private void SBtn_GetDBValue_LoopStop_Click(object sender, EventArgs e)
  211. {
  212. cts.Cancel();
  213. SBtn_GetDBValue_LoopStop.Enabled = false;
  214. SBtn_GetDBValue_LoopStart.Enabled = true;
  215. }
  216. /// <summary>
  217. ///
  218. /// </summary>
  219. /// <param name="token"></param>
  220. private void ThreadCode(CancellationToken token)
  221. {
  222. while (!token.IsCancellationRequested)
  223. {
  224. try
  225. {
  226. if (dt == null)
  227. {
  228. return;
  229. }
  230. if (!plc1.IsConnected)
  231. {
  232. plc1.Connect();
  233. }
  234. int rowNum = 0;
  235. foreach (DataRow row in dt.Rows)
  236. {
  237. if (row[5] != DBNull.Value && row[4] != DBNull.Value && !string.IsNullOrEmpty(row[5].ToString()) && !string.IsNullOrEmpty(row[4].ToString()))
  238. {
  239. string value = string.Empty;
  240. string plcType = row[4].ToString();
  241. int plcAddress = Convert.ToInt32(row[5].ToString());
  242. switch (plcType)
  243. {
  244. case "Bool":
  245. value = (plc1.ReadHoldingRegisters(plcAddress, 1).FirstOrDefault() == 1).ToString(); // 读取
  246. break;
  247. case "Int16":
  248. value = plc1.ReadHoldingRegisters<short>(plcAddress).ToString(); // 读取
  249. break;
  250. case "Int32":
  251. value = plc1.ReadHoldingRegisters<int>(plcAddress).ToString(); // 读取
  252. break;
  253. case "String<32>":
  254. value = plc1.ReadHoldingRegisters(plcAddress, 32, 32).ToString(); // 读取
  255. break;
  256. case "Float":
  257. value = plc1.ReadHoldingRegisters<float>(plcAddress).ToString(); // 读取
  258. break;
  259. case "Bype<3>": // 日期yyyyMMdd;yyyy、MM、dd共占用3个地址位
  260. string plcDate_Y = plc1.ReadHoldingRegisters(plcAddress, 1).FirstOrDefault().ToString("0000"); // 产品序列号的 当前日期_年
  261. string plcDate_M = plc1.ReadHoldingRegisters(plcAddress + 1, 1).FirstOrDefault().ToString("00"); // 产品序列号的 当前日期_月
  262. string plcDate_D = plc1.ReadHoldingRegisters(plcAddress + 2, 1).FirstOrDefault().ToString("00"); // 产品序列号的 当前日期_日
  263. value = string.Concat(plcDate_Y, plcDate_M, plcDate_D); // 产品序列号的 当前日期
  264. break;
  265. default:
  266. break;
  267. }
  268. dgv_DBInfo.Invoke(new Action(() =>
  269. {
  270. dgv_DBInfo.Rows[rowNum].Cells[9].Value = value;
  271. }));
  272. }
  273. rowNum++;
  274. }
  275. }
  276. catch (Exception ex)
  277. {
  278. string str = ex.StackTrace;
  279. MessageBox.Show("PLC交互工位DB详情页面循环刷新当前值出错!异常信息:" + ex.Message);
  280. cts.Cancel();
  281. this.Invoke(new Action(() =>
  282. {
  283. SBtn_GetDBValue_LoopStart.Enabled = true;
  284. SBtn_GetDBValue_LoopStop.Enabled = false;
  285. }));
  286. _Form_PLCDB.AddMessage(LogType.Error, "PLC交互工位DB详情页面循环刷新当前值出错!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message.ToString());
  287. }
  288. Thread.Sleep(1000); // 等待
  289. }
  290. }
  291. #endregion 循环刷新
  292. /// <summary>
  293. /// 写入
  294. /// </summary>
  295. private void 写入ToolStripMenuItem_Click(object sender, EventArgs e)
  296. {
  297. try
  298. {
  299. int selectedRowIndex = dgv_DBInfo.SelectedIndex;
  300. if (selectedRowIndex < 0 || selectedRowIndex > dgv_DBInfo.RowCount - 1)
  301. {
  302. MessageBox.Show("请先选择要写入的‘当前值’单元格", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  303. return;
  304. }
  305. if (!plc1.IsConnected)
  306. {
  307. plc1.Connect();
  308. }
  309. if (dgv_DBInfo.Rows[selectedRowIndex].Cells[5].Value != DBNull.Value && dgv_DBInfo.Rows[selectedRowIndex].Cells[4].Value != DBNull.Value && !string.IsNullOrEmpty(dgv_DBInfo.Rows[selectedRowIndex].Cells[5].ToString()) && !string.IsNullOrEmpty(dgv_DBInfo.Rows[selectedRowIndex].Cells[4].ToString()))
  310. {
  311. string plcVName = dgv_DBInfo.Rows[selectedRowIndex].Cells[3].Value?.ToString(); // DB点位的变量名称
  312. string plcType = dgv_DBInfo.Rows[selectedRowIndex].Cells[4].Value.ToString(); // DB点位的类型
  313. int plcAddress = Convert.ToInt32(dgv_DBInfo.Rows[selectedRowIndex].Cells[5].Value.ToString()); // DB点位的地址
  314. string plcVValue = dgv_DBInfo.Rows[selectedRowIndex].Cells[9].Value.ToString(); // DB点位的值
  315. switch (plcType)
  316. {
  317. case "Bool":
  318. if (plcVValue.ToLower() == "true")
  319. {
  320. plcVValue = "1";
  321. }
  322. else if (plcVValue.ToLower() == "false")
  323. {
  324. plcVValue = "0";
  325. }
  326. else
  327. {
  328. MessageBox.Show("写入失败!写入的值不是Bool类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  329. return;
  330. }
  331. plc1.WriteSingleRegister(plcAddress, Convert.ToInt32(plcVValue)); // 写入
  332. MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  333. break;
  334. case "Int16":
  335. int value = 0;
  336. try
  337. {
  338. value = Convert.ToInt32(plcVValue);
  339. }
  340. catch
  341. {
  342. MessageBox.Show("写入失败!写入的值不是Int16类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  343. return;
  344. }
  345. plc1.WriteSingleRegister(plcAddress, value); // 写入
  346. MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  347. break;
  348. case "Int32":
  349. int value1 = 0;
  350. try
  351. {
  352. value1 = Convert.ToInt32(plcVValue);
  353. }
  354. catch
  355. {
  356. MessageBox.Show("写入失败!写入的值不是Int32类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  357. return;
  358. }
  359. plc1.WriteMultipleRegisters<int>(plcAddress, value1); // 写入
  360. MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  361. break;
  362. case "Float":
  363. float value2 = 0;
  364. try
  365. {
  366. value2 = Convert.ToSingle(plcVValue);
  367. }
  368. catch
  369. {
  370. MessageBox.Show("写入失败!写入的值不是Float类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  371. return;
  372. }
  373. plc1.WriteMultipleRegisters<float>(plcAddress, value2); // 写入
  374. MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  375. break;
  376. case "String<32>":
  377. if (plcVValue == null || plcVValue.Length < 1)
  378. {
  379. MessageBox.Show("写入失败!写入值的长度必须 > 1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  380. return;
  381. }
  382. plc1.WriteMultipleRegisters<string>(plcAddress, plcVValue, 32); // 写入
  383. MessageBox.Show("写入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  384. break;
  385. //case "Bype<3>": // 日期yyyyMMdd;yyyy、MM、dd共占用3个地址位
  386. default:
  387. MessageBox.Show("不支持修改该类型的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
  388. return;
  389. break;
  390. }
  391. _Form_PLCDB.AddMessage(LogType.Info, $"用户【{_Form_PLCDB.currentRole}】将‘{plcVName}’【{plcAddress}】的DB值修改为‘{plcVValue}’!");
  392. }
  393. }
  394. catch (Exception ex)
  395. {
  396. string str = ex.StackTrace;
  397. MessageBox.Show("写入值失败!错误信息:" + ex.Message);
  398. _Form_PLCDB.AddMessage(LogType.Error, "PLC交互工位DB详情页面写入当前值出错!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message.ToString());
  399. }
  400. }
  401. }
  402. }