Просмотр исходного кода

西门子通讯库修改

master
pry 2 лет назад
Родитель
Сommit
7ced2375b0
5 измененных файлов: 96 добавлений и 19 удалений
  1. +48
    -16
      BPASmartClient.S7Net/EntityClassResolution.cs
  2. +19
    -0
      BPASmartClient.S7Net/SiemensAttribute.cs
  3. +10
    -3
      TestDemo/Form1.cs
  4. +18
    -0
      TestDemo/ReadClass.cs
  5. +1
    -0
      TestDemo/TestDemo.csproj

+ 48
- 16
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<SiemensAttribute>();
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<SiemensAttribute>();
if (tempLen != null) len = tempLen.Len + 2;
}

byte[] sarray = new byte[len];
Array.Copy(bytes, (int)numBytes, sarray, 0, len);
List<byte> bytes1 = new List<byte>();
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<SiemensAttribute>();
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);
}
}



+ 19
- 0
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; }
}
}

+ 10
- 3
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<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);



+ 18
- 0
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; }
}
}

+ 1
- 0
TestDemo/TestDemo.csproj Просмотреть файл

@@ -13,6 +13,7 @@
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
<ProjectReference Include="..\BPASmartClient.Http\BPASmartClient.Http.csproj" />
<ProjectReference Include="..\BPASmartClient.HubHelper\BPASmartClient.HubHelper.csproj" />
<ProjectReference Include="..\BPASmartClient.S7Net\BPASmartClient.S7Net.csproj" />
</ItemGroup>

</Project>

Загрузка…
Отмена
Сохранить