using IMCS.DefaultList; using IMCS.Logic; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using YG; namespace IMCS.UControl { public partial class UserControl3 : UserControl { private short taskRun = 0; bool Run = false; short StartIndex = 0; short EndIndex = 0; public System.ComponentModel.BindingList CurrentTask = new BindingList(); public UserControl3() { InitializeComponent(); this.Load += UserControl3_Load; } YG.Device.DeviceS7 s7; ZhiLiangZhongXinHandle longHaiTe; private void UserControl3_Load(object sender, EventArgs e) { longHaiTe = new ZhiLiangZhongXinHandle(); longHaiTe.DeviceInit(); longHaiTe.DeviceStart(); longHaiTe.Msg += LongHaiTe_Msg; foreach (Control cc in this.tableLayoutPanel1.Controls) { if (cc is Label) { Label lb = (Label)cc; if (!lb.Tag.Equals("999")) { lb.AllowDrop = true; lb.MouseMove += new MouseEventHandler(Lb_MouseDown1); lb.DragEnter += new DragEventHandler(Label_DragEnter); lb.DragDrop += new DragEventHandler(Label_DragDrop); } lb.BackColor = System.Drawing.Color.AliceBlue; lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; lb.AutoSize = false; lb.Width = 100; lb.Height = 45; } } Task.Factory.StartNew(async () => { while (true && !Run) { await Task.Delay(100); if (CurrentTask.Where(m => !m.Task_GameOver).Count() > 0) { CurrentTask.Where(m => !m.Task_GameOver).FirstOrDefault().FirstOrDefaultYG((t) => { if (!t.Task_Send) { Write_Plc((short)t.Task_Index, t.Task_StartIndex, t.Task_EndIndex, t.Task_Operation); t.Task_Send = true; } }); } } }); } private void Lb_MouseDown1(object sender, MouseEventArgs e) { Label startlable = sender as Label; if ((e.Button == System.Windows.Forms.MouseButtons.Left)) { startlable.DoDragDrop(startlable, DragDropEffects.Copy | DragDropEffects.Move); //形成拖拽效果,移动+拷贝的组合效果 } } private void Label_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } private void Write_Plc(short title, short start, short end, short operatio) { lb_TaskId.Invoke(new Action(() => { lb_TaskId.Text = $"ID:{title}"; }));// .Text = $"执行任务ID{title}"; lb_StartPoint.Invoke(new Action(() => { lb_StartPoint.Text = $"起点:{start}"; }));//.Text = $"任务执行起点{start}"; lb_EndPoint.Invoke(new Action(() => { lb_EndPoint.Text = $"终点:{end}"; }));//.Text = $"任务结束点{end}"; lb_Run.Invoke(new Action(() => { lb_Run.Text = $"执行:{operatio}"; })); taskRun = operatio; longHaiTe.DeviceSend("DB200.44", title); System.Threading.Thread.Sleep(500); longHaiTe.DeviceSend("DB200.46", start); System.Threading.Thread.Sleep(500); longHaiTe.DeviceSend("DB200.48", end); System.Threading.Thread.Sleep(500); longHaiTe.DeviceSend("DB200.50", operatio); } private void Label_DragDrop(object sender, DragEventArgs e) { Label lb = (Label)sender; EndIndex = (short)lb.Tag.ObjectToInt(); var cc = (Label)e.Data.GetData(typeof(Label)); StartIndex = cc.Tag.ObjectToShort(); Run = false; { CurrentTask.Add(new WeiLiTaskDefaultList() { Task_Index = CurrentTask.Count + 1, Task_EndIndex = EndIndex, Task_Operation = 1, Task_Type = TaskType.取, Task_StartIndex = StartIndex }); CurrentTask.Add(new WeiLiTaskDefaultList() { Task_Index = CurrentTask.Count + 1, Task_EndIndex = EndIndex, Task_Operation = 4, Task_Type = TaskType.放, Task_StartIndex = StartIndex }); } } private void LongHaiTe_Msg(string obje1, string obje2, string obje3) { System.Console.WriteLine($"{obje1}-->>{obje2}-->{obje3}"); if (obje3.Length - obje3.Replace(",", "").Length != 1) { MessageBox.Show("接受到的值解析出现问题"); return; } string[] vs = obje3.Split(new string[] { "," }, StringSplitOptions.None); short state = vs[1].ObjectToShort(); lb_RunState.Invoke(new Action(() => { lb_RunState.Text = "状态:" + state.ToString(); })); CurrentTask.Where(m => m.Task_Index.Equals(vs[0].StringToInt())).FirstOrDefault().FirstOrDefaultYG((t) => { if (t.Task_Type.Equals(TaskType.取) && state == 3) { t.Task_GameOver = true; } else if (t.Task_Type.Equals(TaskType.放) && state == 6) { t.Task_GameOver = true; } else if (t.Task_Type.Equals(TaskType.移动) && state == 8) { t.Task_GameOver = true; } t.Task_RunState = state; }); } } }