分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-20 d2cef0150abe6d14cbfa60c8845795b91a1f5f97
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
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Entity.DomainModels;
 
namespace WIDESEA_WCS.WCSClient
{
    public class DBExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="detail">设备信息地址</param>
        /// <param name="client"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static object Read(dt_plcinfodetail detail, PLCClient client)
        {
 
            if (detail.plcdetail_valtype == typeof(int).Name.ToLower())//4字节,有符号类型
            {
                return (client.Read<int>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(uint).Name.ToLower())//4字节,无符号类型
            {
                return (client.Read<uint>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(short).Name.ToLower())//2字节,有符号类型,最常用
            {
                return (client.Read<short>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(ushort).Name.ToLower())//2字节,无符号类型
            {
                return (client.Read<ushort>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(float).Name.ToLower())//浮点型
            {
                return (client.Read<float>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(bool).Name.ToLower())
            {
                return (client.Read<bool>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(byte).Name.ToLower())//字节
            {
                return (client.Read<byte>(detail.plcdetail_db + "." + detail.plcdetail_value));
            }
            else if (detail.plcdetail_valtype == typeof(string).Name.ToLower())//字符串
            {
                return (client.Read<string>(detail.plcdetail_db + "." + detail.plcdetail_value, detail.plcdetail_len.GetValueOrDefault()));
            }
            else
            {
                throw new Exception($"【{detail.plcdetail_remark}】,DB地址{detail.plcdetail_db + "." + detail.plcdetail_value},未定义数据类型{detail.plcdetail_valtype}");
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="detail"></param>
        /// <param name="client"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Read(DBItemGroup itemGroup, PLCClient client)
        {
            //PLCClient client = PLCClient.Clients.Where(x => x.PLCName == itemGroup.name).FirstOrDefault();
            if (client == null)
            {
                throw new Exception($"未找到{itemGroup.name}连接对象");
            }
            else
            {
                if (itemGroup.dataType == typeof(int).Name.ToLower())//4字节,有符号类型
                {
                    return (client.Read<int>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(uint).Name.ToLower())//4字节,无符号类型
                {
                    return (client.Read<uint>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(short).Name.ToLower())//2字节,有符号类型,最常用
                {
                    return (client.Read<short>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(ushort).Name.ToLower())//2字节,无符号类型
                {
                    return (client.Read<ushort>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(float).Name.ToLower())//浮点型
                {
                    return (client.Read<float>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(bool).Name.ToLower())
                {
                    return (client.Read<bool>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(byte).Name.ToLower())//字节
                {
                    return (client.Read<byte>(itemGroup.dbAddress));
                }
                else if (itemGroup.dataType == typeof(string).Name.ToLower())//字符串
                {
                    return (client.Read<string>(itemGroup.dbAddress, itemGroup.dataLen.GetValueOrDefault()));
                }
                else
                {
                    throw new Exception($"【{itemGroup.name}】,DB地址{itemGroup.dbAddress},未定义数据类型{itemGroup.dataType}");
                }
            }
        }
    }
}