schangxiang@126.com
2024-09-06 05f2a20bb792169bf7b8a101af8718b96449f55a
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
using HslCommunication.Profinet.Siemens;
using iWare_SCADA_BusinessLogical.Utils;
using iWare_SCADA_Model;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWare_SCADA_BusinessLogical
{
    public class PLCManger
    {
        /// <summary>
        ///  实例化单个PLCService
        /// </summary>
        /// <param name="_ip"></param>
        /// <param name="device"></param>
        /// <returns></returns>
        public static PLCService GetSinglePLCService(DataCaptureConfig dataCaptureConfig)
        {
            HslCommunicationParam hslCommunicationParam = new HslCommunicationParam()
            {
                cpu = (SiemensPLCS)dataCaptureConfig.DataCapturePLCType,
                ip = dataCaptureConfig.PLCIP,
                //rack = WareSdaStruct.Srm_Rack,
                //slot = WareSdaStruct.Srm_Slot,
                port = dataCaptureConfig.PLCPort.Value
            };
            PLCService PLCService = PLCFactory.GetOPCService(PLCServiceTypeEnum.Siemens_HslCommunicationService, hslCommunicationParam);
            return PLCService;
        }
        public static Type GetTypeForString(string datatype)
        {
            switch (datatype.ToLower())
            {
                case "int":
                    return typeof(Int16);
                case "dint":
                    return typeof(Int32);
                case "char":
                    return typeof(Char);
                case "real":
                    return typeof(double);
                //return typeof(Single);
                case "bool":
                    return typeof(bool);
                case "string":
                    return typeof(string);
                case "byte":
                    return typeof(Byte);
                case "other":
                    return typeof(string);
                default:
                    return typeof(string);
            }
        }
 
        public static object GetVauleForObjectType(object vaule, TypeCode type)
        {
 
            switch (type)
            {
                case TypeCode.Int16:
                    return Convert.ToInt16(vaule);
                case TypeCode.Int32:
                    return Convert.ToInt32(vaule);
                case TypeCode.Char:
                    return Convert.ToChar(vaule);
                case TypeCode.Single:
                    return Convert.ToSingle(vaule);
                case TypeCode.Boolean:
                    return Convert.ToBoolean(vaule);
                case TypeCode.String:
                    return Convert.ToString(vaule);
                case TypeCode.Byte:
                    return Convert.ToByte(vaule);
 
                default:
                    return Convert.ToString(vaule);
            }
        }
 
        public static LogType GetLogTypeForWorkingProcedure(string wp)
        {
            switch (wp)
            {
 
                case "OP05":
                    return LogType.PLCOP05;
                case "OP10":
                    return LogType.PLCOP10;
                case "OP20":
                    return LogType.PLCOP20;
                case "OP30":
                    return LogType.PLCOP30;
                case "OP35":
                    return LogType.PLCOP35;
                case "OP40":
                    return LogType.PLCOP40;
                case "OP50":
                    return LogType.PLCOP50;
                case "OP60":
                    return LogType.PLCOP60;
                case "OP70":
                    return LogType.PLCOP70;
                case "OP80":
                    return LogType.PLCOP80;
                case "HMI":
                    return LogType.PLCHMI;
                default:
                    return LogType.PLCService;
            }
        }
 
 
        /// <summary>
        /// 上线
        /// </summary>
        /// <param name="wp"></param>
        /// <returns></returns>
        public static LogType GetLogTypeForOnLine(string wp)
        {
            //强制转换
            try
            {
                LogType _logType = (LogType)Enum.Parse(typeof(LogType), wp);
                return _logType;
            }
            catch (Exception ex)
            {
                return LogType.OtherOnLine;
            }
        }
 
        public static string GetWorkPieceIDForPLC(DataCaptureConfig _dataCaptureConfig, PLCService plcService)
        {
            try
            {
                var valueid = plcService.ReadValuePoint(_dataCaptureConfig.DbNumber, _dataCaptureConfig.Offset, _dataCaptureConfig.DataCaptureColumnLength.Value, PLCManger.GetTypeForString(_dataCaptureConfig.DataCaptureColumnType));
                return valueid.ToString().Trim();
            }
            catch (Exception ex)
            {
                Log4NetHelper.WriteErrorLog(PLCManger.GetLogTypeForWorkingProcedure(_dataCaptureConfig.WorkingProcedure), $" {_dataCaptureConfig.WorkingProcedure}工序监控读码标记 读取工件码数据时异常:", ex);
                throw;
            }
        }
    }
}