From 7ced2375b0c858bcc180b0bf54edae321f0fe734 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, 19 Oct 2022 20:43:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A5=BF=E9=97=A8=E5=AD=90=E9=80=9A=E8=AE=AF?= =?UTF-8?q?=E5=BA=93=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 | 64 ++++++++++++++----- BPASmartClient.S7Net/SiemensAttribute.cs | 19 ++++++ TestDemo/Form1.cs | 13 +++- TestDemo/ReadClass.cs | 18 ++++++ TestDemo/TestDemo.csproj | 1 + 5 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 BPASmartClient.S7Net/SiemensAttribute.cs create mode 100644 TestDemo/ReadClass.cs diff --git a/BPASmartClient.S7Net/EntityClassResolution.cs b/BPASmartClient.S7Net/EntityClassResolution.cs index 8a994b4a..012acfb1 100644 --- a/BPASmartClient.S7Net/EntityClassResolution.cs +++ b/BPASmartClient.S7Net/EntityClassResolution.cs @@ -19,7 +19,7 @@ namespace BPASmartClient.S7Net select p; } - private static double GetIncreasedNumberOfBytes(double numBytes, Type type) + private static double GetIncreasedNumberOfBytes(double numBytes, Type type, PropertyInfo propertyInfo) { switch (type.Name) { @@ -46,7 +46,13 @@ namespace BPASmartClient.S7Net break; case "String": //numBytes += 256.0; - numBytes += 1.0; + double len = 256.0; + if (propertyInfo.CustomAttributes.Count() > 0) + { + var tempLen = propertyInfo.GetCustomAttribute(); + if (tempLen != null) len = Convert.ToDouble(tempLen.Len) + 2.0; + } + numBytes += len; break; default: numBytes = GetClassSize(Activator.CreateInstance(type), numBytes, isInnerProperty: true); @@ -80,12 +86,12 @@ namespace BPASmartClient.S7Net IncrementToEven(ref numBytes); for (int i = 0; i < array.Length; i++) { - numBytes = GetIncreasedNumberOfBytes(numBytes, elementType); + numBytes = GetIncreasedNumberOfBytes(numBytes, elementType, accessableProperty); } } else { - numBytes = GetIncreasedNumberOfBytes(numBytes, accessableProperty.PropertyType); + numBytes = GetIncreasedNumberOfBytes(numBytes, accessableProperty.PropertyType, accessableProperty); } } @@ -101,7 +107,7 @@ namespace BPASmartClient.S7Net return numBytes; } - private static object? GetPropertyValue(Type propertyType, byte[] bytes, ref double numBytes) + private static object? GetPropertyValue(Type propertyType, byte[] bytes, PropertyInfo propertyInfo, ref double numBytes) { object obj = null; switch (propertyType.Name) @@ -195,10 +201,20 @@ namespace BPASmartClient.S7Net //obj = GetGb2312()?.GetString(sarray).Trim().Replace(" ", "").Replace("\n", ""); //numBytes += 256.0; - byte[] sarray = new byte[1]; - Array.Copy(bytes, (int)numBytes, sarray, 0, 1); - obj = GetGb2312()?.GetString(sarray).Trim().Replace(" ", "").Replace("\n", ""); - numBytes += 1; + int len = 256; + if (propertyInfo.CustomAttributes.Count() > 0) + { + var tempLen = propertyInfo.GetCustomAttribute(); + if (tempLen != null) len = tempLen.Len + 2; + } + + byte[] sarray = new byte[len]; + Array.Copy(bytes, (int)numBytes, sarray, 0, len); + 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", ""); + numBytes += len; break; default: { @@ -240,19 +256,19 @@ namespace BPASmartClient.S7Net break; } - array.SetValue(GetPropertyValue(elementType, bytes, ref numBytes), i); + array.SetValue(GetPropertyValue(elementType, bytes, accessableProperty, ref numBytes), i); } } else { - accessableProperty.SetValue(sourceClass, GetPropertyValue(accessableProperty.PropertyType, bytes, ref numBytes), null); + accessableProperty.SetValue(sourceClass, GetPropertyValue(accessableProperty.PropertyType, bytes, accessableProperty, ref numBytes), null); } } return numBytes; } - private static double SetBytesFromProperty(object propertyValue, byte[] bytes, double numBytes) + private static double SetBytesFromProperty(object propertyValue, byte[] bytes, double numBytes, PropertyInfo propertyInfo) { int num = 0; int num2 = 0; @@ -298,9 +314,25 @@ namespace BPASmartClient.S7Net array = LReal.ToByteArray((double)propertyValue); break; case "String": - array = new byte[256]; + //array = new byte[256]; + //var res = GetGb2312()?.GetBytes(propertyValue?.ToString()); + //for (int i = 0; i < res?.Length; i++) { array[i] = res[i]; } + int len = 256; + if (propertyInfo.CustomAttributes.Count() > 0) + { + var tempLen = propertyInfo.GetCustomAttribute(); + if (tempLen != null) len = tempLen.Len + 2; + } + array = new byte[len]; var res = GetGb2312()?.GetBytes(propertyValue?.ToString()); - for (int i = 0; i < res?.Length; i++) { array[i] = res[i]; } + if (array.Length > 2) array[0] = (byte)(len - 2); array[1] = (byte)res.Length; + + for (int i = 0; i < res?.Length; i++) + { + int index = i + 2; + if (index < array.Length) + array[index] = res[i]; + } break; default: numBytes = ToBytes(propertyValue, bytes, numBytes); @@ -339,12 +371,12 @@ namespace BPASmartClient.S7Net break; } - numBytes = SetBytesFromProperty(array.GetValue(i), bytes, numBytes); + numBytes = SetBytesFromProperty(array.GetValue(i), bytes, numBytes, accessableProperty); } } else { - numBytes = SetBytesFromProperty(accessableProperty.GetValue(sourceClass, null), bytes, numBytes); + numBytes = SetBytesFromProperty(accessableProperty.GetValue(sourceClass, null), bytes, numBytes, accessableProperty); } } diff --git a/BPASmartClient.S7Net/SiemensAttribute.cs b/BPASmartClient.S7Net/SiemensAttribute.cs new file mode 100644 index 00000000..02977223 --- /dev/null +++ b/BPASmartClient.S7Net/SiemensAttribute.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.S7Net +{ + [AttributeUsage(AttributeTargets.Property)] + public class SiemensAttribute : Attribute + { + public SiemensAttribute(int len) + { + Len = len; + } + + public int Len { get; set; } + } +} diff --git a/TestDemo/Form1.cs b/TestDemo/Form1.cs index 6ff6e854..b0bb2a54 100644 --- a/TestDemo/Form1.cs +++ b/TestDemo/Form1.cs @@ -3,6 +3,7 @@ using BPASmartClient.Helper; using BPASmartClient.Http; using BPASmartClient.HubHelper; using Newtonsoft.Json; +using BPASmartClient.S7Net; namespace TestDemo { @@ -14,6 +15,12 @@ namespace TestDemo { InitializeComponent(); + SiemensHelper siemensHelper = new SiemensHelper(); + siemensHelper.Connect(S7.Net.CpuType.S71200, "192.168.101.11"); + var res = siemensHelper.ReadClass(1); + + siemensHelper.WriteClass(new ReadClass() { temp1 = "ÎÒ", temp2 = "Äã" }, 1); + ActionManage.GetInstance.Register(new Action(() => { label1.Text = (count++).ToString(); @@ -38,9 +45,9 @@ namespace TestDemo private void button2_Click(object sender, EventArgs e) { - Upstreamrequest aGVToUpSystem = new Upstreamrequest() { agvCode="123" }; - // string value = JsonConvert.SerializeObject(aGVToUpSystem); - // string url = $"http://192.168.1.40:8089/apicallback/quicktron/robotjob/upstreamrequest?sign={value}"; + Upstreamrequest aGVToUpSystem = new Upstreamrequest() { agvCode = "123" }; + // string value = JsonConvert.SerializeObject(aGVToUpSystem); + // string url = $"http://192.168.1.40:8089/apicallback/quicktron/robotjob/upstreamrequest?sign={value}"; string url = $"http://192.168.1.40:8089/apicallback/quicktron/robotjob/upstreamrequest"; var res = APIHelper.GetInstance.HttpRequest(url, "", aGVToUpSystem, RequestType.POST); diff --git a/TestDemo/ReadClass.cs b/TestDemo/ReadClass.cs new file mode 100644 index 00000000..b2ae31f9 --- /dev/null +++ b/TestDemo/ReadClass.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BPASmartClient.S7Net; + +namespace TestDemo +{ + internal class ReadClass : Attribute + { + [Siemens(8)] + public string temp1 { get; set; } + + [Siemens(8)] + public string temp2 { get; set; } + } +} diff --git a/TestDemo/TestDemo.csproj b/TestDemo/TestDemo.csproj index 48b0191a..b2cc2406 100644 --- a/TestDemo/TestDemo.csproj +++ b/TestDemo/TestDemo.csproj @@ -13,6 +13,7 @@ + \ No newline at end of file