schangxiang@126.com
2025-08-13 a97a624c1fb269a059f97629076433c46b4e8b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
                    {
                        if (t1_pro.GetValue(t1, null) == null)
                        {
                            proValues = "";
                        }
                        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();
        }
 
    }
}