Files
cms_thermo_active/Step.NC/NcHandler.cs
T
CMS3762\carminatini 8650e84204 Siemens Bugs Fixed
2017-12-21 17:08:36 +01:00

183 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using static Step.Utils.Constants;
using static Step.Config.StartupConfig;
using CMS_CORE;
using CMS_CORE.Demo;
using CMS_CORE.Fanuc;
using CMS_CORE.Siemens;
using CMS_CORE.Osai;
using Step.Model.DTOModels;
using System.Globalization;
using static Step.Utils.ExceptionManager;
namespace Step.NC
{
public class NcHandler : IDisposable
{
public Nc numericalControl;
public NcHandler()
{
// Choose NC
numericalControl = SetNumericalControl();
}
public void Connect()
{
// Connect NC
if (!numericalControl.NC_IsConnected())
numericalControl.NC_Connect();
}
public Nc SetNumericalControl()
{
// Return new Numerical control instance choosed from the configuration
switch ((NC_VENDOR)NcConfig.NcVendor)
{
case NC_VENDOR.DEMO:
return new Nc_Demo(NcConfig.NcIpAddress, NcConfig.NcPort);
case NC_VENDOR.FANUC:
return new Nc_Fanuc(NcConfig.NcIpAddress, NcConfig.NcPort, 2000);
case NC_VENDOR.SIEMENS:
return new Nc_Siemens(2000);
case NC_VENDOR.OSAI:
return new Nc_Osai(NcConfig.NcIpAddress, NcConfig.NcPort, 2);
}
return null;
}
public DTONcGenericDataModel GetNcGenericData()
{
DTONcGenericDataModel genericData = new DTONcGenericDataModel();
// Get date time
DateTime dateTime = new DateTime();
numericalControl.NC_RDateTime(ref dateTime);
genericData.DateTime = dateTime;
// Get language
CultureInfo lang = new CultureInfo("en");
numericalControl.NC_RLanguage(ref lang);
genericData.Language = lang;
string tmpInfo = "";
// Get serial number
numericalControl.NC_RSerialNumber(ref tmpInfo);
genericData.SerialNumber = tmpInfo;
// Get software version
numericalControl.NC_RSoftwareVersion(ref tmpInfo);
genericData.SoftwareVersion = tmpInfo;
// Get model name
numericalControl.NC_RModelName(ref tmpInfo);
genericData.Model = tmpInfo;
// Get machine number
numericalControl.NC_RMachineNumber(ref tmpInfo);
genericData.MachineNumber = tmpInfo;
// Get max process number
ushort procNum = 0;
numericalControl.NC_RProcessesNum(ref procNum);
genericData.ProcessNumber = procNum;
return genericData;
}
public DTOAlarmsModel GetNcAlarms()
{
DTOAlarmsModel alarms = new DTOAlarmsModel();
List<string> tmpAlarms = new List<string>();
// Read NC active alarms
numericalControl.NC_RActiveAlarms(ref tmpAlarms);
// Create response list from strings
foreach (string alarmMessage in tmpAlarms)
{
alarms.NcAlarms.Add(new GenericAlarmModel()
{
alarmMessage = alarmMessage,
});
}
// Get NC max process number
ushort maxProcNumber = 0;
numericalControl.NC_RProcessesNum(ref maxProcNumber);
// For each process
for (ushort i = 1; i <= maxProcNumber; i++)
{
// Get process active alarms
numericalControl.PROC_RActiveAlarms(i, ref tmpAlarms);
// Create response list from strings
foreach (string alarmMessage in tmpAlarms)
{
alarms.ProcessAlarms.Add(new ProcessAlarmModel()
{
alarmMessage = alarmMessage,
process = i
});
}
}
// Read PLC Active Messages
numericalControl.PLC_RActiveMessages(ref tmpAlarms);
// Formatting response list from strings
foreach (string alarmMessage in tmpAlarms)
{
alarms.PlcAlarms.Add(new GenericAlarmModel()
{
alarmMessage = alarmMessage,
});
}
return alarms;
}
public List<DTOAxesModel> GetAxesPositions()
{
List<DTOAxesModel> axes = new List<DTOAxesModel>();
// Get NC max process number
ushort maxProcNumber = 0;
numericalControl.NC_RProcessesNum(ref maxProcNumber);
// For each process
for (ushort i = 1; i <= maxProcNumber; i++)
{
DTOAxesModel axis = GetAxesPositionsByProcess(i);
axes.Add(axis);
}
return axes;
}
public DTOAxesModel GetAxesPositionsByProcess(ushort processNum)
{
DTOAxesModel axes = new DTOAxesModel();
numericalControl.AXES_RInterpPosition(processNum, ref axes.interpolated);
numericalControl.AXES_RMachinePosition(processNum, ref axes.machine);
numericalControl.AXES_RProgrPosition(processNum, ref axes.programmePos);
numericalControl.AXES_RDistanceToGo(processNum, ref axes.toGo);
//numericalControl.AXES_RFollowingError(1, ref axes.followingErr);
return axes;
}
public void Dispose()
{
numericalControl.NC_Disconnect();
}
}
}