初始化上传
This commit is contained in:
47
常用工具集/Utility/Network/S7netplus/ORM/S7Client.cs
Normal file
47
常用工具集/Utility/Network/S7netplus/ORM/S7Client.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using S7.Net;
|
||||
|
||||
namespace MES.Utility.Network.S7netplus.ORM
|
||||
{
|
||||
public class S7Client : IDisposable
|
||||
{
|
||||
public S7Helper Plc;
|
||||
public bool IsConnected;
|
||||
public S7Client(CpuType cpu, string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
Plc = new S7Helper(cpu, ip);
|
||||
IsConnected = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsConnected = false;
|
||||
}
|
||||
}
|
||||
|
||||
public S7Read<T> Readable<T>() where T : class, new()
|
||||
{
|
||||
return new S7Read<T>(this);
|
||||
}
|
||||
|
||||
public S7Write<T> Writeable<T>(T entity) where T : class, new()
|
||||
{
|
||||
return new S7Write<T>(this, entity);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Plc != null)
|
||||
{
|
||||
Plc.Dispose();
|
||||
Plc = null;
|
||||
}
|
||||
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
19
常用工具集/Utility/Network/S7netplus/ORM/S7NodeType.cs
Normal file
19
常用工具集/Utility/Network/S7netplus/ORM/S7NodeType.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MES.Utility.Network.S7netplus.ORM
|
||||
{
|
||||
public enum S7NodeType
|
||||
{
|
||||
[Description("可读可写")]
|
||||
ReadAndWrite,
|
||||
[Description("只读")]
|
||||
ReadOnly,
|
||||
[Description("只写")]
|
||||
WriteOnly
|
||||
}
|
||||
}
|
||||
29
常用工具集/Utility/Network/S7netplus/ORM/S7PLCAttribute.cs
Normal file
29
常用工具集/Utility/Network/S7netplus/ORM/S7PLCAttribute.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MES.Utility.Network.S7netplus.ORM;
|
||||
|
||||
namespace MES.Utility.Network.S7netplus.ORM
|
||||
{
|
||||
public class S7PLCAttribute : Attribute
|
||||
{
|
||||
public string Address { get; set; }
|
||||
public int StrLength { get; set; }
|
||||
public S7NodeType Type { get; set; }
|
||||
public S7PLCAttribute() { }
|
||||
|
||||
public S7PLCAttribute(string address, S7NodeType type)
|
||||
{
|
||||
this.Address = address;
|
||||
this.Type = type;
|
||||
}
|
||||
public S7PLCAttribute(string address, int strLength, S7NodeType type)
|
||||
{
|
||||
this.Address = address;
|
||||
this.StrLength = strLength;
|
||||
this.Type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
187
常用工具集/Utility/Network/S7netplus/ORM/S7Read.cs
Normal file
187
常用工具集/Utility/Network/S7netplus/ORM/S7Read.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MES.Utility.Network.S7netplus.ORM
|
||||
{
|
||||
public class S7Read<T> where T : class, new()
|
||||
{
|
||||
private S7Client opcClient;
|
||||
private Dictionary<string, PropertyInfo> fieldDict;
|
||||
private Dictionary<string, int> stringLengthDict;
|
||||
/// <summary>
|
||||
/// 构造方法,传入参数
|
||||
/// </summary>
|
||||
/// <param name="opcClient"></param>
|
||||
public S7Read(S7Client opcClient)
|
||||
{
|
||||
this.opcClient = opcClient;
|
||||
//默认取出所有的只读或者可读可写的属性
|
||||
GetNodeType(out fieldDict, out stringLengthDict);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获得所有具有可读属性的数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void GetNodeType(out Dictionary<string, PropertyInfo> fieldDict, out Dictionary<string, int> stringLengthDict)
|
||||
{
|
||||
fieldDict = new Dictionary<string, PropertyInfo>();
|
||||
stringLengthDict = new Dictionary<string, int>();
|
||||
|
||||
//获得对象T的属性
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
//得到所有添加注解的属性
|
||||
foreach (PropertyInfo info in properties)
|
||||
{
|
||||
//判断是否具有HttpController属性
|
||||
Object[] attr = info.GetCustomAttributes(false);
|
||||
if (attr.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//从注解数组中取第一个注解(一个属性可以包含多个注解)
|
||||
S7PLCAttribute myattr = attr[0] as S7PLCAttribute;
|
||||
if (myattr == null)
|
||||
continue;
|
||||
if (myattr.Type == S7NodeType.ReadAndWrite || myattr.Type == S7NodeType.ReadOnly)
|
||||
{
|
||||
fieldDict.Add(myattr.Address, info);
|
||||
stringLengthDict.Add(myattr.Address, myattr.StrLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行获得数据,映射成对象
|
||||
/// </summary>
|
||||
/// <param name="message">返回错误信息</param>
|
||||
/// <returns></returns>
|
||||
public bool Execute(out T t)
|
||||
{
|
||||
|
||||
if (fieldDict.Count == 0)
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (!opcClient.IsConnected)
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
bool flag = false;
|
||||
T entity = (T)Activator.CreateInstance(typeof(T));
|
||||
//循环读取每一个
|
||||
foreach (KeyValuePair<string, PropertyInfo> keyValue in fieldDict)
|
||||
{
|
||||
//判断当前keyValue是什么类型
|
||||
if (keyValue.Value.PropertyType == typeof(ushort))
|
||||
{
|
||||
ushort value=0;
|
||||
flag = opcClient.Plc.ReadInt16(keyValue.Key, out value);
|
||||
if (!flag)
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
keyValue.Value.SetValue(entity, value);
|
||||
}
|
||||
else if (keyValue.Value.PropertyType == typeof(int))
|
||||
{
|
||||
ushort value =0;
|
||||
flag = opcClient.Plc.ReadInt16(keyValue.Key, out value);
|
||||
if (!flag)
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
keyValue.Value.SetValue(entity, value);
|
||||
}
|
||||
//else if (keyValue.Value.PropertyType == typeof(float))
|
||||
//{
|
||||
// float value;
|
||||
// flag = opcClient.Plc.ReadReal(keyValue.Key, out value);
|
||||
// if (!flag)
|
||||
// {
|
||||
// t = default(T);
|
||||
// return false;
|
||||
// }
|
||||
// keyValue.Value.SetValue(entity, value);
|
||||
//}
|
||||
else if (keyValue.Value.PropertyType == typeof(string))
|
||||
{
|
||||
string value;
|
||||
flag = opcClient.Plc.ReadString(keyValue.Key, stringLengthDict[keyValue.Key], out value);
|
||||
if (!flag)
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
keyValue.Value.SetValue(entity, value);
|
||||
}
|
||||
}
|
||||
t = entity;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
t = default(T);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 传入Lambda表达式,指定读取的数据
|
||||
/// </summary>
|
||||
/// <param name="columns"></param>
|
||||
/// <returns></returns>
|
||||
public S7Read<T> ReadField(Expression<Func<T, object>> columns)
|
||||
{
|
||||
Dictionary<string, PropertyInfo> fieldDict = new Dictionary<string, PropertyInfo>();
|
||||
Dictionary<string, int> stringLengthDict = new Dictionary<string, int>();
|
||||
|
||||
//取出要读的属性
|
||||
string lambda = columns.Body.ToString();
|
||||
int index1 = lambda.IndexOf('(');
|
||||
int index2 = lambda.IndexOf(')');
|
||||
string str = lambda.Substring(index1 + 1, index2 - index1 - 1);
|
||||
List<string> keyList = str.Split(',').Select(it => it.Split('=')[0].Trim()).ToList();
|
||||
fieldDict.Clear();
|
||||
//获得对象T的属性
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
//得到所有添加注解的属性
|
||||
foreach (PropertyInfo info in properties)
|
||||
{
|
||||
//判断是否具有HttpController属性
|
||||
Object[] attr = info.GetCustomAttributes(false);
|
||||
if (attr.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//从注解数组中取第一个注解(一个属性可以包含多个注解)
|
||||
S7PLCAttribute myattr = attr[0] as S7PLCAttribute;
|
||||
if (myattr == null)
|
||||
continue;
|
||||
if (myattr.Type == S7NodeType.ReadAndWrite || myattr.Type == S7NodeType.ReadOnly)
|
||||
{
|
||||
if (keyList.Contains(info.Name))
|
||||
{
|
||||
fieldDict.Add(myattr.Address, info);
|
||||
stringLengthDict.Add(myattr.Address, myattr.StrLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.fieldDict = fieldDict;
|
||||
this.stringLengthDict = stringLengthDict;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
176
常用工具集/Utility/Network/S7netplus/ORM/S7Write.cs
Normal file
176
常用工具集/Utility/Network/S7netplus/ORM/S7Write.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MES.Utility.Network.S7netplus.ORM
|
||||
{
|
||||
public class S7Write<T> where T : class, new()
|
||||
{
|
||||
private S7Client opcClient;
|
||||
private T entity;
|
||||
private Dictionary<string, PropertyInfo> dict;
|
||||
private Dictionary<string, int> strLengthDict;
|
||||
|
||||
/// <summary>
|
||||
/// 构造方法,传入参数
|
||||
/// </summary>
|
||||
/// <param name="opcClient"></param>
|
||||
/// <param name="entity"></param>
|
||||
public S7Write(S7Client opcClient, T entity)
|
||||
{
|
||||
this.opcClient = opcClient;
|
||||
this.entity = entity;
|
||||
//默认取出所有的只写或者可读可写的属性
|
||||
GetAllKeyList(out dict, out strLengthDict);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获得所有具有可写属性的数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void GetAllKeyList(out Dictionary<string, PropertyInfo> dict, out Dictionary<string, int> strLengthDict)
|
||||
{
|
||||
dict = new Dictionary<string, PropertyInfo>();
|
||||
strLengthDict = new Dictionary<string, int>();
|
||||
|
||||
|
||||
//获取entity对象的所有属性
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
//得到所有添加注解的属性
|
||||
foreach (PropertyInfo info in properties)
|
||||
{
|
||||
//判断是否具有HttpController属性
|
||||
object[] attr = info.GetCustomAttributes(false);
|
||||
if (attr.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//从注解数组中取第一个注解(一个属性可以包含多个注解)
|
||||
S7PLCAttribute myattr = attr[0] as S7PLCAttribute;
|
||||
if (myattr == null)
|
||||
continue;
|
||||
if (myattr.Type == S7NodeType.ReadAndWrite || myattr.Type == S7NodeType.WriteOnly)
|
||||
{
|
||||
dict.Add(myattr.Address, info);
|
||||
strLengthDict.Add(myattr.Address, myattr.StrLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行写入数据,将对象中的数据写入到OPC服务中
|
||||
/// </summary>
|
||||
/// <param name="message">返回写入过程中出现的错误信息</param>
|
||||
/// <returns></returns>
|
||||
public bool Execute()
|
||||
{
|
||||
if (dict.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!opcClient.IsConnected)
|
||||
return false;
|
||||
//循环写入
|
||||
try
|
||||
{
|
||||
bool flag;
|
||||
foreach (KeyValuePair<string, PropertyInfo> keyValue in dict)
|
||||
{
|
||||
//判断当前keyValue是什么类型
|
||||
if (keyValue.Value.PropertyType == typeof(ushort))
|
||||
{
|
||||
flag = opcClient.Plc.WriteInt16(keyValue.Key, (ushort)keyValue.Value.GetValue(entity));
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (keyValue.Value.PropertyType == typeof(int))
|
||||
{
|
||||
flag = opcClient.Plc.WriteInt32(keyValue.Key, (int)keyValue.Value.GetValue(entity));
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (keyValue.Value.PropertyType == typeof(float))
|
||||
{
|
||||
flag = opcClient.Plc.WriteSingle(keyValue.Key, (float)keyValue.Value.GetValue(entity));
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (keyValue.Value.PropertyType == typeof(string))
|
||||
{
|
||||
flag = opcClient.Plc.WriteString(keyValue.Key, (string)keyValue.Value.GetValue(entity), strLengthDict[keyValue.Key]);
|
||||
if (!flag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 传入Lambda表达式,指定写入的数据
|
||||
/// </summary>
|
||||
/// <param name="columns"></param>
|
||||
/// <returns></returns>
|
||||
public S7Write<T> WriteField(Expression<Func<T, object>> columns)
|
||||
{
|
||||
Dictionary<string, PropertyInfo> dict = new Dictionary<string, PropertyInfo>();
|
||||
Dictionary<string, int> strLengthDict = new Dictionary<string, int>();
|
||||
|
||||
|
||||
string lambda = columns.Body.ToString();
|
||||
int index1 = lambda.IndexOf('(');
|
||||
int index2 = lambda.IndexOf(')');
|
||||
string str = lambda.Substring(index1 + 1, index2 - index1 - 1);
|
||||
List<string> keyList = str.Split(',').Select(it => it.Split('=')[0].Trim()).ToList();
|
||||
|
||||
//获取entity对象的所有属性
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
//得到所有添加注解的属性
|
||||
foreach (PropertyInfo info in properties)
|
||||
{
|
||||
//判断是否具有HttpController属性
|
||||
Object[] attr = info.GetCustomAttributes(false);
|
||||
if (attr.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
//从注解数组中取第一个注解(一个属性可以包含多个注解)
|
||||
S7PLCAttribute myattr = attr[0] as S7PLCAttribute;
|
||||
if (myattr == null)
|
||||
continue;
|
||||
if (myattr.Type == S7NodeType.ReadAndWrite || myattr.Type == S7NodeType.WriteOnly)
|
||||
{
|
||||
string name = info.Name;
|
||||
if (keyList.Contains(name))
|
||||
{
|
||||
dict.Add(myattr.Address, info);
|
||||
strLengthDict.Add(myattr.Address, myattr.StrLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.dict = dict;
|
||||
this.strLengthDict = strLengthDict;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user