DispatchSystem7352.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10. using System.Threading;
  11. using AGV_WPF_Global;
  12. using AGV_WPF.Tools;
  13. using System.Threading.Tasks;
  14. namespace AGV_WPF
  15. {
  16. public partial class DispatchSystem7352 : Form
  17. {
  18. DataTable dtStation = new DataTable();
  19. private delegate void InvokeDelegate();
  20. public DispatchSystem7352()
  21. {
  22. InitializeComponent();
  23. }
  24. private void DispatchSystem7352_Load(object sender, EventArgs e)
  25. {
  26. LoadConfig();
  27. }
  28. public void LoadConfig()
  29. {
  30. string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
  31. string dataPath = currPath + @"data";
  32. string dataFile = dataPath + @"\物料信息表.xls";
  33. try
  34. {
  35. string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
  36. string confStr2 = @"Persist Security Info=false;";
  37. string confStr3 = @"Data Source=" + dataFile + ";";
  38. string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
  39. string connStr = confStr1 + confStr2 + confStr3 + confStr4;
  40. OleDbConnection oleDC = new OleDbConnection(connStr);
  41. OleDbDataAdapter oleDDA1 = new OleDbDataAdapter("Select * From [自动工位表$]", oleDC);
  42. oleDDA1.Fill(dtStation);
  43. oleDC.Close();
  44. foreach (DataColumn dcol in dtStation.Columns)
  45. {
  46. if (dcol.ColumnName.Contains("DB"))// && !dcol.ColumnName.Contains("心跳"))
  47. {
  48. DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
  49. col.HeaderText = dcol.ColumnName;
  50. col.Name = dcol.ColumnName;
  51. col.ReadOnly = true;
  52. col.Resizable = DataGridViewTriState.True;
  53. col.SortMode = DataGridViewColumnSortMode.Automatic;
  54. dataGridView1.Columns.Add(col);
  55. }
  56. //else if (dcol.ColumnName.Contains("DB") && dcol.ColumnName.Contains("心跳"))
  57. //{
  58. // DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
  59. // col.HeaderText = dcol.ColumnName;
  60. // col.Name = dcol.ColumnName;
  61. // col.ReadOnly = true;
  62. // col.Resizable = DataGridViewTriState.True;
  63. // col.SortMode = DataGridViewColumnSortMode.Automatic;
  64. // dataGridView1.Columns.Add(col);
  65. //}
  66. }
  67. for (int i = 0; i < dtStation.Rows.Count; i++)
  68. {
  69. int index = dataGridView1.Rows.Add();
  70. dataGridView1.Rows[index].Cells[0].Value = dtStation.Rows[i][0].ToString();
  71. dataGridView1.Rows[index].Cells[1].Value = dtStation.Rows[i][1].ToString();
  72. var dr = dtStation.Rows[i];
  73. // MyTest.ManagedThreadPoolcs.ManagedThreadPool.QueueUserWorkItem(new WaitCallback(Test), dtStation.Rows[i]);
  74. Task.Factory.StartNew(() => Test(dr));
  75. Thread.Sleep(50);
  76. }
  77. }
  78. catch (Exception e)
  79. {
  80. MessageBox.Show(e.Message);
  81. }
  82. }
  83. public void Test(object Dr)
  84. {
  85. if (Dr != null)
  86. {
  87. DataRow dr = (DataRow)Dr;
  88. int index = dtStation.Rows.IndexOf(dr);
  89. HslCommunication.Profinet.Siemens.SiemensS7Net s7 = null;
  90. {
  91. s7 = new HslCommunication.Profinet.Siemens.SiemensS7Net(HslCommunication.Profinet.Siemens.SiemensPLCS.S1200,
  92. dr["PLC地址"].ToString());
  93. s7.Rack = (byte)0;
  94. s7.Slot = (byte)0;
  95. s7.ConnectTimeOut = 1200;
  96. s7.ReceiveTimeOut = 1200;
  97. s7.SetPersistentConnection();
  98. }
  99. while (true)
  100. {
  101. try
  102. {
  103. //心跳
  104. if (!string.IsNullOrEmpty(dr["中控心跳DB"].ToString()))
  105. {
  106. var HeartBit = s7.ReadBool(dr["中控心跳DB"].ToString());
  107. if (!HeartBit.IsSuccess)
  108. {
  109. Thread.Sleep(1000);
  110. continue;
  111. }
  112. {
  113. s7.Write(dr["中控心跳DB"].ToString(), !HeartBit.Content);
  114. }
  115. }
  116. var infos = GlobalPara.AgvInfoDic.Values.ToList().FindAll(o=>o.ConnectStatus==true&&o.AGVNUM>0).ToList();
  117. //进入
  118. {
  119. var info = infos.Find(o => o.MARKNUM.ToString() == dr["请求进入地标"].ToString() && o.RUNSTATUS_Cur == 0x41 && o.ConnectStatus && o.MARKNUM > 0);
  120. if (info != null)
  121. {
  122. s7.Write(dr["请求进入DB"].ToString(), true);
  123. var inResult = s7.ReadBool(dr["允许进入DB"].ToString());
  124. if (inResult.Content == true)
  125. {
  126. DispatcherTool.ReleaseAgvOnStop(info.MARKNUM, 0);
  127. }
  128. }
  129. else
  130. {
  131. s7.Write(dr["请求进入DB"].ToString(), false);
  132. }
  133. }
  134. //离开
  135. {
  136. var info = infos.Find(o => o.MARKNUM.ToString() == dr["离开完成地标"].ToString());
  137. if (info != null)
  138. {
  139. s7.Write(dr["离开完成DB"].ToString(), true);
  140. if (info.RUNSTATUS_Cur == 0x41)
  141. {
  142. DispatcherTool.ReleaseAgvOnStop(info.MARKNUM, 0);
  143. }
  144. }
  145. else
  146. {
  147. s7.Write(dr["离开完成DB"].ToString(), false);
  148. }
  149. }
  150. //读取PLC值显示
  151. foreach (DataColumn dcol in dtStation.Columns)
  152. {
  153. if (dcol.ColumnName.Contains("DB"))
  154. {
  155. var result = s7.ReadBool(dr[dcol.ColumnName].ToString());
  156. if (dataGridView1.IsHandleCreated && this.IsHandleCreated)
  157. {
  158. dataGridView1.Invoke(new InvokeDelegate(delegate
  159. {
  160. dataGridView1.Rows[index].Cells[dcol.ColumnName].Value = result.Content;
  161. }));
  162. }
  163. }
  164. }
  165. Thread.Sleep(500);
  166. }
  167. catch (Exception)
  168. {
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }