From e3a1fcc38c8f659ea4a2f47b40a798367a902756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=84=8F=20=E5=BD=AD?= <2417589739@qq.com> Date: Wed, 28 Sep 2022 17:14:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HKCardOUT/App.xaml | 19 ++++---- HKCardOUT/Properties/DesignTimeResources.xaml | 3 +- HKCardOUT/Views/RootView.xaml.cs | 1 + HKControl/Main.cs | 33 ++++++++++---- HKControl/Siemens.cs | 21 +++++++-- UHFHelper/UHF_RS485_Helper.cs | 17 +++++-- test/Form1.Designer.cs | 45 +++++++++++++++++-- test/Form1.cs | 28 +++++++++--- test/test.csproj | 1 + 9 files changed, 131 insertions(+), 37 deletions(-) diff --git a/HKCardOUT/App.xaml b/HKCardOUT/App.xaml index a3177d4..59364ad 100644 --- a/HKCardOUT/App.xaml +++ b/HKCardOUT/App.xaml @@ -1,18 +1,19 @@ - + - + - - + + diff --git a/HKCardOUT/Properties/DesignTimeResources.xaml b/HKCardOUT/Properties/DesignTimeResources.xaml index 2e644b9..6a45a53 100644 --- a/HKCardOUT/Properties/DesignTimeResources.xaml +++ b/HKCardOUT/Properties/DesignTimeResources.xaml @@ -1,5 +1,4 @@ - + diff --git a/HKCardOUT/Views/RootView.xaml.cs b/HKCardOUT/Views/RootView.xaml.cs index 2ddced3..95a97b3 100644 --- a/HKCardOUT/Views/RootView.xaml.cs +++ b/HKCardOUT/Views/RootView.xaml.cs @@ -18,6 +18,7 @@ namespace HKCardOUT.Views private void RootView_Loaded(object sender, RoutedEventArgs e) { (this.DataContext as RootViewModel).Main = this; + HKControl.Main.GetInstance.Init(); } } } diff --git a/HKControl/Main.cs b/HKControl/Main.cs index e2f6bc0..e78e699 100644 --- a/HKControl/Main.cs +++ b/HKControl/Main.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Diagnostics; using HKHelper; using S7.Net; @@ -63,9 +64,9 @@ namespace HKControl if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); } ThreadManage.GetInstance().Start(new Action(() => { - SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress); SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() => { + Debug.WriteLine($"{item.DeviceNum}:连接成功"); ThreadManage.GetInstance().StartLong(new Action(() => { var res = SiemensDicitonary[item.DeviceNum].Read("MB7"); @@ -86,6 +87,7 @@ namespace HKControl Thread.Sleep(100); }), $"{item.DeviceNum} 号设备监听"); }); + SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress); }), $"{item.DeviceNum} 号设备连接初始化"); }); @@ -103,22 +105,28 @@ namespace HKControl switch (carNum) { case 1: - SiemensDicitonary[1].Write("M6.0", true); + //SiemensDicitonary[1].Write("M6.0", true); + Control(1, "M6.0"); break; case 2: - SiemensDicitonary[1].Write("M6.1", true); + //SiemensDicitonary[1].Write("M6.1", true); + Control(1, "M6.1"); break; case 3: - SiemensDicitonary[2].Write("M6.0", true); + //SiemensDicitonary[2].Write("M6.0", true); + Control(2, "M6.0"); break; case 4: - SiemensDicitonary[2].Write("M6.1", true); + //SiemensDicitonary[2].Write("M6.1", true); + Control(2, "M6.1"); break; case 5: - SiemensDicitonary[3].Write("M6.0", true); + //SiemensDicitonary[3].Write("M6.0", true); + Control(3, "M6.0"); break; case 6: - SiemensDicitonary[3].Write("M6.1", true); + //SiemensDicitonary[3].Write("M6.1", true); + Control(3, "M6.1"); break; default: @@ -130,6 +138,13 @@ namespace HKControl }), "开始运行设备"); } + private void Control(int num, string add) + { + SiemensDicitonary[num].Write(add, true); + Thread.Sleep(1000); + SiemensDicitonary[num].Write(add, false); + } + private void DataInit() { Json.Read(); @@ -140,7 +155,7 @@ namespace HKControl Json.Data.CommunicationModels.Add(new CommunicationModel() { IpAddress = "192.168.0.1", - DeviceNum = 1 + DeviceNum = 3 }); Json.Data.CommunicationModels.Add(new CommunicationModel() @@ -152,7 +167,7 @@ namespace HKControl Json.Data.CommunicationModels.Add(new CommunicationModel() { IpAddress = "192.168.0.3", - DeviceNum = 3 + DeviceNum = 1 }); Json.Save(); diff --git a/HKControl/Siemens.cs b/HKControl/Siemens.cs index c183953..1afa235 100644 --- a/HKControl/Siemens.cs +++ b/HKControl/Siemens.cs @@ -1,6 +1,7 @@ using S7.Net; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,10 +21,24 @@ namespace HKControl /// plc 端口号 /// PLC 机架号 /// PLC 插槽号 - public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 2) + public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0) { - myPlc = new Plc(cpuType, ip, port, rack, solt); - myPlc.Open(); + try + { + myPlc = new Plc(cpuType, ip, port, rack, solt); + myPlc.Open(); + if (myPlc.IsConnected) + { + ConnectOk?.Invoke(); + Debug.WriteLine($"设备{ip}连接成功"); + } + } + catch (Exception ex) + { + Debug.WriteLine($"设备连接出错:{ip}"); + //Debug.WriteLine(ex.ToString()); + } + } /// diff --git a/UHFHelper/UHF_RS485_Helper.cs b/UHFHelper/UHF_RS485_Helper.cs index 1c9e29b..17424e2 100644 --- a/UHFHelper/UHF_RS485_Helper.cs +++ b/UHFHelper/UHF_RS485_Helper.cs @@ -26,10 +26,19 @@ namespace UHFHelper /// public void Open(SerialParam param) { - _serialParam = param; - _serialPort = new SerialPort(_serialParam.PortName, _serialParam.BaudRate, _serialParam.PortParity, _serialParam.DataBits); - _serialPort.Open(); - + if (SerialPort.GetPortNames().Contains(param.PortName)) + { + _serialParam = param; + _serialPort = new SerialPort(_serialParam.PortName, _serialParam.BaudRate, _serialParam.PortParity, _serialParam.DataBits); + try + { + _serialPort.Open(); + } + catch (Exception) + { + } + + } } /// /// 获取串口状态 diff --git a/test/Form1.Designer.cs b/test/Form1.Designer.cs index 05ce488..7805f6f 100644 --- a/test/Form1.Designer.cs +++ b/test/Form1.Designer.cs @@ -44,8 +44,11 @@ this.listView1 = new System.Windows.Forms.ListView(); this.columnHeader1 = new System.Windows.Forms.ColumnHeader(); this.columnHeader2 = new System.Windows.Forms.ColumnHeader(); - this.button9 = new System.Windows.Forms.Button(); this.columnHeader3 = new System.Windows.Forms.ColumnHeader(); + this.button9 = new System.Windows.Forms.Button(); + this.button10 = new System.Windows.Forms.Button(); + this.textBox2 = new System.Windows.Forms.TextBox(); + this.button11 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 @@ -191,6 +194,10 @@ // this.columnHeader2.Text = "号码"; // + // columnHeader3 + // + this.columnHeader3.Text = "次数"; + // // button9 // this.button9.Location = new System.Drawing.Point(88, 387); @@ -201,15 +208,42 @@ this.button9.UseVisualStyleBackColor = true; this.button9.Click += new System.EventHandler(this.button9_Click); // - // columnHeader3 + // button10 // - this.columnHeader3.Text = "次数"; + this.button10.Location = new System.Drawing.Point(126, 517); + this.button10.Name = "button10"; + this.button10.Size = new System.Drawing.Size(127, 37); + this.button10.TabIndex = 14; + this.button10.Text = "PLC 初始化"; + this.button10.UseVisualStyleBackColor = true; + this.button10.Click += new System.EventHandler(this.button10_Click); + // + // textBox2 + // + this.textBox2.Location = new System.Drawing.Point(270, 472); + this.textBox2.Name = "textBox2"; + this.textBox2.Size = new System.Drawing.Size(127, 23); + this.textBox2.TabIndex = 15; + this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged); + // + // button11 + // + this.button11.Location = new System.Drawing.Point(270, 517); + this.button11.Name = "button11"; + this.button11.Size = new System.Drawing.Size(127, 37); + this.button11.TabIndex = 16; + this.button11.Text = "开始出餐"; + this.button11.UseVisualStyleBackColor = true; + this.button11.Click += new System.EventHandler(this.button11_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1075, 589); + this.Controls.Add(this.button11); + this.Controls.Add(this.textBox2); + this.Controls.Add(this.button10); this.Controls.Add(this.button9); this.Controls.Add(this.listView1); this.Controls.Add(this.button8); @@ -253,5 +287,8 @@ private ColumnHeader columnHeader2; private Button button9; private ColumnHeader columnHeader3; + private Button button10; + private TextBox textBox2; + private Button button11; } } \ No newline at end of file diff --git a/test/Form1.cs b/test/Form1.cs index 4c55f12..ec1f9a9 100644 --- a/test/Form1.cs +++ b/test/Form1.cs @@ -61,7 +61,7 @@ namespace test add.Add(02); } UHF_RS485_Helper uHF_RS485_Helper; - private void button5_Click(object sender, EventArgs e) + private void button5_Click(object sender, EventArgs e) { // ʼ UHF_RS485_Helper.GetInstance().Open(new SerialParam @@ -76,11 +76,11 @@ namespace test } } - private void button6_Click(object sender, EventArgs e) + private void button6_Click(object sender, EventArgs e) { if (UHF_RS485_Helper.GetInstance().GetSerialPortState()) { - var oldss = UHF_RS485_Helper.GetInstance().ReadCard(01); + var oldss = UHF_RS485_Helper.GetInstance().ReadCard(01); var sss = oldss?.ResData; textBox4.Text = sss; label2.Text = sss; @@ -99,7 +99,7 @@ namespace test ti.Elapsed += Ti_Elapsed; ti.Start(); } - + private void Ti_Elapsed(object? sender, System.Timers.ElapsedEventArgs e) { foreach (var item in add) @@ -111,7 +111,7 @@ namespace test //this.listView1.Items.Add(lvi); this.Invoke(() => { - if (listView1.Items.Count==0) + if (listView1.Items.Count == 0) { ListViewItem lvi = new ListViewItem(); lvi.SubItems.Add(oldss.ResData ?? ""); @@ -146,7 +146,23 @@ namespace test ListViewItem lvi = new ListViewItem("1"); lvi.SubItems.Add("1,1"); lvi.SubItems.Add("2,2"); - + + } + + private void button10_Click(object sender, EventArgs e) + { + HKControl.Main.GetInstance.Init(); + } + + private void textBox2_TextChanged(object sender, EventArgs e) + { + + } + + private void button11_Click(object sender, EventArgs e) + { + int num = Convert.ToInt32(textBox2.Text.Trim()); + HKControl.Main.GetInstance.Start(num); } } } \ No newline at end of file diff --git a/test/test.csproj b/test/test.csproj index dcd2ca6..a408899 100644 --- a/test/test.csproj +++ b/test/test.csproj @@ -10,6 +10,7 @@ +