21c8a12310
Fix errore getBit (sostituito con HasFlag)
458 lines
12 KiB
C#
458 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MTC_Sim
|
|
{
|
|
public class utils
|
|
{
|
|
|
|
/// <summary>
|
|
/// legge conf in formato stringa
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static string CRS(string key)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
answ = ConfigurationManager.AppSettings[key].ToString();
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// legge conf in formato INT
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static Int32 CRI(string key)
|
|
{
|
|
int answ = 0;
|
|
try
|
|
{
|
|
answ = Convert.ToInt32(CRS(key));
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// verifica se un dato bit sia alzato (come flag di strobe)
|
|
/// </summary>
|
|
/// <param name="value">valore da testare</param>
|
|
/// <param name="flag">valore cercato, può essere un singolo valore o un insieme in modalità AND</param>
|
|
/// <returns></returns>
|
|
public static bool IsSetAll(Strobe value, Strobe flag)
|
|
{
|
|
return ((value & flag) == flag);
|
|
}
|
|
/// <summary>
|
|
/// verifica se un dato bit sia alzato (come flag di strobe)
|
|
/// </summary>
|
|
/// <param name="value">valore da testare</param>
|
|
/// <param name="flag">valore cercato, può essere un singolo valore o un insieme in modalità OR</param>
|
|
/// <returns></returns>
|
|
public static bool IsSetAny(Strobe value, Strobe flag)
|
|
{
|
|
return ((value & flag) != 0);
|
|
}
|
|
/// <summary>
|
|
/// verifica se un dato bit sia alzato (come flag di strobe)
|
|
/// </summary>
|
|
/// <param name="value">valore da testare</param>
|
|
/// <param name="flag">valore cercato, può essere un singolo valore o un insieme in modalità AND</param>
|
|
/// <returns></returns>
|
|
public static bool IsSetAll(StatusBitMap value, StatusBitMap flag)
|
|
{
|
|
return ((value & flag) == flag);
|
|
}
|
|
/// <summary>
|
|
/// verifica se un dato bit sia alzato (come flag di strobe)
|
|
/// </summary>
|
|
/// <param name="value">valore da testare</param>
|
|
/// <param name="flag">valore cercato, può essere un singolo valore o un insieme in modalità OR</param>
|
|
/// <returns></returns>
|
|
public static bool IsSetAny(StatusBitMap value, StatusBitMap flag)
|
|
{
|
|
return ((value & flag) != 0);
|
|
}
|
|
/// <summary>
|
|
/// formatta un numero in forma binaria 0/1
|
|
/// </summary>
|
|
/// <param name="valore"></param>
|
|
/// <returns></returns>
|
|
public static string binaryForm(int valore)
|
|
{
|
|
string answ = "";
|
|
try
|
|
{
|
|
answ = string.Format(new BinaryFormatter(), "{0:B}", valore);
|
|
}
|
|
catch
|
|
{ }
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// imposta un bita al valore richiesto duplicando il valore IN come OUT
|
|
/// </summary>
|
|
/// <param name="original">valore originale da aggiornare</param>
|
|
/// <param name="bitVal">valore richiesto x il bit (0/1)</param>
|
|
/// <param name="bitIndex">indice bit, 0 based (es: 0..31 per 32bit)</param>
|
|
/// <returns></returns>
|
|
public static byte[] setBitOnStFlag(byte[] original, int bitVal, int bitIndex)
|
|
{
|
|
// risposta è identica ad originale...
|
|
byte[] answ = original;
|
|
// verifico se il bit è 0/1
|
|
if (bitVal <= 1 && bitVal >= 0)
|
|
{
|
|
// verifico se si possa aggiornare il bit richiesto (<= al totale dei bit...)
|
|
if (bitIndex <= original.Length * 8 - 1)
|
|
{
|
|
// calcolo byte
|
|
int byteIndex = bitIndex / 8;
|
|
// bit nel byte
|
|
int bitInByteIndex = bitIndex % 8;
|
|
// bit richiesto
|
|
byte mask = (byte)(bitVal << bitInByteIndex);
|
|
// imposto!
|
|
answ[byteIndex] |= mask;
|
|
#if false
|
|
byte mask = (byte)(1 << bitInByteIndex);
|
|
bool isSet = (bytes[byteIndex] & mask) != 0;
|
|
// set to 1
|
|
bytes[byteIndex] |= mask;
|
|
// Set to zero
|
|
bytes[byteIndex] &= ~mask;
|
|
// Toggle
|
|
bytes[byteIndex] ^= mask;
|
|
#endif
|
|
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
}
|
|
|
|
|
|
public enum tipoAdapter
|
|
{
|
|
/// <summary>
|
|
/// Adapter generico/demo
|
|
/// </summary>
|
|
DEMO,
|
|
/// <summary>
|
|
/// adapter FANUC-CMS
|
|
/// </summary>
|
|
FANUC,
|
|
/// <summary>
|
|
/// Adapter HMI-SCM
|
|
/// </summary>
|
|
HMI,
|
|
/// <summary>
|
|
/// Adapter non specificato
|
|
/// </summary>
|
|
ND
|
|
}
|
|
/// <summary>
|
|
/// Sorgente dati (per modalità recupero sw)
|
|
/// </summary>
|
|
public enum fonteDati
|
|
{
|
|
/// <summary>
|
|
/// dati recuperati da metodi/classi con HMI
|
|
/// </summary>
|
|
HMI,
|
|
/// <summary>
|
|
/// dati recuperati da recupero memorie PLC/CNC
|
|
/// </summary>
|
|
MemLoad,
|
|
/// <summary>
|
|
/// in questo caso dato fittizio/senza dati (x init)
|
|
/// </summary>
|
|
NoData,
|
|
/// <summary>
|
|
/// Dati generati random
|
|
/// </summary>
|
|
Random
|
|
}
|
|
|
|
public enum gatherCycle
|
|
{
|
|
/// <summary>
|
|
/// lettura dati ad alta frequenza
|
|
/// </summary>
|
|
HF,
|
|
/// <summary>
|
|
/// lettura dati standard
|
|
/// </summary>
|
|
MF,
|
|
/// <summary>
|
|
/// lettura dati bassa freq
|
|
/// </summary>
|
|
LF
|
|
}
|
|
|
|
/// <summary>
|
|
/// informazioni di produzione
|
|
/// </summary>
|
|
public struct prodData
|
|
{
|
|
public string Operator;
|
|
|
|
public bool Status;
|
|
public int AccTime;
|
|
public int Power;
|
|
public string FuncMode;
|
|
public bool EmrStop;
|
|
public string MessageCode;
|
|
public string MessageText;
|
|
|
|
}
|
|
|
|
public struct PathData
|
|
{
|
|
public int PathSel;
|
|
public string PathType; // LAVOR/ASSERV
|
|
public string RunMode;
|
|
public string ExeMode;
|
|
public int pzTot;
|
|
public string ProgramName;
|
|
public string ProgrRow;
|
|
public string PartId;
|
|
public string ActiveAxes;
|
|
public string CodG_Act;
|
|
public string SubMode;
|
|
|
|
public int PathFeedrate;
|
|
public int PathFeedrateOver;
|
|
public int PathRapidOver;
|
|
public position PathPosAct;
|
|
}
|
|
|
|
|
|
public struct UnOpData
|
|
{
|
|
public int UnOpSel;
|
|
public int UnOpToolId;
|
|
public int UnOpNumCU;
|
|
public string UnOpStatus;
|
|
public int UnOpVitaRes;
|
|
public int UnOpSpeed;
|
|
public int UnOpLoad;
|
|
public int UnOpAccTime;
|
|
}
|
|
|
|
public struct AxisData
|
|
{
|
|
public int AxisSel;
|
|
public string AxisMainProc;
|
|
public bool AxisIsMaster;
|
|
public string AxisMastId;
|
|
public string AxisType;
|
|
public string AxisDir;
|
|
public int AxisLoad;
|
|
public int AxisPosAct;
|
|
public int AxisPosTgt;
|
|
public int AxisFeedAct;
|
|
public int AxisFeedOver;
|
|
public string AxisAccel;
|
|
public string AxisAccTime;
|
|
public string AxisBattery;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vettore completo posizione (X-Y-Z con versori i-j-k)
|
|
/// </summary>
|
|
public class position
|
|
{
|
|
public float x;
|
|
public float y;
|
|
public float z;
|
|
public float i;
|
|
public float j;
|
|
public float k;
|
|
public position()
|
|
{
|
|
x = 0;
|
|
y = 0;
|
|
z = 0;
|
|
i = 0;
|
|
j = 0;
|
|
k = 0;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Allarme (per decodifica)
|
|
/// </summary>
|
|
public class allarme
|
|
{
|
|
public string codNum;
|
|
public string gruppo;
|
|
public string livello;
|
|
public string descrizione;
|
|
public allarme()
|
|
{
|
|
codNum = "";
|
|
gruppo = "";
|
|
livello = "";
|
|
descrizione = "";
|
|
}
|
|
public allarme(string _codNum, string _gruppo, string _livello, string _descrizione)
|
|
{
|
|
codNum = _codNum;
|
|
gruppo = _gruppo;
|
|
livello = _livello;
|
|
descrizione = _descrizione;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Strobe: contiene il set di strobe di comunicazione
|
|
///
|
|
/// rif: http://stackoverflow.com/questions/17209054/parse-bits-in-a-byte-to-enum
|
|
/// </summary>
|
|
[Flags]
|
|
public enum Strobe : int
|
|
{
|
|
NONE = 0,
|
|
M_CODE = 1 << 0,
|
|
S_CODE = 1 << 1,
|
|
T_CODE = 1 << 2,
|
|
PZ_OK = 1 << 3,
|
|
PZ_KO = 1 << 4,
|
|
FEED_SPEED = 1 << 5,
|
|
POS_ACT = 1 << 6,
|
|
SP07 = 1 << 7,
|
|
SP08 = 1 << 8,
|
|
SP09 = 1 << 9,
|
|
SP10 = 1 << 10,
|
|
SP11 = 1 << 11,
|
|
SP12 = 1 << 12,
|
|
SP13 = 1 << 13,
|
|
SP14 = 1 << 14,
|
|
SP15 = 1 << 15,
|
|
SP16 = 1 << 16,
|
|
SP17 = 1 << 17,
|
|
SP18 = 1 << 18,
|
|
SP19 = 1 << 19,
|
|
SP20 = 1 << 20,
|
|
SP21 = 1 << 21,
|
|
SP22 = 1 << 22,
|
|
SP23 = 1 << 23,
|
|
SP24 = 1 << 24,
|
|
SP25 = 1 << 25,
|
|
SP26 = 1 << 26,
|
|
SP27 = 1 << 27,
|
|
SP28 = 1 << 28,
|
|
SP29 = 1 << 29,
|
|
SP30 = 1 << 30,
|
|
SP31 = 1 << 31
|
|
}
|
|
/// <summary>
|
|
/// StFlag8: set di 8 bit (1 word) contente semaforo di variabili
|
|
/// </summary>
|
|
[Flags]
|
|
public enum StFlag8 : int
|
|
{
|
|
NONE = 0,
|
|
B0 = 1 << 0,
|
|
B1 = 1 << 1,
|
|
B2 = 1 << 2,
|
|
B3 = 1 << 3,
|
|
B4 = 1 << 4,
|
|
B5 = 1 << 5,
|
|
B6 = 1 << 6,
|
|
B7 = 1 << 7
|
|
}
|
|
/// <summary>
|
|
/// StFlag32: set di 32 bit (4 word) contente semaforo di variabili
|
|
/// </summary>
|
|
[Flags]
|
|
public enum StFlag32 : int
|
|
{
|
|
NONE = 0,
|
|
B00 = 1 << 0,
|
|
B01 = 1 << 1,
|
|
B02 = 1 << 2,
|
|
B03 = 1 << 3,
|
|
B04 = 1 << 4,
|
|
B05 = 1 << 5,
|
|
B06 = 1 << 6,
|
|
B07 = 1 << 7,
|
|
B08 = 1 << 8,
|
|
B09 = 1 << 9,
|
|
B10 = 1 << 10,
|
|
B11 = 1 << 11,
|
|
B12 = 1 << 12,
|
|
B13 = 1 << 13,
|
|
B14 = 1 << 14,
|
|
B15 = 1 << 15,
|
|
B16 = 1 << 16,
|
|
B17 = 1 << 17,
|
|
B18 = 1 << 18,
|
|
B19 = 1 << 19,
|
|
B20 = 1 << 20,
|
|
B21 = 1 << 21,
|
|
B22 = 1 << 22,
|
|
B23 = 1 << 23,
|
|
B24 = 1 << 24,
|
|
B25 = 1 << 25,
|
|
B26 = 1 << 26,
|
|
B27 = 1 << 27,
|
|
B28 = 1 << 28,
|
|
B29 = 1 << 29,
|
|
B30 = 1 << 30,
|
|
B31 = 1 << 31
|
|
}
|
|
/// <summary>
|
|
/// StatusBitMap: contiene il set di semafori/flag x status + allarmi (x classi)
|
|
/// </summary>
|
|
[Flags]
|
|
public enum StatusBitMap : int
|
|
{
|
|
NONE = 0,
|
|
ESTOP = 1 << 0,
|
|
RM_AUTO = 1 << 1,
|
|
RM_MANUAL = 1 << 2,
|
|
RM_MDI = 1 << 3,
|
|
RM_EDIT = 1 << 4,
|
|
EM_RUN = 1 << 5,
|
|
EM_READY = 1 << 6,
|
|
EM_STOP = 1 << 7,
|
|
EM_FEEDHOLD = 1 << 8,
|
|
HM = 1 << 9,
|
|
ST11 = 1 << 10,
|
|
ST12 = 1 << 11,
|
|
ST13 = 1 << 12,
|
|
ST14 = 1 << 13,
|
|
ST15 = 1 << 14,
|
|
ST16 = 1 << 15,
|
|
AL01 = 1 << 16,
|
|
AL02 = 1 << 17,
|
|
AL03 = 1 << 18,
|
|
AL04 = 1 << 19,
|
|
AL05 = 1 << 20,
|
|
AL06 = 1 << 21,
|
|
AL07 = 1 << 22,
|
|
AL08 = 1 << 23,
|
|
AL09 = 1 << 24,
|
|
AL10 = 1 << 25,
|
|
AL11 = 1 << 26,
|
|
AL12 = 1 << 27,
|
|
AL13 = 1 << 28,
|
|
AL14 = 1 << 29,
|
|
AL15 = 1 << 30,
|
|
AL16 = 1 << 31
|
|
}
|
|
}
|