using iWareCommon.Common.Globle;
|
using iWareCommon.Utils;
|
using iWareModel;
|
using System;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
|
namespace iWareSda.Common
|
{
|
public static class SdaHelper
|
{
|
|
/// <summary>
|
/// 根据DB块地址 赋值DB块的实际值
|
/// </summary>
|
/// <typeparam name="T1">地址对象</typeparam>
|
/// <typeparam name="T2">目标对象</typeparam>
|
/// <param name="t1">地址对象</param>
|
/// <param name="t2">目标对象</param>
|
/// <param name="plcService">PLC服务对象</param>
|
/// <param name="dbHeader">地址头的字符串,如果是西门子的PLC,要为空,罗克韦尔的要有</param>
|
/// <param name="isAllenBradley">是否是罗克韦尔的PLC</param>
|
public static void SetPropertyValueForDB<T1, T2>(T1 t1, T2 t2, PLCService plcService, string dbHeader, bool isAllenBradley = false)
|
{
|
string proValues = "";
|
try
|
{
|
//this.R_HandShake = Convert.ToInt16(this.S7.ReadValuePoint(r_dbHeader, r_dbBlock.R_HandShake, typeof(short)));//例子
|
//遍历T2里面的属性
|
PropertyInfo[] t2_pros = typeof(T2).GetProperties();
|
//遍历T1里面的属性
|
PropertyInfo[] t1_pros = typeof(T1).GetProperties();
|
var dbAddressValue = "";//获取DB块的地址
|
PropertyInfo t2Pro;
|
string[] arr = new string[2];//属性值
|
foreach (var t1_pro in t1_pros)
|
{
|
//获取该属性的值
|
if (isAllenBradley)
|
{//如果是罗克韦尔的PLC,地址就是他的名字
|
dbAddressValue = t1_pro.Name;
|
}
|
else
|
{
|
proValues = t1_pro.GetValue(t1, null).ToString();
|
arr = proValues.Split(WareSdaStruct.PLCDBADDRESS_SEPARATE);
|
if (arr.Length != 2)
|
{
|
continue;//继续下一个循环
|
}
|
dbHeader = arr[0];
|
dbAddressValue = arr[1];
|
}
|
if (!string.IsNullOrEmpty(dbAddressValue))
|
{//当地址不为空的时候才去获取值
|
t2Pro = t2_pros.First(x => x.Name == t1_pro.Name);
|
if (t2Pro.PropertyType == typeof(short))
|
{
|
t2Pro.SetValue(t2, Convert.ToInt16(plcService.ReadValuePoint(dbHeader, dbAddressValue, typeof(short))));
|
}
|
else if (t2Pro.PropertyType == typeof(int))
|
{
|
t2Pro.SetValue(t2, Convert.ToInt32(plcService.ReadValuePoint(dbHeader, dbAddressValue, typeof(int))));
|
}
|
else if (t2Pro.PropertyType == typeof(bool))
|
{
|
t2Pro.SetValue(t2, Convert.ToBoolean(plcService.ReadValuePoint(dbHeader, dbAddressValue, typeof(bool))));
|
}
|
else if (t2Pro.PropertyType == typeof(float))
|
{
|
float aa = Convert.ToSingle(plcService.ReadValuePoint(dbHeader, dbAddressValue, typeof(float)));
|
t2Pro.SetValue(t2, aa);
|
}
|
else if (t2Pro.PropertyType == typeof(string))
|
{//增加stirng支持 【EditBy shaocx,2022-04-22】
|
object aa = plcService.ReadValuePoint(dbHeader, dbAddressValue, typeof(string));
|
t2Pro.SetValue(t2, aa == null ? "" : aa.ToString());
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
var bb = proValues;
|
throw ex;
|
}
|
}
|
|
|
/// <summary>
|
/// 获取实时的PLC值,并转为字符串
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="TWrite"></typeparam>
|
/// <typeparam name="TRead"></typeparam>
|
/// <param name="t"></param>
|
/// <param name="t_write"></param>
|
/// <param name="t_read"></param>
|
/// <param name="header_write">西门子PLC可以为空</param>
|
/// <param name="header_read">西门子PLC可以为空</param>
|
/// <param name="deviceId"></param>
|
/// <param name="deviceName"></param>
|
/// <returns></returns>
|
public static string GetStrShow<T, TWrite, TRead>(T t, TWrite t_write, TRead t_read, string header_write, string header_read, int deviceId, string deviceName)
|
{
|
|
var pros = ClassHelper.GetPropertieModels<T>(t);
|
StringBuilder sb = new StringBuilder();
|
sb.Append(string.Format("===================================设备号{0},设备名{1}=================================== \r\n", deviceId, deviceName));
|
var pros_writes = ClassHelper.GetPropertieModels<TWrite>(t_write);
|
var pros_reads = ClassHelper.GetPropertieModels<TRead>(t_read);
|
PropertieModel pros_read = null;
|
PropertieModel pros_write = null;
|
|
string dbAddress = "";
|
foreach (var item in pros)
|
{
|
dbAddress = "";
|
//获取 TWrite和TRead的对象
|
pros_read = pros_reads.Where(x => x.PropertyName == item.PropertyName).FirstOrDefault();
|
if (pros_read != null)
|
{
|
if (string.IsNullOrEmpty(header_read))
|
{
|
dbAddress = pros_read.DataValue;
|
}
|
else
|
{
|
dbAddress = header_read + "___" + pros_read.DataValue;
|
}
|
}
|
else
|
{
|
pros_write = pros_writes.Where(x => x.PropertyName == item.PropertyName).FirstOrDefault();
|
if (pros_write != null)
|
{
|
if (string.IsNullOrEmpty(header_write))
|
{
|
dbAddress = pros_write.DataValue;
|
}
|
else
|
{
|
dbAddress = header_write + "___" + pros_write.DataValue;
|
}
|
}
|
}
|
|
sb.Append(string.Format("{0}:{1} address:{2} value:{3} \r\n", item.DescriptionName, item.PropertyName, dbAddress, item.DataValue));
|
}
|
return sb.ToString();
|
}
|
|
|
|
/// <summary>
|
/// 获取PLC的库存信息
|
/// </summary>
|
/// <param name="header_write"></param>
|
/// <param name="S7"></param>
|
/// <returns></returns>
|
public static string GetStrShowForStock(string header_write, PLCService S7)
|
{
|
StringBuilder sb = new StringBuilder();
|
string dbAddress = "";
|
short value = 0;
|
foreach (var item in SysGloble.offsetPlaceNoDict)
|
{
|
dbAddress = "";
|
value = 0;
|
dbAddress = header_write + "___" + item.Value;
|
//读取他的PLC值
|
value = Convert.ToInt16(S7.ReadValuePoint(header_write, item.Value.ToString(), typeof(short)));
|
sb.Append(string.Format("{0} : {1} address:{2}\r\n", item.Key, value, dbAddress));
|
}
|
return sb.ToString();
|
}
|
|
}
|
}
|