1
wankeda
2025-04-11 a206dda817a9f9615db1fa52e3ac193af9ac4a92
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using HslCommunication;
using HslCommunication.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Communicator
{
    public class SiemensCustomModel : IDataTransfer
    {
        #region <Const>
        #endregion <Const>
 
        #region <Private Member>
        public ushort ReadCount { get; }
 
        private IByteTransform byteTransform = new ReverseBytesTransform();
        #endregion <Private Member>
 
        #region <Public Menber>
 
        #endregion <Public Menber>
 
        #region <Constructor function>
        public SiemensCustomModel()
        {
            ushort readCount = 0;
            PropertyInfo[] propertyInfos = GetType().GetProperties().Where(x => x.CanWrite).ToArray();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                object? obj = propertyInfo.GetValue(this);
                TypeCode typeCode = Type.GetTypeCode(propertyInfo.PropertyType);
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Char:
                        readCount += sizeof(byte);
                        break;
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                        readCount += 2;
                        break;
                    case TypeCode.Single:
                    case TypeCode.Int32:
                    case TypeCode.UInt32:
                        readCount += 4;
                        break;
                    case TypeCode.String:
                        ushort dataLength = CheckStringAttribute(propertyInfo);
                        readCount += dataLength;
                        break;
                    default:
                        throw new NotSupportedException("不支持的基础类型: " + propertyInfo.PropertyType);
                }
            }
            ReadCount = readCount;
        }
        #endregion <Constructor function>
 
        #region <Private Method>
        private ushort CheckStringAttribute(PropertyInfo propertyInfo)
        {
            Attribute? attribute = propertyInfo.GetCustomAttribute(typeof(DataLengthAttribute));
            if (attribute == null)
            {
                throw new Exception($"字符串需要配置【DataLength】特性");
            }
            ushort dataLength = ((DataLengthAttribute)attribute).DataLength;
            if (dataLength <= 0 || dataLength > 256)
            {
                throw new Exception($"【DataLength】特性请配置有效参数,参数范围【1-256】");
            }
            if (dataLength % 2 != 0)
            {
                dataLength += 1;
            }
            dataLength += 2;
 
            return dataLength;
        }
        #endregion <Private Method>
 
        #region <Public Method>
        public void ParseSource(byte[] Content)
        {
            PropertyInfo[] propertyInfos = GetType().GetProperties().Where(x => x.CanWrite).ToArray();
            int index = 0;
            for (int i = 0; i < propertyInfos.Length; i++)
            {
                PropertyInfo propertyInfo = propertyInfos[i];
 
                TypeCode typeCode = Type.GetTypeCode(propertyInfo.PropertyType);
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                        propertyInfo.SetValue(this, Content[index]);
                        index += sizeof(byte);
                        break;
                    case TypeCode.Char:
                        propertyInfo.SetValue(this, (char)Content[index]);
                        index += sizeof(char);
                        break;
                    case TypeCode.Int16:
                        propertyInfo.SetValue(this, byteTransform.TransInt16(Content, index));
                        index += sizeof(short);
                        break;
                    case TypeCode.UInt16:
                        propertyInfo.SetValue(this, byteTransform.TransUInt16(Content, index));
                        index += sizeof(ushort);
                        break;
                    case TypeCode.Single:
                        propertyInfo.SetValue(this, byteTransform.TransSingle(Content, index));
                        index += sizeof(float);
                        break;
                    case TypeCode.Int32:
                        propertyInfo.SetValue(this, byteTransform.TransInt32(Content, index));
                        index += sizeof(int);
                        break;
                    case TypeCode.UInt32:
                        propertyInfo.SetValue(this, byteTransform.TransUInt32(Content, index));
                        index += sizeof(uint);
                        break;
                    case TypeCode.String:
                        ushort dataLength = CheckStringAttribute(propertyInfo);
                        propertyInfo.SetValue(this, Encoding.Default.GetString(Content, index + 2, Content[index + 1]));
                        index += dataLength;
                        break;
                    default:
                        throw new NotSupportedException("不支持的基础类型: " + propertyInfo.PropertyType);
                }
            }
        }
 
        public byte[] ToSource()
        {
            int propertyValueHasNull = -1;
            List<byte> bytes = new List<byte>();
            PropertyInfo[] propertyInfos = GetType().GetProperties().Where(x => x.CanWrite).ToArray();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                object? obj = propertyInfo.GetValue(this);
                if (obj == null && propertyValueHasNull != -1)
                {
                    throw new Exception($"{GetType().Name}错误");
                }
                else
                {
                    if (propertyValueHasNull == -1)
                        propertyValueHasNull = 2;
                }
                TypeCode typeCode = Type.GetTypeCode(propertyInfo.PropertyType);
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.SByte:
                    case TypeCode.Char:
                        if (obj != null)
                        {
                            bytes.Add((byte)obj);
                        }
                        else
                        { propertyValueHasNull = 1; }
                        break;
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                        if (obj != null)
                        {
                            byte[] bytesShort = BitConverter.GetBytes(Convert.ToUInt16(obj));
                            Array.Reverse(bytesShort);
                            bytes.AddRange(bytesShort);
                        }
                        else
                        { propertyValueHasNull = 1; }
                        break;
                    case TypeCode.Single:
                        if (obj != null)
                        {
                            byte[] bytesSingle = BitConverter.GetBytes(Convert.ToSingle(obj));
                            Array.Reverse(bytesSingle);
                            bytes.AddRange(bytesSingle);
                        }
                        else
                        { propertyValueHasNull = 1; }
                        break;
                    case TypeCode.Int32:
                    case TypeCode.UInt32:
                        if (obj != null)
                        {
                            byte[] bytesInt = BitConverter.GetBytes(Convert.ToUInt32(obj));
                            Array.Reverse(bytesInt);
                            bytes.AddRange(bytesInt);
                        }
                        else
                        { propertyValueHasNull = 1; }
                        break;
                    case TypeCode.String:
 
                        if (obj != null)
                        {
                            ushort dataLength = CheckStringAttribute(propertyInfo);
                            byte[] bytesString = Encoding.Default.GetBytes(obj.ToString());
                            bytes.Add(Convert.ToByte(dataLength));
                            bytes.Add(Convert.ToByte(obj.ToString().Length));
                            bytes.AddRange(bytesString);
                            for (int i = 0; i < dataLength - obj.ToString().Length - 2; i++)
                            {
                                bytes.Add(0);
                            }
                        }
                        else
                        { propertyValueHasNull = 1; }
                        break;
                    default:
                        throw new NotSupportedException("不支持的基础类型: " + propertyInfo.PropertyType);
                }
            }
 
            return bytes.ToArray();
        }
        #endregion <Public Method>
 
        #region <Event>
        #endregion <Event>
    }
}