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
{
///
/// 实例化单个PLCService
///
///
///
///
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;
}
}
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;
}
}
}
}