123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.OleDb;
- using System.Threading;
- using AGV_WPF_Global;
- using AGV_WPF.Tools;
- using System.Threading.Tasks;
- namespace AGV_WPF
- {
- public partial class DispatchSystem7352 : Form
- {
- DataTable dtStation = new DataTable();
- private delegate void InvokeDelegate();
- public DispatchSystem7352()
- {
- InitializeComponent();
- }
- private void DispatchSystem7352_Load(object sender, EventArgs e)
- {
- LoadConfig();
- }
- public void LoadConfig()
- {
- string currPath = System.AppDomain.CurrentDomain.BaseDirectory;
- string dataPath = currPath + @"data";
- string dataFile = dataPath + @"\物料信息表.xls";
- try
- {
- string confStr1 = @"Provider=Microsoft.ACE.OLEDB.12.0;";
- string confStr2 = @"Persist Security Info=false;";
- string confStr3 = @"Data Source=" + dataFile + ";";
- string confStr4 = @"Extended Properties=" + "'Excel 12.0;HDR=YES;IMEX=1'";
- string connStr = confStr1 + confStr2 + confStr3 + confStr4;
- OleDbConnection oleDC = new OleDbConnection(connStr);
- OleDbDataAdapter oleDDA1 = new OleDbDataAdapter("Select * From [自动工位表$]", oleDC);
- oleDDA1.Fill(dtStation);
- oleDC.Close();
- foreach (DataColumn dcol in dtStation.Columns)
- {
- if (dcol.ColumnName.Contains("DB"))// && !dcol.ColumnName.Contains("心跳"))
- {
- DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
- col.HeaderText = dcol.ColumnName;
- col.Name = dcol.ColumnName;
- col.ReadOnly = true;
- col.Resizable = DataGridViewTriState.True;
- col.SortMode = DataGridViewColumnSortMode.Automatic;
- dataGridView1.Columns.Add(col);
- }
- //else if (dcol.ColumnName.Contains("DB") && dcol.ColumnName.Contains("心跳"))
- //{
- // DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
- // col.HeaderText = dcol.ColumnName;
- // col.Name = dcol.ColumnName;
- // col.ReadOnly = true;
- // col.Resizable = DataGridViewTriState.True;
- // col.SortMode = DataGridViewColumnSortMode.Automatic;
- // dataGridView1.Columns.Add(col);
- //}
- }
- for (int i = 0; i < dtStation.Rows.Count; i++)
- {
- int index = dataGridView1.Rows.Add();
- dataGridView1.Rows[index].Cells[0].Value = dtStation.Rows[i][0].ToString();
- dataGridView1.Rows[index].Cells[1].Value = dtStation.Rows[i][1].ToString();
- var dr = dtStation.Rows[i];
- // MyTest.ManagedThreadPoolcs.ManagedThreadPool.QueueUserWorkItem(new WaitCallback(Test), dtStation.Rows[i]);
- Task.Factory.StartNew(() => Test(dr));
- Thread.Sleep(50);
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- }
- public void Test(object Dr)
- {
- if (Dr != null)
- {
- DataRow dr = (DataRow)Dr;
- int index = dtStation.Rows.IndexOf(dr);
- HslCommunication.Profinet.Siemens.SiemensS7Net s7 = null;
- {
- s7 = new HslCommunication.Profinet.Siemens.SiemensS7Net(HslCommunication.Profinet.Siemens.SiemensPLCS.S1200,
- dr["PLC地址"].ToString());
- s7.Rack = (byte)0;
- s7.Slot = (byte)0;
- s7.ConnectTimeOut = 1200;
- s7.ReceiveTimeOut = 1200;
- s7.SetPersistentConnection();
- }
- while (true)
- {
- try
- {
- //心跳
- if (!string.IsNullOrEmpty(dr["中控心跳DB"].ToString()))
- {
- var HeartBit = s7.ReadBool(dr["中控心跳DB"].ToString());
- if (!HeartBit.IsSuccess)
- {
- Thread.Sleep(1000);
- continue;
- }
- {
- s7.Write(dr["中控心跳DB"].ToString(), !HeartBit.Content);
- }
- }
- var infos = GlobalPara.AgvInfoDic.Values.ToList().FindAll(o=>o.ConnectStatus==true&&o.AGVNUM>0).ToList();
- //进入
- {
- var info = infos.Find(o => o.MARKNUM.ToString() == dr["请求进入地标"].ToString() && o.RUNSTATUS_Cur == 0x41 && o.ConnectStatus && o.MARKNUM > 0);
- if (info != null)
- {
- s7.Write(dr["请求进入DB"].ToString(), true);
- var inResult = s7.ReadBool(dr["允许进入DB"].ToString());
- if (inResult.Content == true)
- {
- DispatcherTool.ReleaseAgvOnStop(info.MARKNUM, 0);
- }
- }
- else
- {
- s7.Write(dr["请求进入DB"].ToString(), false);
- }
- }
- //离开
- {
- var info = infos.Find(o => o.MARKNUM.ToString() == dr["离开完成地标"].ToString());
- if (info != null)
- {
- s7.Write(dr["离开完成DB"].ToString(), true);
- if (info.RUNSTATUS_Cur == 0x41)
- {
- DispatcherTool.ReleaseAgvOnStop(info.MARKNUM, 0);
- }
- }
- else
- {
- s7.Write(dr["离开完成DB"].ToString(), false);
- }
- }
- //读取PLC值显示
- foreach (DataColumn dcol in dtStation.Columns)
- {
- if (dcol.ColumnName.Contains("DB"))
- {
- var result = s7.ReadBool(dr[dcol.ColumnName].ToString());
- if (dataGridView1.IsHandleCreated && this.IsHandleCreated)
- {
- dataGridView1.Invoke(new InvokeDelegate(delegate
- {
- dataGridView1.Rows[index].Cells[dcol.ColumnName].Value = result.Content;
- }));
- }
- }
- }
- Thread.Sleep(500);
- }
- catch (Exception)
- {
- }
- }
- }
- }
- }
- }
|