UserControl3.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using IMCS.DefaultList;
  2. using IMCS.Logic;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using YG;
  13. namespace IMCS.UControl
  14. {
  15. public partial class UserControl3 : UserControl
  16. {
  17. private short taskRun = 0;
  18. bool Run = false;
  19. short StartIndex = 0;
  20. short EndIndex = 0;
  21. public System.ComponentModel.BindingList<WeiLiTaskDefaultList> CurrentTask = new BindingList<WeiLiTaskDefaultList>();
  22. public UserControl3()
  23. {
  24. InitializeComponent();
  25. this.Load += UserControl3_Load;
  26. }
  27. YG.Device.DeviceS7 s7;
  28. ZhiLiangZhongXinHandle longHaiTe;
  29. private void UserControl3_Load(object sender, EventArgs e)
  30. {
  31. longHaiTe = new ZhiLiangZhongXinHandle();
  32. longHaiTe.DeviceInit();
  33. longHaiTe.DeviceStart();
  34. longHaiTe.Msg += LongHaiTe_Msg;
  35. foreach (Control cc in this.tableLayoutPanel1.Controls)
  36. {
  37. if (cc is Label)
  38. {
  39. Label lb = (Label)cc;
  40. if (!lb.Tag.Equals("999"))
  41. {
  42. lb.AllowDrop = true;
  43. lb.MouseMove += new MouseEventHandler(Lb_MouseDown1);
  44. lb.DragEnter += new DragEventHandler(Label_DragEnter);
  45. lb.DragDrop += new DragEventHandler(Label_DragDrop);
  46. }
  47. lb.BackColor = System.Drawing.Color.AliceBlue;
  48. lb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  49. lb.AutoSize = false;
  50. lb.Width = 100;
  51. lb.Height = 45;
  52. }
  53. }
  54. Task.Factory.StartNew(async () =>
  55. {
  56. while (true && !Run)
  57. {
  58. await Task.Delay(100);
  59. if (CurrentTask.Where(m => !m.Task_GameOver).Count() > 0)
  60. {
  61. CurrentTask.Where(m => !m.Task_GameOver).FirstOrDefault().FirstOrDefaultYG((t) =>
  62. {
  63. if (!t.Task_Send)
  64. {
  65. Write_Plc((short)t.Task_Index, t.Task_StartIndex, t.Task_EndIndex, t.Task_Operation);
  66. t.Task_Send = true;
  67. }
  68. });
  69. }
  70. }
  71. });
  72. }
  73. private void Lb_MouseDown1(object sender, MouseEventArgs e)
  74. {
  75. Label startlable = sender as Label;
  76. if ((e.Button == System.Windows.Forms.MouseButtons.Left))
  77. {
  78. startlable.DoDragDrop(startlable, DragDropEffects.Copy | DragDropEffects.Move);
  79. //形成拖拽效果,移动+拷贝的组合效果
  80. }
  81. }
  82. private void Label_DragEnter(object sender, DragEventArgs e)
  83. {
  84. e.Effect = DragDropEffects.Copy;
  85. }
  86. private void Write_Plc(short title, short start, short end, short operatio)
  87. {
  88. lb_TaskId.Invoke(new Action(() => { lb_TaskId.Text = $"ID:{title}"; }));// .Text = $"执行任务ID{title}";
  89. lb_StartPoint.Invoke(new Action(() => { lb_StartPoint.Text = $"起点:{start}"; }));//.Text = $"任务执行起点{start}";
  90. lb_EndPoint.Invoke(new Action(() => { lb_EndPoint.Text = $"终点:{end}"; }));//.Text = $"任务结束点{end}";
  91. lb_Run.Invoke(new Action(() => { lb_Run.Text = $"执行:{operatio}"; }));
  92. taskRun = operatio;
  93. longHaiTe.DeviceSend("DB200.44", title);
  94. System.Threading.Thread.Sleep(500);
  95. longHaiTe.DeviceSend("DB200.46", start);
  96. System.Threading.Thread.Sleep(500);
  97. longHaiTe.DeviceSend("DB200.48", end);
  98. System.Threading.Thread.Sleep(500);
  99. longHaiTe.DeviceSend("DB200.50", operatio);
  100. }
  101. private void Label_DragDrop(object sender, DragEventArgs e)
  102. {
  103. Label lb = (Label)sender;
  104. EndIndex = (short)lb.Tag.ObjectToInt();
  105. var cc = (Label)e.Data.GetData(typeof(Label));
  106. StartIndex = cc.Tag.ObjectToShort();
  107. Run = false;
  108. {
  109. CurrentTask.Add(new WeiLiTaskDefaultList() { Task_Index = CurrentTask.Count + 1, Task_EndIndex = EndIndex, Task_Operation = 1, Task_Type = TaskType.取, Task_StartIndex = StartIndex });
  110. CurrentTask.Add(new WeiLiTaskDefaultList() { Task_Index = CurrentTask.Count + 1, Task_EndIndex = EndIndex, Task_Operation = 4, Task_Type = TaskType.放, Task_StartIndex = StartIndex });
  111. }
  112. }
  113. private void LongHaiTe_Msg(string obje1, string obje2, string obje3)
  114. {
  115. System.Console.WriteLine($"{obje1}-->>{obje2}-->{obje3}");
  116. if (obje3.Length - obje3.Replace(",", "").Length != 1)
  117. {
  118. MessageBox.Show("接受到的值解析出现问题");
  119. return;
  120. }
  121. string[] vs = obje3.Split(new string[] { "," }, StringSplitOptions.None);
  122. short state = vs[1].ObjectToShort();
  123. lb_RunState.Invoke(new Action(() => { lb_RunState.Text = "状态:" + state.ToString(); }));
  124. CurrentTask.Where(m => m.Task_Index.Equals(vs[0].StringToInt())).FirstOrDefault().FirstOrDefaultYG((t) =>
  125. {
  126. if (t.Task_Type.Equals(TaskType.取) && state == 3)
  127. {
  128. t.Task_GameOver = true;
  129. }
  130. else if (t.Task_Type.Equals(TaskType.放) && state == 6)
  131. {
  132. t.Task_GameOver = true;
  133. }
  134. else if (t.Task_Type.Equals(TaskType.移动) && state == 8)
  135. {
  136. t.Task_GameOver = true;
  137. }
  138. t.Task_RunState = state;
  139. });
  140. }
  141. }
  142. }