Files
cms-core-active/CMS_CORE_Library/Fanuc/Nc_Fanuc.cs
T
2018-08-21 15:53:04 +00:00

2295 lines
86 KiB
C#

using CMS_CORE.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using static CMS_CORE_Library.DataStructures;
#pragma warning disable 1591
namespace CMS_CORE.Fanuc
{
public class Nc_Fanuc : Nc
{
private int TimeOut;
private Focas1 FocasEnv;
private List<ushort> nLibHandle;
private String Cnc_name;
private String Cnc_SftVersion;
private String Cnc_SeriesNum;
private short Cnc_NumPath;
private byte FanucLanguage;
private DateTime Last_Static_Read;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Contructor & global methods
/**
* <summary>
* Instantiate The NC-Fanuc Class
* </summary>
* <param name="IpAddress">Remote Ip-Address of the Nc</param>
* <param name="RemotePort">Remote Port of the Nc</param>
* <param name="ConnectionTimeOut">Send/Recieve timeout connection [mS]</param>
* */
public Nc_Fanuc(String IpAddress, ushort RemotePort, ushort ConnectionTimeOut)
{
//Set internal valiables
Connected = false;
Ip = IpAddress;
Port = RemotePort;
TimeOut = ConnectionTimeOut / 1000;
//Istantiate the Focas Library
FocasEnv = new Focas1();
nLibHandle = new List<ushort>();
}
//Connect Method
public override CmsError NC_Connect()
{
short nReturn;
short ActivePath;
ushort TempLibHandle;
short i;
//Try to connect to The NC
nReturn = Focas1.cnc_allclibhndl3(Ip, Port, TimeOut, out TempLibHandle);
if (nReturn != 0)
return GetNcError(nReturn);
else
nLibHandle.Add(TempLibHandle);
//Read the Actual Paths
nReturn = Focas1.cnc_getpath(nLibHandle[0], out ActivePath, out Cnc_NumPath);
if (nReturn != 0)
return GetNcError(nReturn);
//Setup all Paths
for (i = 0; i < Cnc_NumPath; i++)
{
if (i > 0)
{
nReturn = Focas1.cnc_allclibhndl3(Ip, Port, TimeOut, out TempLibHandle);
if (nReturn != 0)
return GetNcError(nReturn);
else
nLibHandle.Add(TempLibHandle);
}
nReturn = Focas1.cnc_setpath(nLibHandle[i], (short)(i + 1));
if (nReturn != 0)
return GetNcError(nReturn);
}
//Setup the "Connected" value to TRUE
Connected = true;
return NO_ERROR;
}
//Disconnect Method
public override CmsError NC_Disconnect()
{
if (Connected)
{
short nReturn;
//If is connected -> Disconncet
foreach (ushort handle in nLibHandle)
{
nReturn = Focas1.cnc_freelibhndl(handle);
}
//Setup the "Connected" value to FALSE
Connected = false;
}
return NO_ERROR;
}
public override CmsError NC_GetTranslatedPlcMessages(string language, ref Dictionary<int, string> messages)
{
return NO_ERROR;
}
#endregion Contructor & global methods
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region High level methods
//Get the NC Language
public override CmsError NC_RLanguage(ref CultureInfo Language)
{
// Read static data
CmsError cmsError = ReadStaticNCData();
if (cmsError.IsError())
return cmsError;
Language = ConvertToSTEPLanguage(FanucLanguage);
return NO_ERROR;
}
//Get the NC Serial Number
public override CmsError NC_RSerialNumber(ref string SN)
{
// Read static data
CmsError cmsError = ReadStaticNCData();
if (cmsError.IsError())
return cmsError;
SN = Cnc_SeriesNum;
return NO_ERROR;
}
//Get the NC Software Version
public override CmsError NC_RSoftwareVersion(ref string SWV)
{
// Read static data
CmsError cmsError = ReadStaticNCData();
if (cmsError.IsError())
return cmsError;
SWV = Cnc_SftVersion;
return NO_ERROR;
}
//Get the NC model Name
public override CmsError NC_RModelName(ref string ModelName)
{
// Read static data
CmsError cmsError = ReadStaticNCData();
if (cmsError.IsError())
return cmsError;
ModelName = Cnc_name;
return NO_ERROR;
}
//Get the processes-count configurated
public override CmsError NC_RProcessesNum(ref ushort ProcNumber)
{
//Read static data
CmsError cmsError = ReadStaticNCData();
if (cmsError.IsError())
return cmsError;
ProcNumber = (ushort)Cnc_NumPath;
return NO_ERROR;
}
//Get the NC Time
public override CmsError NC_RDateTime(ref DateTime ActualTime)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBTIMER TimerDate = new Focas1.IODBTIMER();
Focas1.IODBTIMER TimerTime = new Focas1.IODBTIMER();
short nReturn = 0;
//Setup the IODBTIMER
TimerDate.type = 0;
TimerTime.type = 1;
//Execute the method
nReturn = Focas1.cnc_gettimer(nLibHandle[0], TimerDate);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Execute the method
nReturn = Focas1.cnc_gettimer(nLibHandle[0], TimerTime);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Setup the out value
ActualTime = new DateTime(TimerDate.date.year, TimerDate.date.month, TimerDate.date.date, TimerTime.time.hour, TimerTime.time.minute, TimerTime.time.second);
return NO_ERROR;
}
//Get the Machine Number
public override CmsError NC_RMachineNumber(ref string MachNumber)
{
ushort Value = 0;
// Read word
CmsError cmsError = MEM_RWWord(R, UNDEF_PROC, MATR_MACCH_FANUC.MemType, MATR_MACCH_FANUC.Address, ref Value);
if (cmsError.IsError())
return cmsError;
MachNumber = Value.ToString();
return NO_ERROR;
}
//Get the NC Alarms
public override CmsError NC_RActiveAlarms(ref List<AlarmModel> alarms)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
alarms.Clear();
Focas1.ODBALMMSG2 Messg = new Focas1.ODBALMMSG2();
short nReturn = 0;
short count = FANUC_MAXMSGCNC;
// Get path index
short path = GetHandleIndexFromPath(1);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdalmmsg2(nLibHandle[path], -1, ref count, Messg);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Add Alarm in List
alarms.Clear();
if (count >= 1)
AddNcAlarmToList((ushort)Messg.msg1.alm_no, Messg.msg1.alm_msg.Substring(0, Messg.msg1.msg_len), DateTime.Now, alarms);
if (count >= 2)
AddNcAlarmToList((ushort)Messg.msg2.alm_no, Messg.msg2.alm_msg.Substring(0, Messg.msg2.msg_len), DateTime.Now, alarms);
if (count >= 3)
AddNcAlarmToList((ushort)Messg.msg3.alm_no, Messg.msg3.alm_msg.Substring(0, Messg.msg3.msg_len), DateTime.Now, alarms);
if (count >= 4)
AddNcAlarmToList((ushort)Messg.msg4.alm_no, Messg.msg4.alm_msg.Substring(0, Messg.msg4.msg_len), DateTime.Now, alarms);
if (count >= 5)
AddNcAlarmToList((ushort)Messg.msg5.alm_no, Messg.msg5.alm_msg.Substring(0, Messg.msg5.msg_len), DateTime.Now, alarms);
if (count >= 6)
AddNcAlarmToList((ushort)Messg.msg6.alm_no, Messg.msg6.alm_msg.Substring(0, Messg.msg6.msg_len), DateTime.Now, alarms);
if (count >= 7)
AddNcAlarmToList((ushort)Messg.msg7.alm_no, Messg.msg7.alm_msg.Substring(0, Messg.msg7.msg_len), DateTime.Now, alarms);
if (count >= 8)
AddNcAlarmToList((ushort)Messg.msg8.alm_no, Messg.msg8.alm_msg.Substring(0, Messg.msg8.msg_len), DateTime.Now, alarms);
if (count >= 9)
AddNcAlarmToList((ushort)Messg.msg9.alm_no, Messg.msg9.alm_msg.Substring(0, Messg.msg9.msg_len), DateTime.Now, alarms);
if (count >= 10)
AddNcAlarmToList((ushort)Messg.msg10.alm_no, Messg.msg10.alm_msg.Substring(0, Messg.msg10.msg_len), DateTime.Now, alarms);
return NO_ERROR;
}
// Check if the NC is in running state
public override CmsError NC_RIsRunning(ref bool running)
{
ushort nProcess = 0;
// Get the number of processes
CmsError cmsError = NC_RProcessesNum(ref nProcess);
if (cmsError.IsError())
return cmsError;
ushort i = 1;
running = false;
do
{
PROC_STATUS procStatus = PROC_STATUS.IDLE;
// Read process status
cmsError = PROC_RStatus(i, ref procStatus);
if (cmsError.IsError())
return cmsError;
if (procStatus == PROC_STATUS.RUN)
running = true;
i++;
} while (!running || i <= nProcess);
return NO_ERROR;
}
#endregion High level methods
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PLC High-level data
//Get the PMC Messages
public override CmsError PLC_RActiveMessages(ref List<PlcAlarmModel> alarms)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.OPMSG3 Messg = new Focas1.OPMSG3();
short nReturn = 0;
short count = FANUC_MAXMSGPMC;
//Execute the method
nReturn = Focas1.cnc_rdopmsg3(nLibHandle[0], -1, ref count, Messg);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
// Add Alarm in List
alarms.Clear();
if (count >= 1 && Messg.msg1.datano >= 0)
AddPlcAlarmsToList((ushort)Messg.msg1.datano, false, false, new List<int>() { 1 }, alarms);
if (count >= 2 && Messg.msg2.datano >= 0)
AddPlcAlarmsToList((ushort)Messg.msg2.datano, false, false, new List<int>() { 1 }, alarms);
if (count >= 3 && Messg.msg3.datano >= 0)
AddPlcAlarmsToList((ushort)Messg.msg3.datano, false, false, new List<int>() { 1 }, alarms);
if (count >= 4 && Messg.msg4.datano >= 0)
AddPlcAlarmsToList((ushort)Messg.msg4.datano, false, false, new List<int>() { 1 }, alarms);
if (count >= 5 && Messg.msg5.datano >= 0)
AddPlcAlarmsToList((ushort)Messg.msg5.datano, false, false, new List<int>() { 1 }, alarms);
return NO_ERROR;
}
public override CmsError PLC_WRefreshMessage(uint id)
{
return NO_ERROR;
}
public override CmsError PLC_WRestoreMessage(uint id)
{
return NO_ERROR;
}
public override CmsError PLC_WRefreshAllMessages()
{
return NO_ERROR;
}
// Get pre and post power on data
public override CmsError PLC_RPowerOnData(ref PreAndPostPowerOnModel powerOnModel)
{
return NO_ERROR;
}
// Write power on data
public override CmsError PLC_WPowerOnData(uint id, bool value)
{
return NO_ERROR;
}
// Get functionality data
public override CmsError PLC_RFunctionAccess(ref List<FunctionalityModel> functions)
{
functions = new List<FunctionalityModel>();
return NO_ERROR;
}
// Get axis reset procedure data
public override CmsError PLC_RAxesResetData(ref AxisResetDataModel axisResetData)
{
return NO_ERROR;
}
// Get all the machine counters
public override CmsError PLC_RMachineCounters(ref List<CounterModel> counters)
{
return NO_ERROR;
}
public override CmsError PLC_RNcSoftKeys(ref List<SoftKeysModel> ncSoftKeys)
{
return NO_ERROR;
}
public override CmsError PLC_RUserSoftKeys(ref List<SoftKeysModel> softKeys)
{
return NO_ERROR;
}
public override CmsError PLC_WNcSoftKey(uint id)
{
return NO_ERROR;
}
public override CmsError PLC_WUserSoftKey(uint id)
{
return NO_ERROR;
}
public override CmsError PLC_RHeadsData(List<HeadDataModel> heads, int number)
{
return NO_ERROR;
}
public override CmsError PLC_WHeadOverride(uint id, HEAD_OVERRIDE_SIGN sign)
{
return NO_ERROR;
}
#endregion PLC High-level data
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PROCESS (PATH) High-level data
// Get process part program data
public override CmsError PROC_RStatusAndData(ushort procNumber, ref ProcessDataModel processData)
{
//processData.Id = procNumber;
//// Read part program name
//CmsError cmsError = PROC_RPPName(procNumber, ref processData.PartProgramName);
//if (cmsError.IsError())
// return cmsError;
//ushort readValue = 0;
//// Read processes status from memory
//cmsError = MEM_RWWord(R, 0, MEMORY_ADDRESS.PRE_POWER_ON.MemType, MEMORY_ADDRESS.PROCESS_STATUS.Address + 2 * (procNumber - 1), ref readValue);
//if (cmsError.IsError())
// return cmsError;
//byte[] bytes = BitConverter.GetBytes(readValue);
//// Convert Byte into true/false array
//BitArray bits = new BitArray(new byte[] { bytes[0] }); //1st byte -> 0 : Proccess type (AUX-WORK), 1: Process in alarm status, 2: Run, 3: Hold, 4: Read, 5: PartProgramVisible
//// Populate response structure
//processData.Type = bits[0] ? "WORK" : "AUX";
//processData.IsInAlarm = bits[1];
//// Choose process status
//if (bits[2])
// processData.Status = "RUN";
//else
//{
// if (bits[3])
// processData.Status = "HOLD";
// else
// processData.Status = "READY";
//}
//// Get Reps from 2nd byte
//processData.Reps = bytes[1];
//// Get part program info visibility
//processData.Visible = bits[5];
return NO_ERROR;
}
//Get the process Alarms
public override CmsError PROC_RActiveAlarms(ushort ProcNumber, ref List<AlarmModel> Alarms)
{
Alarms.Clear();
return NO_ERROR;
}
//Get PP Lines
public override CmsError PROC_RPPLines(ushort ProcNumber, ref List<string> Lines)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn = 0;
int linN;
ushort Lenght = 256;
short ReturnIndex = 1;
Char[] Chars = new Char[255];
String[] newLines;
short path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdblkcount(nLibHandle[path], out linN);
ReturnIndex = (short)linN;
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdexecprog(nLibHandle[path], ref Lenght, out ReturnIndex, Chars);
newLines = new String(Chars).Replace("\0", String.Empty).Split('\n');
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Setup the return values
Lines.Clear();
foreach (String Line in newLines)
Lines.Add(Line);
return NO_ERROR;
}
//Get Active PP Name
public override CmsError PROC_RPPName(ushort ProcNumber, ref string Name)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn = 0;
Focas1.ODBEXEPRG Prg = new Focas1.ODBEXEPRG(); ;
// Get path index
short path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_exeprgname(nLibHandle[path], Prg);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Name = new String(Prg.name).Replace("\0", String.Empty);
return NO_ERROR;
}
//Get the process status
public override CmsError PROC_RStatus(ushort Number, ref PROC_STATUS Status)
{
Focas1.ODBST StatInfo = new Focas1.ODBST();
short nReturn;
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
// Get path index
short path = GetHandleIndexFromPath(Number);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_statinfo(nLibHandle[path], StatInfo);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Status = ConverToSTEPStatus(StatInfo);
return NO_ERROR;
}
//Get the process Mode
public override CmsError PROC_RMode(ushort Number, ref PROC_MODE Mode)
{
Focas1.ODBST StatInfo = new Focas1.ODBST();
short nReturn = 0;
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
// Get path index
short path = GetHandleIndexFromPath(Number);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_statinfo(nLibHandle[path], StatInfo);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Mode = ConverToSTEPMode(StatInfo);
return NO_ERROR;
}
public override CmsError PROC_RSelectedProcess(ref ushort procNumber)
{
procNumber = 1;
return NO_ERROR;
}
public override CmsError PROC_WSelectProcess(ushort procNumber)
{
return NO_ERROR;
}
#endregion PROCESS (PATH) High-level data
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region PROCESS-AXES (PATH) High-level data
//Get the Inrpolated position
public override CmsError AXES_RInterpPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.ODBPOS axisFan = new Focas1.ODBPOS();
short nReturn;
short i;
short NumAxes = Focas1.MAX_AXIS;
// Get path index
short path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdposition(nLibHandle[path], FANUC_RELATIVEPOS, ref NumAxes, axisFan);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//If an axis goes out from Process Clear the List
if (Axes.Count > NumAxes)
{
Axes.Clear();
}
//Fill all the List
for (i = 1; i <= NumAxes; i++)
{
switch (i)
{
case 1: ReadRelativeAxisPos(axisFan.p1.rel, ref Axes); break;
case 2: ReadRelativeAxisPos(axisFan.p2.rel, ref Axes); break;
case 3: ReadRelativeAxisPos(axisFan.p3.rel, ref Axes); break;
case 4: ReadRelativeAxisPos(axisFan.p4.rel, ref Axes); break;
case 5: ReadRelativeAxisPos(axisFan.p5.rel, ref Axes); break;
case 6: ReadRelativeAxisPos(axisFan.p6.rel, ref Axes); break;
case 7: ReadRelativeAxisPos(axisFan.p7.rel, ref Axes); break;
case 8: ReadRelativeAxisPos(axisFan.p8.rel, ref Axes); break;
case 9: ReadRelativeAxisPos(axisFan.p9.rel, ref Axes); break;
case 10: ReadRelativeAxisPos(axisFan.p10.rel, ref Axes); break;
case 11: ReadRelativeAxisPos(axisFan.p11.rel, ref Axes); break;
case 12: ReadRelativeAxisPos(axisFan.p12.rel, ref Axes); break;
case 13: ReadRelativeAxisPos(axisFan.p13.rel, ref Axes); break;
case 14: ReadRelativeAxisPos(axisFan.p14.rel, ref Axes); break;
case 15: ReadRelativeAxisPos(axisFan.p15.rel, ref Axes); break;
case 16: ReadRelativeAxisPos(axisFan.p16.rel, ref Axes); break;
case 17: ReadRelativeAxisPos(axisFan.p17.rel, ref Axes); break;
case 18: ReadRelativeAxisPos(axisFan.p18.rel, ref Axes); break;
case 19: ReadRelativeAxisPos(axisFan.p19.rel, ref Axes); break;
case 20: ReadRelativeAxisPos(axisFan.p20.rel, ref Axes); break;
case 21: ReadRelativeAxisPos(axisFan.p21.rel, ref Axes); break;
case 22: ReadRelativeAxisPos(axisFan.p22.rel, ref Axes); break;
case 23: ReadRelativeAxisPos(axisFan.p23.rel, ref Axes); break;
case 24: ReadRelativeAxisPos(axisFan.p24.rel, ref Axes); break;
case 25: ReadRelativeAxisPos(axisFan.p25.rel, ref Axes); break;
case 26: ReadRelativeAxisPos(axisFan.p26.rel, ref Axes); break;
case 27: ReadRelativeAxisPos(axisFan.p27.rel, ref Axes); break;
case 28: ReadRelativeAxisPos(axisFan.p28.rel, ref Axes); break;
case 29: ReadRelativeAxisPos(axisFan.p29.rel, ref Axes); break;
case 30: ReadRelativeAxisPos(axisFan.p30.rel, ref Axes); break;
case 31: ReadRelativeAxisPos(axisFan.p31.rel, ref Axes); break;
case 32: ReadRelativeAxisPos(axisFan.p32.rel, ref Axes); break;
}
}
return NO_ERROR;
}
//Get the Machine position
public override CmsError AXES_RMachinePosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.ODBPOS axisFan = new Focas1.ODBPOS();
short nReturn;
short i;
short NumAxes = Focas1.MAX_AXIS;
// Get path index
short path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdposition(nLibHandle[path], FANUC_MACHINEPOS, ref NumAxes, axisFan);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//If an axis goes out from Process Clear the List
if (Axes.Count > NumAxes)
{
Axes.Clear();
}
//Fill all the List
for (i = 1; i <= NumAxes; i++)
{
switch (i)
{
case 1: ReadRelativeAxisPos(axisFan.p1.mach, ref Axes); break;
case 2: ReadRelativeAxisPos(axisFan.p2.mach, ref Axes); break;
case 3: ReadRelativeAxisPos(axisFan.p3.mach, ref Axes); break;
case 4: ReadRelativeAxisPos(axisFan.p4.mach, ref Axes); break;
case 5: ReadRelativeAxisPos(axisFan.p5.mach, ref Axes); break;
case 6: ReadRelativeAxisPos(axisFan.p6.mach, ref Axes); break;
case 7: ReadRelativeAxisPos(axisFan.p7.mach, ref Axes); break;
case 8: ReadRelativeAxisPos(axisFan.p8.mach, ref Axes); break;
case 9: ReadRelativeAxisPos(axisFan.p9.mach, ref Axes); break;
case 10: ReadRelativeAxisPos(axisFan.p10.mach, ref Axes); break;
case 11: ReadRelativeAxisPos(axisFan.p11.mach, ref Axes); break;
case 12: ReadRelativeAxisPos(axisFan.p12.mach, ref Axes); break;
case 13: ReadRelativeAxisPos(axisFan.p13.mach, ref Axes); break;
case 14: ReadRelativeAxisPos(axisFan.p14.mach, ref Axes); break;
case 15: ReadRelativeAxisPos(axisFan.p15.mach, ref Axes); break;
case 16: ReadRelativeAxisPos(axisFan.p16.mach, ref Axes); break;
case 17: ReadRelativeAxisPos(axisFan.p17.mach, ref Axes); break;
case 18: ReadRelativeAxisPos(axisFan.p18.mach, ref Axes); break;
case 19: ReadRelativeAxisPos(axisFan.p19.mach, ref Axes); break;
case 20: ReadRelativeAxisPos(axisFan.p20.mach, ref Axes); break;
case 21: ReadRelativeAxisPos(axisFan.p21.mach, ref Axes); break;
case 22: ReadRelativeAxisPos(axisFan.p22.mach, ref Axes); break;
case 23: ReadRelativeAxisPos(axisFan.p23.mach, ref Axes); break;
case 24: ReadRelativeAxisPos(axisFan.p24.mach, ref Axes); break;
case 25: ReadRelativeAxisPos(axisFan.p25.mach, ref Axes); break;
case 26: ReadRelativeAxisPos(axisFan.p26.mach, ref Axes); break;
case 27: ReadRelativeAxisPos(axisFan.p27.mach, ref Axes); break;
case 28: ReadRelativeAxisPos(axisFan.p28.mach, ref Axes); break;
case 29: ReadRelativeAxisPos(axisFan.p29.mach, ref Axes); break;
case 30: ReadRelativeAxisPos(axisFan.p30.mach, ref Axes); break;
case 31: ReadRelativeAxisPos(axisFan.p31.mach, ref Axes); break;
case 32: ReadRelativeAxisPos(axisFan.p32.mach, ref Axes); break;
}
}
return NO_ERROR;
}
//Get the Distance To Go
public override CmsError AXES_RDistanceToGo(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.ODBPOS axisFan = new Focas1.ODBPOS();
short nReturn;
short i;
short NumAxes = Focas1.MAX_AXIS;
// Get path index
short path = GetHandleIndexFromPath(ProcNumber);
if (path < 0)
return PROC_NOT_FOUND_ERROR;
//Execute the method
nReturn = Focas1.cnc_rdposition(nLibHandle[GetHandleIndexFromPath(ProcNumber)], FANUC_DISTTOGO, ref NumAxes, axisFan);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//If an axis goes out from Process Clear the List
if (Axes.Count > NumAxes)
{
Axes.Clear();
}
//Fill all the List
for (i = 1; i <= NumAxes; i++)
{
switch (i)
{
case 1: ReadRelativeAxisPos(axisFan.p1.dist, ref Axes); break;
case 2: ReadRelativeAxisPos(axisFan.p2.dist, ref Axes); break;
case 3: ReadRelativeAxisPos(axisFan.p3.dist, ref Axes); break;
case 4: ReadRelativeAxisPos(axisFan.p4.dist, ref Axes); break;
case 5: ReadRelativeAxisPos(axisFan.p5.dist, ref Axes); break;
case 6: ReadRelativeAxisPos(axisFan.p6.dist, ref Axes); break;
case 7: ReadRelativeAxisPos(axisFan.p7.dist, ref Axes); break;
case 8: ReadRelativeAxisPos(axisFan.p8.dist, ref Axes); break;
case 9: ReadRelativeAxisPos(axisFan.p9.dist, ref Axes); break;
case 10: ReadRelativeAxisPos(axisFan.p10.dist, ref Axes); break;
case 11: ReadRelativeAxisPos(axisFan.p11.dist, ref Axes); break;
case 12: ReadRelativeAxisPos(axisFan.p12.dist, ref Axes); break;
case 13: ReadRelativeAxisPos(axisFan.p13.dist, ref Axes); break;
case 14: ReadRelativeAxisPos(axisFan.p14.dist, ref Axes); break;
case 15: ReadRelativeAxisPos(axisFan.p15.dist, ref Axes); break;
case 16: ReadRelativeAxisPos(axisFan.p16.dist, ref Axes); break;
case 17: ReadRelativeAxisPos(axisFan.p17.dist, ref Axes); break;
case 18: ReadRelativeAxisPos(axisFan.p18.dist, ref Axes); break;
case 19: ReadRelativeAxisPos(axisFan.p19.dist, ref Axes); break;
case 20: ReadRelativeAxisPos(axisFan.p20.dist, ref Axes); break;
case 21: ReadRelativeAxisPos(axisFan.p21.dist, ref Axes); break;
case 22: ReadRelativeAxisPos(axisFan.p22.dist, ref Axes); break;
case 23: ReadRelativeAxisPos(axisFan.p23.dist, ref Axes); break;
case 24: ReadRelativeAxisPos(axisFan.p24.dist, ref Axes); break;
case 25: ReadRelativeAxisPos(axisFan.p25.dist, ref Axes); break;
case 26: ReadRelativeAxisPos(axisFan.p26.dist, ref Axes); break;
case 27: ReadRelativeAxisPos(axisFan.p27.dist, ref Axes); break;
case 28: ReadRelativeAxisPos(axisFan.p28.dist, ref Axes); break;
case 29: ReadRelativeAxisPos(axisFan.p29.dist, ref Axes); break;
case 30: ReadRelativeAxisPos(axisFan.p30.dist, ref Axes); break;
case 31: ReadRelativeAxisPos(axisFan.p31.dist, ref Axes); break;
case 32: ReadRelativeAxisPos(axisFan.p32.dist, ref Axes); break;
}
}
return NO_ERROR;
}
//Get the Programmed Position
public override CmsError AXES_RProgrPosition(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
//In fanuc is not implemented, so i need to build it
Dictionary<string, double> ActualMach = new Dictionary<string, double>();
Dictionary<string, double> ToGo = new Dictionary<string, double>();
KeyValuePair<string, Double> val;
//Read the positions
CmsError cmsError = AXES_RMachinePosition(ProcNumber, ref ActualMach);
if (cmsError.IsError())
return cmsError;
cmsError = AXES_RDistanceToGo(ProcNumber, ref ToGo);
if (cmsError.IsError())
return cmsError;
//If it has the same size
if (ActualMach.Count == ToGo.Count)
{
//If an axis goes out from Process Clear the List
if (Axes.Count > ActualMach.Count)
{
Axes.Clear();
}
foreach (KeyValuePair<string, Double> axis in ActualMach)
{
val = Axes.FirstOrDefault(X => X.Key == axis.Key);
if (val.Key != null)
Axes[val.Key] = axis.Value + ToGo[val.Key];
else
Axes.Add(axis.Key, axis.Value + ToGo[axis.Key]);
}
}
else
return CmsError.InternalError("Impossible to get programmed position");
return NO_ERROR;
}
public override CmsError AXES_RFollowingError(ushort ProcNumber, ref Dictionary<string, double> Axes)
{
return FUNCTION_NOT_ALLOWED_ERROR;
}
public override CmsError AXES_RAxesNames(ushort process, ref List<AxisModel> axesData)
{
return NO_ERROR;
}
public override CmsError AXES_RSelectedAxis(ref byte axisId)
{
return NO_ERROR;
}
public override CmsError AXES_WSelectAxis(byte axisId)
{
return NO_ERROR;
}
#endregion PROCESS-AXES (PATH) High-level data
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: single valiable in memory
//Read-Write a Boolean-Value inside the NC.
public override CmsError MEM_RWBoolean(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemBit, ref bool Value)
{
//Check if the Bit Number is Correct
CmsError cmsError = CheckBitRange(MemBit);
if (cmsError.IsError())
return cmsError;
byte ReadValue = 0;
byte pow = (byte)Math.Pow(2, MemBit);
//Read the Byte where is the bit
cmsError = MEM_RWByte(R, Process, MemType, MemIndex, 0, ref ReadValue);
if (cmsError.IsError())
return cmsError;
//If i have to read -> Read the Bit
if (bWrite == R)
{
Value = (ReadValue & pow) == pow;
}
//If i have to Write -> Write the Bit
else
{
if (Value)
ReadValue = (byte)(ReadValue | (1 << MemBit));
else
ReadValue = (byte)(ReadValue & ~(1 << MemBit));
cmsError = MEM_RWByte(W, Process, MemType, MemIndex, 0, ref ReadValue);
if (cmsError.IsError())
return cmsError;
}
return NO_ERROR;
}
//Read-Write a Byte-Value inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWByte(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemByte, ref byte Value)
{
List<byte> Values = new List<byte>() { Value };
//uses the List method with one-element list
CmsError cmsError = MEM_RWByteList(bWrite, Process, MemType, MemIndex, 0, 1, ref Values);
if (cmsError.IsError())
return cmsError;
Value = Values.First();
return NO_ERROR;
}
//Write a Int-Value inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWInteger(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref int Value)
{
List<int> ListValue = new List<int>() { Value };
//uses the List method with one-element list
CmsError cmsError = MEM_RWIntegerList(bWrite, Process, MemType, MemIndex, 1, ref ListValue);
if (cmsError.IsError())
return cmsError;
Value = ListValue.First();
return NO_ERROR;
}
public override CmsError MEM_RWDWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref uint Value)
{
List<uint> ListValue = new List<uint>() { Value };
//uses the List method with one-element list
CmsError cmsError = MEM_RWDWordList(bWrite, Process, MemType, MemIndex, 1, ref ListValue);
if (cmsError.IsError())
return cmsError;
Value = ListValue.First();
return NO_ERROR;
}
//Write a Short-Value inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWShort(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref short Value)
{
List<short> ListValue = new List<short>() { Value };
//uses the List method with one-element list
CmsError cmsError = MEM_RWShortList(bWrite, Process, MemType, MemIndex, 1, ref ListValue);
if (cmsError.IsError())
return cmsError;
Value = ListValue.First();
return NO_ERROR;
}
//Write a Word-Value inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, ref ushort Value)
{
List<ushort> ListValue = new List<ushort>() { Value };
//uses the List method with one-element list
CmsError cmsError = MEM_RWWordList(bWrite, Process, MemType, MemIndex, 1, ref ListValue);
if (cmsError.IsError())
return cmsError;
Value = ListValue.First();
return NO_ERROR;
}
//--------------------------------------------------------------------------------------------------------------------------
// Siemens Version of Memory-Access Methods
public override CmsError MEM_RWBoolean(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemBit, ref bool Value)
{
return MEM_RWBoolean(bWrite, Process, MemType, MemIndex, MemBit, ref Value);
}
public override CmsError MEM_RWByte(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByte, ref byte Value)
{
return MEM_RWByte(bWrite, Process, MemType, MemIndex, MemByte, ref Value);
}
public override CmsError MEM_RWWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref ushort Value)
{
return MEM_RWWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override CmsError MEM_RWShort(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref short Value)
{
return MEM_RWShort(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override CmsError MEM_RWDWord(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref uint Value)
{
return MEM_RWDWord(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override CmsError MEM_RWInteger(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, ref int Value)
{
return MEM_RWInteger(bWrite, Process, MemType, MemIndex, MemIndex, ref Value);
}
public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int MemByteStart, int Number, ref List<byte> Value)
{
return MEM_RWByteList(bWrite, Process, MemType, MemIndex, MemByteStart, Number, ref Value);
}
public override CmsError MEM_RWWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<ushort> Value)
{
return MEM_RWWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override CmsError MEM_RWShortList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<short> Value)
{
return MEM_RWShortList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override CmsError MEM_RWDWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<uint> Value)
{
return MEM_RWDWordList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
public override CmsError MEM_RWIntegerList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemTable, int MemIndex, int Number, ref List<int> Value)
{
return MEM_RWIntegerList(bWrite, Process, MemType, MemIndex, Number, ref Value);
}
#endregion NC Low-level function: single valiable in memory
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: variables List in memory
//Read-Write a Byte-Value-List inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWByteList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int MemByteStart, int Number, ref List<byte> Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
//Check if the Memory area is corrected
cmsError = CheckMemoryArea(MemType.ToString());
if (cmsError.IsError())
return cmsError;
//The maximum number of variables read/writed at the same time is 5
//I have to iterate it
Focas1.IODBPMC0 Io = new Focas1.IODBPMC0();
Io.cdata = new byte[FANUC_MAXNVAR];
short nReturn;
int ActualNumber;
int FanucNumber;
int Iterat;
int LastNumber;
int ActualStartIndex;
int ActualEndIndex;
int j = 0;
int k = 0;
//Setup the 'Value' variable
if (bWrite)
Number = Value.Count();
else
Value.Clear();
//Setup the iterations
LastNumber = (Number % FANUC_MAXNVAR);
Iterat = Number / FANUC_MAXNVAR;
//Iterates
for (int i = 0; i <= Iterat; i++)
{
//Set the variables-index to iterate
ActualStartIndex = MemIndex + (i * FANUC_MAXNVAR);
if (i == Iterat && LastNumber == 0)
break;
else if (i == Iterat)
ActualEndIndex = ActualStartIndex + LastNumber - 1;
else
ActualEndIndex = ActualStartIndex + FANUC_MAXNVAR - 1;
//Set the variables-numbers to iterate
ActualNumber = (ActualEndIndex - ActualStartIndex + 1);
//Set the variables-number Fanuc
FanucNumber = ActualNumber + 8;
//Set the IODBPMC if you have to write
if (bWrite)
{
Io.datano_s = (short)ActualStartIndex;
Io.datano_e = (short)ActualEndIndex;
Io.type_a = (short)MemType;
Io.type_d = (short)FANUC_DType.BYTE;
//Fill the cdata
j = (i * FANUC_MAXNVAR);
for (k = 0; k < ActualNumber; k++)
Io.cdata[k] = Value[j + k];
}
//Execute the method
if (!bWrite)
nReturn = Focas1.pmc_rdpmcrng(nLibHandle[0], (short)MemType, (short)FANUC_DType.BYTE, (ushort)ActualStartIndex, (ushort)ActualEndIndex, (ushort)FanucNumber, Io);
else
nReturn = Focas1.pmc_wrpmcrng(nLibHandle[0], (ushort)FanucNumber, Io);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Else add the variables in the List (is is a read-statement)
else if (!bWrite)
Value.AddRange(Io.cdata.Take(ActualNumber).ToArray());
}
return NO_ERROR;
}
//Write a Int-Value-List inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWIntegerList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<int> Value)
{
//The maximum number of variables read/writed at the same time is 5
//I have to iterate it
Focas1.IODBPMC2 Io = new Focas1.IODBPMC2();
Io.ldata = new int[FANUC_MAXNVAR];
short nReturn;
int ActualNumber;
int FanucNumber;
int Iterat;
int LastNumber;
int ActualStartIndex;
int ActualEndIndex;
int j = 0;
int k = 0;
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
//Check if the Memory area is corrected
cmsError = CheckMemoryArea(MemType.ToString());
if (cmsError.IsError())
return cmsError;
//Setup the 'Value' variable
if (bWrite)
Number = Value.Count();
else
Value.Clear();
//Setup the iterations
LastNumber = (Number % FANUC_MAXNVAR);
Iterat = Number / FANUC_MAXNVAR;
//Iterates
for (int i = 0; i <= Iterat; i++)
{
//Set the variables-index to iterate
ActualStartIndex = MemIndex + (i * FANUC_MAXNVAR * 4);
if (i == Iterat && LastNumber == 0)
break;
else if (i == Iterat)
ActualEndIndex = ActualStartIndex + (LastNumber * 4) - 1;
else
ActualEndIndex = ActualStartIndex + (FANUC_MAXNVAR * 4) - 1;
//Set the variables-numbers to iterate
ActualNumber = (ActualEndIndex - ActualStartIndex + 1) / 4;
//Set the variables-number Fanuc
FanucNumber = (ActualNumber * 4) + 8;
//Set the IODBPMC if you have to write
if (bWrite)
{
Io.datano_s = (short)ActualStartIndex;
Io.datano_e = (short)ActualEndIndex;
Io.type_a = (short)MemType;
Io.type_d = (short)FANUC_DType.LONG;
//Fill the cdata
j = (i * FANUC_MAXNVAR);
for (k = 0; k < ActualNumber; k++)
Io.ldata[k] = Value[j + k];
}
//Execute the method
if (!bWrite)
nReturn = Focas1.pmc_rdpmcrng(nLibHandle[0], (short)MemType, (short)FANUC_DType.LONG, (ushort)ActualStartIndex, (ushort)ActualEndIndex, (ushort)FanucNumber, Io);
else
nReturn = Focas1.pmc_wrpmcrng(nLibHandle[0], (ushort)FanucNumber, Io);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Else add the variables in the List (is is a read-statement)
else if (!bWrite)
Value.AddRange(Io.ldata.Take(ActualNumber).ToArray());
}
return NO_ERROR;
}
//Write a DWord-Value-List inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWDWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<uint> Values)
{
List<int> WordValues = new List<int>();
//If i have to write convert the INT list into (2x) WORD
if (bWrite)
WordValues = Values.Select(i => (int)i).ToList();
//Exetute the function
CmsError cmsError = MEM_RWIntegerList(bWrite, Process, MemType, MemIndex, Number, ref WordValues);
if (cmsError.IsError())
return cmsError;
//If i have to read, convert the WORD list into INT
if (!bWrite)
Values = WordValues.Select(i => (uint)i).ToList();
return NO_ERROR;
}
//Write a Short-Value-List inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWShortList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<short> Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
//Check if the Memory area is corrected
cmsError = CheckMemoryArea(MemType.ToString());
if (cmsError.IsError())
return cmsError;
//The maximum number of variables read/writed at the same time is 5
//I have to iterate it
Focas1.IODBPMC1 Io = new Focas1.IODBPMC1();
Io.idata = new short[FANUC_MAXNVAR];
short nReturn;
int ActualNumber;
int FanucNumber;
int Iterat;
int LastNumber;
int ActualStartIndex;
int ActualEndIndex;
int j = 0;
int k = 0;
//Setup the 'Value' variable
if (bWrite)
Number = Value.Count();
else
Value.Clear();
//Setup the iterations
LastNumber = (Number % FANUC_MAXNVAR);
Iterat = Number / FANUC_MAXNVAR;
//Iterates
for (int i = 0; i <= Iterat; i++)
{
//Set the variables-index to iterate
ActualStartIndex = MemIndex + (i * FANUC_MAXNVAR * 2);
if (i == Iterat && LastNumber == 0)
break;
else if (i == Iterat)
ActualEndIndex = ActualStartIndex + (LastNumber * 2) - 1;
else
ActualEndIndex = ActualStartIndex + (FANUC_MAXNVAR * 2) - 1;
//Set the variables-numbers to iterate
ActualNumber = (ActualEndIndex - ActualStartIndex + 1) / 2;
//Set the variables-number Fanuc
FanucNumber = (ActualNumber * 2) + 8;
//Set the IODBPMC if you have to write
if (bWrite)
{
Io.datano_s = (short)ActualStartIndex;
Io.datano_e = (short)ActualEndIndex;
Io.type_a = (short)MemType;
Io.type_d = (short)FANUC_DType.WORD;
//Fill the cdata
j = (i * FANUC_MAXNVAR);
for (k = 0; k < ActualNumber; k++)
Io.idata[k] = Value[j + k];
}
//Execute the method
if (!bWrite)
nReturn = Focas1.pmc_rdpmcrng(nLibHandle[0], (short)MemType, (short)FANUC_DType.WORD, (ushort)ActualStartIndex, (ushort)ActualEndIndex, (ushort)FanucNumber, Io);
else
nReturn = Focas1.pmc_wrpmcrng(nLibHandle[0], (ushort)FanucNumber, Io);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
//Else add the variables in the List (is is a read-statement)
else if (!bWrite)
Value.AddRange(Io.idata.Take(ActualNumber).ToArray());
}
return NO_ERROR;
}
//Write a Word-Value-List inside the NC. In writing-mode the field "Number" is not required
public override CmsError MEM_RWWordList(bool bWrite, int Process, MEMORY_TYPE MemType, int MemIndex, int Number, ref List<ushort> Values)
{
List<short> WordValues = new List<short>();
//If i have to write convert the INT list into (2x) WORD
if (bWrite)
WordValues = Values.Select(i => (short)i).ToList();
//Exetute the function
CmsError cmsError = MEM_RWShortList(bWrite, Process, MemType, MemIndex, Number, ref WordValues);
if (cmsError.IsError())
return cmsError;
//If i have to read, convert the WORD list into INT
if (!bWrite)
Values = WordValues.Select(i => (ushort)i).ToList();
return NO_ERROR;
}
#endregion NC Low-level function: variables List in memory
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region NC Low-level function: Parameters
//Get NC Bit Parameter
public override CmsError NC_RParam(short Index, short Bit, ref Boolean Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBPSD_1 outpar = new Focas1.IODBPSD_1();
short nReturn;
short axes_num = 0;
short size = 5;
byte mask = (byte)Math.Pow(2, Bit);
//Check if the Bit Number is Correct
cmsError = CheckBitRange(Bit);
if (cmsError.IsError())
return cmsError;
//Execute the method
nReturn = Focas1.cnc_rdparam(nLibHandle[0], Index, axes_num, size, outpar);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Value = ((outpar.cdata & mask) == mask) ? true : false;
return NO_ERROR;
}
//Get NC Byte Parameter
public override CmsError NC_RParam(short Index, ref byte Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBPSD_1 outpar = new Focas1.IODBPSD_1();
short nReturn;
short axes_num = 0;
short size = 5;
//Execute the method
nReturn = Focas1.cnc_rdparam(nLibHandle[0], Index, axes_num, size, outpar);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Value = outpar.cdata;
return NO_ERROR;
}
//Get NC Word Parameter
public override CmsError NC_RParam(short Index, ref short Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBPSD_1 outpar = new Focas1.IODBPSD_1();
short nReturn;
short axes_num = 0;
short size = 6;
//Execute the method
nReturn = Focas1.cnc_rdparam(nLibHandle[0], Index, axes_num, size, outpar);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Value = outpar.idata;
return NO_ERROR;
}
//Get NC DWord Parameter
public override CmsError NC_RParam(short Index, ref int Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBPSD_1 outpar = new Focas1.IODBPSD_1();
short nReturn;
short axes_num = 0;
short size = 8;
//Execute the method
nReturn = Focas1.cnc_rdparam(nLibHandle[0], Index, axes_num, size, outpar);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Value = outpar.ldata;
return NO_ERROR;
}
//Get NC Real Parameter
public override CmsError NC_RParam(short Index, ref double Value)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
Focas1.IODBPSD_2 outpar = new Focas1.IODBPSD_2();
short nReturn;
short axes_num = 0;
short size = 12;
double Divider = 1;
//Execute the method
nReturn = Focas1.cnc_rdparam(nLibHandle[0], Index, axes_num, size, outpar);
//Throw Exception if there's an error
if (nReturn != 0)
return GetNcError(nReturn);
Divider = Math.Pow(10, outpar.rdata.dec_val);
Value = outpar.rdata.prm_val / Divider;
return NO_ERROR;
}
#endregion NC Low-level function: Parameters
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region File Management
public override CmsError FILES_RGetFileList(string path, ref List<PreviewFileModel> files)
{
return NO_ERROR;
}
public override CmsError FILES_RGetFileInfo(string path, ref InfoFile fileInfo)
{
return NO_ERROR;
}
public override CmsError FILES_WSetActiveProgram(int processId, string filePath, ref ActiveProgramDataModel data)
{
return NO_ERROR;
}
public override CmsError FILES_RActiveProgramData(int processId, ref ActiveProgramDataModel data)
{
return NO_ERROR;
}
public override CmsError FILES_WDeactivateProgram(int processId)
{
return NO_ERROR;
}
public override CmsError FILES_RProgramToFile(string partProgramPath, FileStream localFile)
{
string partProgramContent = "";
// Read NC part program content
CmsError cmsError = ReadPartProgramContent(partProgramPath, ref partProgramContent);
if (cmsError.IsError())
return cmsError;
try
{
// Write content into local file
Nc_Utils.WriteLocalFile(partProgramContent, ref localFile);
}
catch (Exception ex)
{
return CmsError.InternalError(ex.Message);
}
return NO_ERROR;
}
public override CmsError FILES_WProgramFromFile(string partProgramPath, FileStream localFile)
{
string partProgramContent = "";
try
{
// Read local file content
partProgramContent = Nc_Utils.ReadLocalFile(localFile);
}
catch (Exception ex)
{
return CmsError.InternalError(ex.Message);
}
return WritePartProgramContent(partProgramPath, partProgramContent);
}
public override CmsError FILES_CopyProgram(string partProgramPath, string newPartProgramPath, bool failIfExist)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn;
// If the 2 path are the same
if (partProgramPath == newPartProgramPath)
return GetNcError(5); // TODO FIX
if (failIfExist)
{
// Copy Nc part program to the new path
nReturn = Focas1.cnc_pdf_copy(nLibHandle[0], partProgramPath, newPartProgramPath);
//Throw Exception if there's an error
ErrorHandler(nReturn);
}
else
{
// Try a copy and if error is 4 delete file and retry
// Find a function to check if file exist, delete and copy.
// Read and put file
// TODO
//
nReturn = Focas1.cnc_pdf_copy(nLibHandle[0], partProgramPath, newPartProgramPath);
if (nReturn == 5)
{
Focas1.ODBERR errorDetails = new Focas1.ODBERR();
Focas1.cnc_getdtailerr(nLibHandle[0], errorDetails);
if (errorDetails.err_no == 4)
{
FILES_DeleteProgram(newPartProgramPath, "");
nReturn = Focas1.cnc_pdf_copy(nLibHandle[0], partProgramPath, newPartProgramPath);
ErrorHandler(nReturn);
}
}
else if (nReturn != 0)
{
return GetNcError(nReturn);
}
}
return NO_ERROR;
}
public override CmsError FILES_DeleteProgram(string partProgramPath, string partProgramName)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn;
nReturn = Focas1.cnc_pdf_del(nLibHandle[0], partProgramPath + partProgramName);
ErrorHandler(nReturn);
return NO_ERROR;
}
#endregion File Management
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Tools Management
public override CmsError TOOLS_RConfiguration(ref ToolTableConfiguration config)
{
return NO_ERROR;
}
public override CmsError TOOLS_RToolsData(ref List<SiemensToolModel> toolTable)
{
return NO_ERROR;
}
public override CmsError TOOLS_RShanksData(ref List<ShankModel> shanksData)
{
return NO_ERROR;
}
public override CmsError TOOLS_RFamilyData(ref List<FamilyModel> families)
{
return NO_ERROR;
}
public override CmsError TOOLS_RMagazinePositions(ref List<PositionModel> positions)
{
return NO_ERROR;
}
public override CmsError TOOLS_WAddTool(ref SiemensToolModel tool)
{
return NO_ERROR;
}
public override CmsError TOOLS_WAddFamily(ref FamilyModel family)
{
return NO_ERROR;
}
public override CmsError TOOLS_WAddShank(ref ShankModel shank)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateFamilyData(string oldName, string newName)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdatePosition(PositionModel position)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateTool(ref SiemensToolModel tool)
{
return NO_ERROR;
}
public override CmsError TOOLS_WDeleteTool(int id)
{
return NO_ERROR;
}
public override CmsError TOOLS_WDeleteShank(int id)
{
return NO_ERROR;
}
public override CmsError TOOLS_WDeleteFamily(string name)
{
return NO_ERROR;
}
public override CmsError TOOLS_WDeleteEdge(int toolId, int edgeId)
{
return NO_ERROR;
}
public override CmsError TOOLS_WAddEdge(int toolId, ref EdgeModel edge)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateEdge(int toolId, ref EdgeModel newEdge)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateShank(ref ShankModel shank)
{
return NO_ERROR;
}
public override CmsError TOOLS_RMagazineTools(int magazineId, ref List<MountedToolModel> magazinePos)
{
return NO_ERROR;
}
public override CmsError TOOLS_RAvailableTools(ref List<ShankModel> multiTools, ref List<SiemensToolModel> tools)
{
return NO_ERROR;
}
public override CmsError TOOLS_WLoadToolInMagazine(int magazine, NewToolInMagazineModel newMagazineTool)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUnloadToolFromMagazine(int magazineId, int positionId)
{
return NO_ERROR;
}
public override CmsError TOOLS_WLoadToolIntoShank(int shankId, int positionId, int toolId)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUnloadToolFromShank(int shankId, int positionId)
{
return NO_ERROR;
}
public override CmsError TOOLS_GetMagazinesStatus(ref MagazineActionModel magazineAction)
{
return NO_ERROR;
}
#endregion Tools Management
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Nc Tool Manager
public override CmsError TOOLS_RMagazineConfig(ref List<NcMagazineConfigModel> config)
{
return NO_ERROR;
}
public override CmsError TOOLS_WOptions(ToolManagerOptionsModel options)
{
return NO_ERROR;
}
public override CmsError TOOLS_ROffset(short offsetId, ref OffsetModel offset)
{
return NO_ERROR;
}
public override CmsError TOOLS_WOffset(short offsetId, OffsetModel offset)
{
return NO_ERROR;
}
public override CmsError TOOLS_WStartEditData()
{
return NO_ERROR;
}
public override CmsError TOOLS_WStopEditData()
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateTools(List<NcToolModel> list)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateFamilies(List<NcFamilyModel> list)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateShanks(List<NcShankModel> list)
{
return NO_ERROR;
}
public override CmsError TOOLS_WUpdateMagazinePositions(List<NcMagazinePositionModel> list)
{
return NO_ERROR;
}
public override CmsError TOOLS_WStartEditTooling(int magazineId)
{
return NO_ERROR;
}
public override CmsError TOOLS_WStopEditTooling(int magazineId)
{
return NO_ERROR;
}
public override CmsError TOOLS_WRestoreBackup()
{
return NO_ERROR;
}
public override CmsError TOOLS_RUpdatedToolsData(ref Dictionary<int, byte> updatedStatus, ref Dictionary<int, uint> updatedLives)
{
return NO_ERROR;
}
#endregion Nc Tool Manager
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region Subordinate Private Functions
private CmsError ErrorHandler(short exNum)
{
if (exNum != 0)
return GetNcError(exNum);
return NO_ERROR;
}
private void ReadRelativeAxisPos(Focas1.POSELM pos, ref Dictionary<string, double> Axes)
{
//If is setted VISIBLE add to List
if (pos.disp != 0)
{
string AxName;
double AxVal;
KeyValuePair<String, double> val;
//Read the name and Value
AxName = pos.name.ToString() + pos.suff.ToString().Trim('\0');
AxVal = pos.data / Math.Pow(10, pos.dec);
//Fill in the list
val = Axes.FirstOrDefault(X => X.Key == AxName);
if (val.Key != null)
Axes[val.Key] = AxVal;
else
Axes.Add(AxName, AxVal);
}
}
//Convert to Step Language
private CultureInfo ConvertToSTEPLanguage(byte language)
{
switch (language)
{
case 0: return new CultureInfo("en");
case 1: return new CultureInfo("ja");
case 2: return new CultureInfo("de");
case 3: return new CultureInfo("fr");
case 4: return new CultureInfo("zh-CHT");
case 5: return new CultureInfo("it");
case 6: return new CultureInfo("ko");
case 7: return new CultureInfo("es");
case 8: return new CultureInfo("nl");
case 9: return new CultureInfo("da");
case 10: return new CultureInfo("pt");
case 11: return new CultureInfo("pl");
case 12: return new CultureInfo("hu");
case 13: return new CultureInfo("sv");
case 14: return new CultureInfo("cs");
case 15: return new CultureInfo("zh-CHS");
case 16: return new CultureInfo("ru");
case 17: return new CultureInfo("tr");
case 18: return new CultureInfo("bg");
case 19: return new CultureInfo("ro");
case 20: return new CultureInfo("sk");
case 21: return new CultureInfo("fi");
default: return new CultureInfo("en");
}
}
//Manage the Status Process
private PROC_STATUS ConverToSTEPStatus(Focas1.ODBST status)
{
if (status.emergency != 0)
return PROC_STATUS.EMERG;
if (status.alarm != 0)
return PROC_STATUS.ERROR;
switch (status.run)
{
case 0: return PROC_STATUS.IDLE;
case 1: return PROC_STATUS.HOLD;
case 2: return PROC_STATUS.RUN;
case 3: return PROC_STATUS.RUN;
case 4: return PROC_STATUS.RUN;
case 5: return PROC_STATUS.RUN;
case 7: return PROC_STATUS.RUN;
case 8: return PROC_STATUS.RESET;
}
return PROC_STATUS.ERROR;
}
//Manage the Status Mode
private PROC_MODE ConverToSTEPMode(Focas1.ODBST status)
{
switch (status.aut)
{
case 0: return PROC_MODE.MDI;
case 1: return PROC_MODE.AUTO;
case 2: return PROC_MODE.ERROR;
case 3: return PROC_MODE.EDIT;
case 4: return PROC_MODE.HANDLE;
case 5: return PROC_MODE.JOG;
case 6: return PROC_MODE.TEACH;
case 7: return PROC_MODE.TEACH;
case 8: return PROC_MODE.JOGINC;
case 9: return PROC_MODE.REF;
case 10: return PROC_MODE.REMOTE;
}
return PROC_MODE.ERROR;
}
//Get the Right Process
private short GetHandleIndexFromPath(ushort Path)
{
//Check it is too big
if (Path > Cnc_NumPath)
return -1;
//Check it is too small
if (Path <= 0)
return -1;
//Return
return (short)(Path - 1);
}
//Read Static Data
private CmsError ReadStaticNCData()
{
// Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn1, nReturn2;
Focas1.ODBSYS node = new Focas1.ODBSYS();
Focas1.ODBSYS tmpNode = new Focas1.ODBSYS();
Focas1.ODBSYSEX nodeex = new Focas1.ODBSYSEX();
ulong SerialNumber = 0;
//Read oly one time every 10 seconds
if (DateTime.Now - Last_Static_Read > new TimeSpan(NC_MIN_SEC_READ_STATIC_DATA * TimeSpan.TicksPerSecond))
{
//Read Language from NC PARAM
cmsError = NC_RParam((short)PARAM_LING_FANUC.Address, ref FanucLanguage);
if (cmsError.IsError())
{
MessageBox.Show(cmsError.localizationKey);
return cmsError;
}
//Execute the method
nReturn1 = Focas1.cnc_sysinfo(nLibHandle[0], tmpNode);
MessageBox.Show("return 1 dopo metodo " + nReturn1);
node = tmpNode;
nReturn2 = Focas1.cnc_rdcncid(nLibHandle[0], out SerialNumber);
MessageBox.Show("return 1 dopo n return 2" + nReturn1);
//Throw Exception if there's an error
if (nReturn1 != 0)
{
MessageBox.Show("return 1 " + nReturn1);
return GetNcError(nReturn1);
}
else if (nReturn2 != 0 && nReturn2 != 1)
{
MessageBox.Show("return 2 " + nReturn2);
return GetNcError(nReturn2);
}
else
{
//Setup the name
Cnc_name = getName(node.cnc_type, node.mt_type);
//Setup the Software version
Cnc_SftVersion = new string(node.version);
//Setup the Number Series
Cnc_SeriesNum = SerialNumber.ToString();
Last_Static_Read = DateTime.Now;
}
}
return NO_ERROR;
}
//Manage the Exception Launch
private CmsError GetNcError(short exNum)
{
if (exNum == (short)Focas1.focas_ret.EW_SOCKET)
{
NC_Disconnect();
return NOT_CONNECTED_ERROR;
}
return CmsError.NcError(GetErrorMessage(exNum));
}
//Check if NC is connected
private CmsError CheckConnection()
{
if (!NC_IsConnected())
return NOT_CONNECTED_ERROR;
return NO_ERROR;
}
//Check if Memory Area is corrected
private CmsError CheckMemoryArea(String AreaName)
{
if (!AreaName.StartsWith(FANUC_MEMTYPE) && !AreaName.StartsWith(UNDEFINED_MEMTYPE))
return INCORRECT_PARAMETERS_ERROR;
return NO_ERROR;
}
//Check if NC is connected
private CmsError CheckBitRange(int bitnum)
{
if (bitnum < 0 || bitnum > 7)
return BIT_NOT_IN_RANGE_ERROR;
return NO_ERROR;
}
// Convert the internal Name var in Readable STEP Name
private String getName(char[] Cnc_Type, char[] Mt_Type)
{
String name = "Fanuc ";
switch (new String(Cnc_Type))
{
case "15": name += "15 Series"; break;
case "16": name += "16 Series"; break;
case "18": name += "18 Series"; break;
case "21": name += "21 Series"; break;
case "30": name += "30i Series"; break;
case "31": name += "31i Series"; break;
case "32": name += "21i Series"; break;
case "35": name += "35i Series"; break;
case "0": name += "0i Series"; break;
}
name += " " + new string(Mt_Type);
return name;
}
// Write Part program content
private CmsError WritePartProgramContent(string partProgramPath, string partProgramContent)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
short nReturn;
// Start write
nReturn = Focas1.cnc_dwnstart4(nLibHandle[0], 0, partProgramPath);
//Throw Exception if there's an error
ErrorHandler(nReturn);
//int lenghtMax = fileData.Length;
Thread.Sleep(1000);
int lenght = partProgramContent.Length;
// Write program
nReturn = Focas1.cnc_download4(nLibHandle[0], ref lenght, partProgramContent);
//Throw Exception if there's an error
ErrorHandler(nReturn);
nReturn = Focas1.cnc_dwnend4(nLibHandle[0]);
//Throw Exception if there's an error
ErrorHandler(nReturn);
return NO_ERROR;
}
// Read Part program content
private CmsError ReadPartProgramContent(string partProgramPath, ref string partProgramContent)
{
//Check if the NC is Connected
CmsError cmsError = CheckConnection();
if (cmsError.IsError())
return cmsError;
bool fileEnded = false;
short nReturn;
int lenght = 256;
// Start read process
nReturn = Focas1.cnc_upstart4(nLibHandle[0], 0, partProgramPath);
//Throw Exception if there's an error
ErrorHandler(nReturn);
do
{
// Local buffer
char[] tmpContentBuffer = new char[256]; // TODO find function to read size file
// Read file
nReturn = Focas1.cnc_upload4(nLibHandle[0], ref lenght, tmpContentBuffer);
// If File ended
switch (nReturn)
{
case (short)Focas1.focas_ret.EW_RESET:
{
fileEnded = true;
}
break;
case (short)Focas1.EW_OK:
{
// Add the characters into string with all the content
partProgramContent += new string(tmpContentBuffer);
}
break;
case (short)Focas1.focas_ret.EW_BUFFER:
{
continue;
}
break;
default:
{
return GetNcError(nReturn);
}
break;
}
} while ((nReturn == (short)Focas1.focas_ret.EW_OK || nReturn == (short)Focas1.focas_ret.EW_BUFFER) && !fileEnded);
// End read
nReturn = Focas1.cnc_upend4(nLibHandle[0]);
ErrorHandler(nReturn);
partProgramContent = partProgramContent.Replace("\0", String.Empty);
return NO_ERROR;
}
//Manage the Exception Launch
private CmsError ManageException(Exception ex)
{
Connected = false;
return CmsError.InternalError(ex.Message);
}
// Convert the internal error in a readable-String error
private String GetErrorMessage(short Value)
{
String ErrororOwner = "";
ErrororOwner = "Fanuc-Core-Error: ";
Focas1.focas_ret Val = (Focas1.focas_ret)Value;
switch (Val)
{
case Focas1.focas_ret.EW_PROTOCOL: return ErrororOwner + "protocol error";
case Focas1.focas_ret.EW_SOCKET: return ErrororOwner + "Windows socket error";
case Focas1.focas_ret.EW_NODLL: return ErrororOwner + "DLL not exist error";
case Focas1.focas_ret.EW_BUS: return ErrororOwner + "bus error";
case Focas1.focas_ret.EW_SYSTEM2: return ErrororOwner + "system error";
case Focas1.focas_ret.EW_HSSB: return ErrororOwner + "hssb communication error";
case Focas1.focas_ret.EW_HANDLE: return ErrororOwner + "Windows library handle error";
case Focas1.focas_ret.EW_VERSION: return ErrororOwner + "CNC/PMC version missmatch";
case Focas1.focas_ret.EW_UNEXP: return ErrororOwner + "abnormal error";
case Focas1.focas_ret.EW_SYSTEM: return ErrororOwner + "system error";
case Focas1.focas_ret.EW_PARITY: return ErrororOwner + "shared RAM parity error";
case Focas1.focas_ret.EW_MMCSYS: return ErrororOwner + "emm386 or mmcsys install error";
case Focas1.focas_ret.EW_RESET: return ErrororOwner + "reset or stop occured error";
case Focas1.focas_ret.EW_BUSY: return ErrororOwner + "busy error";
case Focas1.focas_ret.EW_FUNC: return ErrororOwner + "command prepare error / pmc not exist";
case Focas1.focas_ret.EW_LENGTH: return ErrororOwner + "data block length error";
case Focas1.focas_ret.EW_NUMBER: return ErrororOwner + "data number / address range error";
case Focas1.focas_ret.EW_ATTRIB: return ErrororOwner + "data type / attribute error";
case Focas1.focas_ret.EW_DATA: return ErrororOwner + "data error";
case Focas1.focas_ret.EW_NOOPT: return ErrororOwner + "no option error";
case Focas1.focas_ret.EW_PROT: return ErrororOwner + "write protect error";
case Focas1.focas_ret.EW_OVRFLOW: return ErrororOwner + "memory overflow error";
case Focas1.focas_ret.EW_PARAM: return ErrororOwner + "cnc parameter not correct error";
case Focas1.focas_ret.EW_BUFFER: return ErrororOwner + "buffer error";
case Focas1.focas_ret.EW_PATH: return ErrororOwner + "path error";
case Focas1.focas_ret.EW_MODE: return ErrororOwner + "cnc mode error";
case Focas1.focas_ret.EW_REJECT: return ErrororOwner + "execution rejected error";
case Focas1.focas_ret.EW_DTSRVR: return ErrororOwner + "data server error";
case Focas1.focas_ret.EW_ALARM: return ErrororOwner + "alarm has been occurred";
case Focas1.focas_ret.EW_STOP: return ErrororOwner + "CNC is not running";
case Focas1.focas_ret.EW_PASSWD: return ErrororOwner + "protection data error";
}
return ErrororOwner + "Generic Error On Function, Error Number: " + Val;
}
#endregion Subordinate Private Functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}