From c01caebb4f2eaa142b6bd442917c0b5482b823e7 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, 9 Nov 2022 21:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A5=BF=E9=97=A8=E5=AD=90=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.S7Net/EntityClassResolution.cs | 26 ++++- BPASmartClient.S7Net/SiemensHelper.cs | 17 +++ SmartClient.sln | 25 ++++- WpfApp1/App.xaml | 9 ++ WpfApp1/App.xaml.cs | 17 +++ WpfApp1/AssemblyInfo.cs | 10 ++ WpfApp1/MainWindow.xaml | 12 +++ WpfApp1/MainWindow.xaml.cs | 90 ++++++++++++++++ WpfApp1/WpfApp1.csproj | 14 +++ WpfApp1/XL_Start_DB.cs | 100 ++++++++++++++++++ 10 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 WpfApp1/App.xaml create mode 100644 WpfApp1/App.xaml.cs create mode 100644 WpfApp1/AssemblyInfo.cs create mode 100644 WpfApp1/MainWindow.xaml create mode 100644 WpfApp1/MainWindow.xaml.cs create mode 100644 WpfApp1/WpfApp1.csproj create mode 100644 WpfApp1/XL_Start_DB.cs diff --git a/BPASmartClient.S7Net/EntityClassResolution.cs b/BPASmartClient.S7Net/EntityClassResolution.cs index 01b52922..75dcc8cd 100644 --- a/BPASmartClient.S7Net/EntityClassResolution.cs +++ b/BPASmartClient.S7Net/EntityClassResolution.cs @@ -214,7 +214,8 @@ namespace BPASmartClient.S7Net List bytes1 = new List(); if (sarray.Length > 2) for (int i = 2; i < sarray.Length; i++) if (sarray[i] > 0) bytes1.Add(sarray[i]); - obj = GetGb2312()?.GetString(bytes1.ToArray()).Trim().Replace(" ", "").Replace("\n", ""); + //obj = GetGb2312()?.GetString(bytes1.ToArray()).Trim().Replace(" ", "").Replace("\n", ""); + obj = bytes1.ToArray().ToStringb().Trim().Replace(" ", "").Replace("\n", ""); numBytes += len; break; default: @@ -243,6 +244,26 @@ namespace BPASmartClient.S7Net return Encoding.GetEncoding("gb2312"); } + + //public static Encoding GetGb2312(this Encoding encoding) + //{ + // Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + // return Encoding.GetEncoding("gb2312"); + //} + + public static byte[] ToArrays(this string value) + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + return Encoding.GetEncoding("gb2312").GetBytes(value); + } + + public static string ToStringb(this byte[] value) + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + return Encoding.GetEncoding("gb2312").GetString(value); + } + + public static double FromBytes(object sourceClass, byte[] bytes, double numBytes = 0.0, bool isInnerClass = false) { if (bytes == null) @@ -332,7 +353,8 @@ namespace BPASmartClient.S7Net if (tempLen != null) len = tempLen.Len + 2; } array = new byte[len]; - var res = GetGb2312()?.GetBytes(propertyValue?.ToString()); + //var res = GetGb2312()?.GetBytes(propertyValue?.ToString()); + var res = propertyValue?.ToString()?.ToArrays(); if (array.Length > 2) array[0] = (byte)(len - 2); array[1] = (byte)res.Length; for (int i = 0; i < res?.Length; i++) diff --git a/BPASmartClient.S7Net/SiemensHelper.cs b/BPASmartClient.S7Net/SiemensHelper.cs index 141fbb5c..d57e30c5 100644 --- a/BPASmartClient.S7Net/SiemensHelper.cs +++ b/BPASmartClient.S7Net/SiemensHelper.cs @@ -1,4 +1,7 @@ using S7.Net; +using System.Net; +using System.Text; +using BPASmartClient.S7Net; namespace BPASmartClient.S7Net { @@ -58,6 +61,20 @@ namespace BPASmartClient.S7Net return myPlc?.Read(dataType, db, address, varType, count); } + public void WriteString(DataType dataType, int db, int startByteAdr, string value) + { + if (!IsConnected) return; + var res = value.ToArrays(); + if (res != null) + { + List bytes = new List(); + bytes.Add((byte)res.Length); + bytes.Add((byte)res.Length); + bytes.AddRange(res); + myPlc?.WriteBytes(dataType, db, startByteAdr, bytes.ToArray()); + } + } + public string Write(string address, TValue value, int Retries = 1) { if (IsConnected) diff --git a/SmartClient.sln b/SmartClient.sln index 28119da9..c4bfe8f7 100644 --- a/SmartClient.sln +++ b/SmartClient.sln @@ -192,7 +192,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.ScreenSplitM EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.ScreenLib", "BPASmartClient.ScreenLib\BPASmartClient.ScreenLib.csproj", "{798BBFFE-3BA2-40BA-BFE1-9502398F1F08}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfTest", "WpfTest\WpfTest.csproj", "{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfTest", "WpfTest\WpfTest.csproj", "{CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApp1", "WpfApp1\WpfApp1.csproj", "{81D03688-7361-45B8-A3EB-9B0A9222A338}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -1808,6 +1810,26 @@ Global {CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x64.Build.0 = Release|Any CPU {CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x86.ActiveCfg = Release|Any CPU {CD43FABA-D1ED-4524-AC51-60ECDE2EAD44}.Release|x86.Build.0 = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM.ActiveCfg = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM.Build.0 = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|ARM64.Build.0 = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x64.ActiveCfg = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x64.Build.0 = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x86.ActiveCfg = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Debug|x86.Build.0 = Debug|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|Any CPU.Build.0 = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM.ActiveCfg = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM.Build.0 = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM64.ActiveCfg = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|ARM64.Build.0 = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x64.ActiveCfg = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x64.Build.0 = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x86.ActiveCfg = Release|Any CPU + {81D03688-7361-45B8-A3EB-9B0A9222A338}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1897,6 +1919,7 @@ Global {135A0978-351F-4727-82CE-2B7A8632AF1B} = {C5968A90-F4CF-4904-88C4-62C77C820A04} {798BBFFE-3BA2-40BA-BFE1-9502398F1F08} = {C6984F48-A0FE-47A4-B63B-E4E5BCFCD6D3} {CD43FABA-D1ED-4524-AC51-60ECDE2EAD44} = {7BE13E55-D8B1-4232-AE63-8BF122633297} + {81D03688-7361-45B8-A3EB-9B0A9222A338} = {8712125E-14CD-4E1B-A1CE-4BDE03805942} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC} diff --git a/WpfApp1/App.xaml b/WpfApp1/App.xaml new file mode 100644 index 00000000..2e70522d --- /dev/null +++ b/WpfApp1/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/WpfApp1/App.xaml.cs b/WpfApp1/App.xaml.cs new file mode 100644 index 00000000..909eaa54 --- /dev/null +++ b/WpfApp1/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace WpfApp1 +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/WpfApp1/AssemblyInfo.cs b/WpfApp1/AssemblyInfo.cs new file mode 100644 index 00000000..8b5504ec --- /dev/null +++ b/WpfApp1/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/WpfApp1/MainWindow.xaml b/WpfApp1/MainWindow.xaml new file mode 100644 index 00000000..cfada060 --- /dev/null +++ b/WpfApp1/MainWindow.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/WpfApp1/MainWindow.xaml.cs b/WpfApp1/MainWindow.xaml.cs new file mode 100644 index 00000000..5c44b05d --- /dev/null +++ b/WpfApp1/MainWindow.xaml.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using BPASmartClient.S7Net; + +namespace WpfApp1 +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + SiemensHelper siemensHelper = new SiemensHelper(); + siemensHelper.Connect(S7.Net.CpuType.S71200, "192.168.101.16"); + if (siemensHelper.IsConnected) + { + siemensHelper.WriteString(S7.Net.DataType.DataBlock, 1, 0, "cccc"); + var res = siemensHelper.ReadClass(1); + + //XL_Start_DB xL_Start_DB = new XL_Start_DB(); + //xL_Start_DB.RecipeCode = "编码"; + //xL_Start_DB.RecipeName = "名称"; + //xL_Start_DB.StockCode = 1; + //xL_Start_DB.TrayCode = 2; + //for (int i = 0; i < xL_Start_DB.Powder.Length; i++) + //{ + // xL_Start_DB.Powder[i].Powder_Name = $"料{i}"; + // xL_Start_DB.Powder[i].Powder_Weight = (float)(5.0 + i); + //} + //for (int i = 0; i < xL_Start_DB.Material.Length; i++) + //{ + // xL_Start_DB.Material[i].Material_Name = $"原料{i}"; + // xL_Start_DB.Material[i].Material_Weight = (float)(50.0 + i); + // xL_Start_DB.Material[i].Material_BarrelNum = (short)(i + 1); + //} + //xL_Start_DB.Order_Request_ACK = true; + //xL_Start_DB.Order_Request = true; + + //for (int i = 0; i < xL_Start_DB.Standby.Length; i++) + //{ + // xL_Start_DB.Standby[i] = (short)(i + 100); + // xL_Start_DB.Standby1[i] = (short)(i + 300); + //} + + res.RecipeCode = ""; + res.RecipeName = ""; + res.StockCode = 0; + res.TrayCode = 0; + for (int i = 0; i < res.Powder.Length; i++) + { + res.Powder[i].Powder_Name = ""; + res.Powder[i].Powder_Weight = 0; + } + for (int i = 0; i < res.Material.Length; i++) + { + res.Material[i].Material_Name = ""; + res.Material[i].Material_Weight = 0; + res.Material[i].Material_BarrelNum = 0; + } + res.Order_Request_ACK = false; + res.Order_Request = false; + + for (int i = 0; i < res.Standby.Length; i++) + { + res.Standby[i] = 0; + res.Standby1[i] = 0; + } + + siemensHelper.WriteClass(res, 1); + + var res1 = siemensHelper.ReadClass(1); + } + + } + } +} diff --git a/WpfApp1/WpfApp1.csproj b/WpfApp1/WpfApp1.csproj new file mode 100644 index 00000000..1d569dcc --- /dev/null +++ b/WpfApp1/WpfApp1.csproj @@ -0,0 +1,14 @@ + + + + WinExe + net6.0-windows + enable + true + + + + + + + diff --git a/WpfApp1/XL_Start_DB.cs b/WpfApp1/XL_Start_DB.cs new file mode 100644 index 00000000..4800ca45 --- /dev/null +++ b/WpfApp1/XL_Start_DB.cs @@ -0,0 +1,100 @@ +using BPASmartClient.S7Net; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WpfApp1 +{ + internal class XL_Start_DB + { + //public XL_Start_DB() + //{ + // for (int i = 0; i < Powder.Length; i++) + // { + // Powder[i] = new UDT2Start(); + // } + + // for (int i = 0; i < Material.Length; i++) + // { + // Material[i] = new UDTStart(); + // } + //} + + /// + /// 配方编码 + /// + [Siemens(16)] + public string RecipeCode { get; set; } + /// + /// 配发名称 + /// + /// + [Siemens(16)] + public string RecipeName { get; set; } + + /// + /// 工站的编号 + /// + public short StockCode { get; set; } + + /// + /// 托盘编号 + /// + public short TrayCode { get; set; } + + public UDT2Start[] Powder { get; set; } = new UDT2Start[10]; + + /// + /// 物料信息 + /// + public UDTStart[] Material { get; set; } = new UDTStart[20]; + + /// + /// 西门子下发订单 + /// + public bool Order_Request_ACK { get; set; } + + public short[] Standby { get; set; } = new short[4]; + + /// + /// 上位机订单请求 + /// + public bool Order_Request { get; set; } + + public short[] Standby1 { get; set; } = new short[4]; + + } + + public class UDTStart + { + [Siemens(6)] + /// + /// 原料名称 + /// + public string Material_Name { get; set; } + /// + /// 原料重量 + /// + public float Material_Weight { get; set; } + /// + /// 原料桶号 + /// + public short Material_BarrelNum { get; set; } + } + + + public class UDT2Start + { + /// + /// 粉料仓名称 + /// + [Siemens(6)] + public string Powder_Name { get; set; } + /// + /// 粉料仓重量 + /// + public float Powder_Weight { get; set; } + } +}