| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 | 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 UserControl2 : UserControl    {        bool Run = false;        short StartIndex = 0;        short EndIndex = 0;        public System.ComponentModel.BindingList<WeiLiTaskDefaultList> CurrentTask = new BindingList<WeiLiTaskDefaultList>();        public UserControl2()        {            InitializeComponent();            this.Load += UserControl2_Load;        }        YG.Device.DeviceS7 s7;        LongHaiTe longHaiTe;        private void UserControl2_Load(object sender, EventArgs e)        {                       longHaiTe = new LongHaiTe();            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.Text.Equals("控制机柜"))                    {                        lb.AllowDrop = true;                        lb.MouseDown += 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 = 60;                        lb.Height = 60;                    }                }            }            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)        {            longHaiTe.DeviceSend("DB3.12", title.ToString().ReturnBtyesWtitString());            longHaiTe.DeviceSend("DB3.6", start);            longHaiTe.DeviceSend("DB3.8", end);            longHaiTe.DeviceSend("DB3.10", 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;            int currentTaskCount = CurrentTask.Where(m => m.Task_StartIndex.Equals(StartIndex) && m.Task_EndIndex.Equals(EndIndex) && m.Task_GameOver).Count();            if (currentTaskCount > 0)            {                System.Windows.Forms.MessageBox.Show("当前任务列表中存在和拖拽内容相似的任务,当前拖拽不执行");            }            else            {                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 = 3, 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[0].ObjectToShort();            CurrentTask.Where(m => m.Task_Index.Equals(vs[1].StringToInt())).FirstOrDefault().FirstOrDefaultYG((t) =>            {                if (t.Task_Type.Equals(TaskType.取) && state == 2)                {                    t.Task_GameOver = true;                }                else if (t.Task_Type.Equals(TaskType.放) && state == 4)                {                    t.Task_GameOver = true;                }                t.Task_RunState = state;            });        }        private void button2_Click(object sender, EventArgs e)        {            //short st = textBox3.Text.ObjectToShort(); ;            //longHaiTe.DeviceSend(textBox4.Text, st);            //var cc = longHaiTe.DeviceRead<short>(this.textBox3.Text);            //System.Windows.Forms.MessageBox.Show($"{cc}+>>");        }        private void button1_Click(object sender, EventArgs e)        {            //byte st = textBox1.Text.ObjectToByte();            //byte et = textBox2.Text.ObjectToByte();            //byte ex = this.textBox5.Text.ObjectToByte();            //s7.SiemensS7Net.Write("DB3.6", st);            //System.Threading.Thread.Sleep(200);            //s7.SiemensS7Net.Write("DB3.8", et);            //System.Threading.Thread.Sleep(200);            //s7.SiemensS7Net.Write("DB3.10", ex);            Byte[] cc = s7.SiemensS7Net.Read("DB3.10", 4).Content;            short tt = cc.GetShort(2);        }        private void button3_Click(object sender, EventArgs e)        {            //var cc = longHaiTe.DeviceRead<short>(this.textBox4.Text);            //MessageBox.Show(cc.ToString());        }        int iiii = 0;        private void button1_Click_1(object sender, EventArgs e)        {            string guid = Guid.NewGuid().ToString().Substring(0, 5);            byte[] bt = new byte[30];            byte[] item = Encoding.ASCII.GetBytes(guid);            Array.Copy(item, 0, bt, 0, item.Length);            longHaiTe.DeviceSend("DB3.12", bt);            //lb_view.Text = guid;            MessageBox.Show(guid);        }        private void button2_Click_1(object sender, EventArgs e)        {            //var CC = longHaiTe.DeviceRead<byte[]>("DB3.10", 22);            //{            //    MessageBox.Show(BitConverter.ToString(CC));            //}        }        private void button1_Click_2(object sender, EventArgs e)        {            string guid = Guid.NewGuid().ToString().Substring(0, 5);            byte[] bt = new byte[30];            byte[] item = Encoding.ASCII.GetBytes(guid);            Array.Copy(item, 0, bt, 0, item.Length);            longHaiTe.DeviceSend("DB3.12", bt);            //lb_view.Text = guid;            MessageBox.Show(guid);        }    }}
 |