wanshenmean
3 天以前 7278264f027d62664a0209699d0f66a22fd06a8e
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_Communicator
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:西门子PLC的数据类型
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Communicator
{
    /// <summary>
    /// 西门子PLC的数据类型
    /// </summary>
    public class SiemensDBDataType
    {
        #region <Const>
        /// <summary>
        /// 32位有符号整型
        /// </summary>
        public const string DataType_DInt = "dint";
 
        /// <summary>
        /// 布尔
        /// </summary>
        public const string DataType_Bool = "bool";
 
        /// <summary>
        /// 字符串
        /// </summary>
        public const string DataType_String = "string";
 
        /// <summary>
        /// 16位有符号整型
        /// </summary>
        public const string DataType_Int = "int";
 
        /// <summary>
        /// 字节
        /// </summary>
        public const string DataType_Byte = "byte";
 
        /// <summary>
        /// 32位无符号整型
        /// </summary>
        public const string DataType_DW = "dw";
 
        /// <summary>
        /// 16位无符号整型
        /// </summary>
        public const string DataType_W = "w";
 
        /// <summary>
        /// 浮点型
        /// </summary>
        public const string DataType_Float = "float";
 
        /// <summary>
        /// 字符
        /// </summary>
        public const string DataType_Char = "char";
 
        /// <summary>
        /// 16位无符号整型
        /// </summary>
        public const string DataType_UInt = "uint";
 
        /// <summary>
        /// 32位无符号整型
        /// </summary>
        public const string DataType_UDInt = "udint";
 
        /// <summary>
        /// 字节数组
        /// </summary>
        public const string DataType_ByteArray = "byte[]";
        #endregion <Const>
 
        /// <summary>
        /// 根据西门子PLC的数据类型获取对应C#的类型编号枚举
        /// </summary>
        /// <param name="dataType">西门子PLC的数据类型</param>
        /// <returns>返回对应C#的类型编号枚举</returns>
        /// <exception cref="CommunicationException"></exception>
        public static TypeCode GetTypeCode(string dataType)
        {
            return dataType.ToLower() switch
            {
                DataType_DInt => TypeCode.Int32,
                DataType_DW => TypeCode.UInt32,
                DataType_Int => TypeCode.Int16,
                DataType_W => TypeCode.UInt16,
                DataType_Float => TypeCode.Single,
                DataType_Bool => TypeCode.Boolean,
                DataType_Byte => TypeCode.Byte,
                DataType_String => TypeCode.String,
                DataType_Char => TypeCode.Char,
                DataType_UInt => TypeCode.UInt16,
                DataType_UDInt => TypeCode.UInt32,
                _ => throw new CommunicationException($"数据类型错误:【{dataType}】", CommunicationErrorType.TypeError),
            };
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        /// <exception cref="CommunicationException"></exception>
        public static int GetTypeLength(string dataType)
        {
            return dataType.ToLower() switch
            {
                DataType_DInt => 4,
                DataType_DW => 4,
                DataType_Int => 2,
                DataType_W => 2,
                DataType_Float => 4,
                DataType_Bool => 1,
                DataType_Byte => 1,
                DataType_Char => 1,
                DataType_UInt => 2,
                DataType_UDInt => 4,
                _ => throw new CommunicationException($"数据类型错误:【{dataType}】", CommunicationErrorType.TypeError),
            };
        }
    }
}