Files
Mapo-IOB-WIN/IOB-WIN/IobFanuc.cs
T
Samuele E. Locatelli e4bbd52f9b leggo solo F
2017-10-30 12:54:00 +01:00

310 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IOB_UT;
using CncLib.CNC;
namespace IOB_WIN
{
public class IobFanuc : IobGeneric
{
/// <summary>
/// wrapper chiamata lettura/scrittura SINGOLO BYTE...
/// </summary>
/// <param name="bWrite"></param>
/// <param name="MemType"></param>
/// <param name="memIndex"></param>
/// <param name="Value"></param>
/// <returns></returns>
public bool FanucMemRW(bool bWrite, FANUC.MemType MemType, Int32 memIndex, ref byte Value)
{
bool answ = false;
if (FANUC_ref.Connected)
{
try
{
parentForm.commPlcActive = true;
answ = FANUC_ref.F_RW_Byte(bWrite, MemType, memIndex, ref Value);
}
catch
{ }
}
parentForm.commPlcActive = false;
return answ;
}
/// <summary>
/// wrapper chiamata lettura/scrittura MULTI BYTE...
/// </summary>
/// <param name="bWrite"></param>
/// <param name="MemType"></param>
/// <param name="memIndex"></param>
/// <param name="MATRICE Value"></param>
/// <returns></returns>
public bool FanucMemRW(bool bWrite, FANUC.MemType MemType, Int32 memIndex, ref byte[] Value)
{
bool answ = false;
if (FANUC_ref.Connected)
{
try
{
parentForm.commPlcActive = true;
answ = FANUC_ref.F_RW_Byte(bWrite, MemType, memIndex, ref Value);
}
catch
{ }
}
parentForm.commPlcActive = false;
return answ;
}
/// <summary>
/// Oggetto MAIN x connessione FANUC
/// </summary>
protected FANUC FANUC_ref;
/// <summary>
/// estende l'init della classe base...
/// </summary>
/// <param name="caller"></param>
/// <param name="adpConf"></param>
public IobFanuc(MainForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
{
// i dati RAW principali sono 6 byte...
RawInput = new byte[6];
// è little endian (NON serve conversione)
hasBigEndian = false;
lg.Info("Start init Adapter FANUC all'IP {0}:{1} per IOB {2}", IOBConf.cncIpAddr, IOBConf.cncPort, IOBConf.codIOB);
// Creo oggetto connessione NC
parentForm.commPlcActive = true;
Runtime.CreateNC(CNC.NcType.FANUC, IOBConf.cncIpAddr, IOBConf.cncPort);
parentForm.commPlcActive = false;
// aggiungo referenza obj FANUC
FANUC_ref = (FANUC)Runtime.NC;
if (utils.CRB("verbose")) lg.Info("FANUC_ref da CncLib");
// disconnetto e connetto...
if (utils.CRB("verbose")) lg.Info("FANUC: tryDisconnect");
tryDisconnect();
lg.Info("FANUC: tryConnect");
tryConnect();
lg.Info("End init Adapter FANUC");
}
/// <summary>
/// Override disconnessione
/// </summary>
public override void tryDisconnect()
{
if (connectionOk)
{
string szStatusConnection = "";
try
{
FANUC_ref.Disconnect(ref szStatusConnection);
connectionOk = false;
lg.Info(szStatusConnection);
lg.Info("Effettuata disconnessione adapter FANUC!");
}
catch (Exception exc)
{
lg.Fatal(exc, "Errore nella disconnessione dall'adapter FANUC");
}
}
else
{
lg.Error("IMPOSSIBILE effettuare disconnessione: Connessione non disponibile...");
}
}
/// <summary>
/// Override connessione
/// </summary>
public override void tryConnect()
{
if (!connectionOk)
{
string szStatusConnection = "";
try
{
parentForm.commPlcActive = true;
FANUC_ref.Connect(ref szStatusConnection);
parentForm.commPlcActive = false;
lg.Info("szStatusConnection: " + szStatusConnection);
connectionOk = true;
// refresh stato allarmi!!!
if (connectionOk)
{
if (adpRunning)
{
// carico status allarmi (completo)
lg.Info("Inizio refresh completo stato allarmi...");
forceAlarmCheck();
lg.Info("Completato refresh completo stato allarmi!");
}
else
{
lg.Info("Connessione OK");
}
}
else
{
lg.Error("Impossibile procedere, connessione mancante...");
}
}
catch (Exception exc)
{
lg.Fatal(string.Format("Errore nella connessione all'adapter FANUC: {0}{1}{2}", szStatusConnection, Environment.NewLine, exc));
connectionOk = false;
lg.Info(string.Format("Segnalo Adapter NON running, pausa di {0} msec prima di ulteriori tentativi di riconnessione", utils.CRI("waitRecMSec")));
}
}
}
public override void readSemafori()
{
base.readSemafori();
if (verboseLog) lg.Info("inizio read semafori");
parentForm.sIN = Semaforo.SV;
// leggo SEMAFORI, leggo da Y8 ...leggo 6 bit da Y8 a Y13...
byte[] MemBlock = new byte[RawInput.Length];
// Red: Y8.4 | Yellow: Y8.5 | Green Y8.6 | WrkZone Y8.7 | All DOorsClosed: Y10.5
int memIndex = 8;
inizio = DateTime.Now;
if (verboseLog) lg.Info("START FanucMemRW-Y");
FanucMemRW(R, FANUC.MemType.Y, memIndex, ref MemBlock);
if (verboseLog) lg.Info("END FanucMemRW-Y");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-Y", MemBlock.Length), DateTime.Now.Subtract(inizio).Ticks);
if (verboseLog) lg.Info(string.Format("MemBlock: {0}", utils.binaryForm((int)MemBlock[0])));
// suddivido!
Buffer.BlockCopy(MemBlock, 0, RawInput, 0, 6);
if (verboseLog) lg.Info(string.Format("RawInput: {0}", utils.binaryForm((int)RawInput[0])));
// salvo il solo BYTE dell'input decifrando il semaforo...
decodeToBitmap();
if (utils.CRB("enableTest"))
{
if (verboseLog) lg.Info("START FanucMemRW-FG");
// !!!FARE!!! ora leggo CABLATO i byte F e G...
byte[] MemBlockF = new byte[2];
byte[] MemBlockG = new byte[2];
inizio = DateTime.Now;
FanucMemRW(R, FANUC.MemType.F, 0, ref MemBlockF);
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-F", MemBlockF.Length), DateTime.Now.Subtract(inizio).Ticks);
//inizio = DateTime.Now;
//FanucMemRW(R, FANUC.MemType.G, 7, ref MemBlockG);
//if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-G", MemBlockG.Length), DateTime.Now.Subtract(inizio).Ticks);
if (verboseLog) lg.Info("END FanucMemRW-FG");
if (nReadIN % 50 == 0) lg.Info(string.Format("Dati F: {0:X}", MemBlockF));
//if (nReadIN % 50 == 0) lg.Info(string.Format("Dati G: {0:X}", MemBlockG));
}
}
/// <summary>
/// Effettua decodifica aree memoria alla bitmap usata x MAPO
/// </summary>
private void decodeToBitmap()
{
// init a zero...
B_input = 0;
// !!!FARE!!! generalizzazione... x ora usa dati "hard coded"...
//Red: Y8.4 | Yellow: Y8.5 | Green Y8.6
// leggo primo byte...
int primoByte = RawInput[0];
if (verboseLog) lg.Info(string.Format("PrimoByte: {0}", primoByte));
if (utils.CRB("macOnFromConn"))
{
// SE SI E' CONNESSO al FANUC allora è 1...
if (FANUC_ref.Connected)
{
B_input += 1 << 0;
}
}
else
{
// B0: facio un OR tra i 3 semafori...
if ((primoByte & 112) > 0)
{
B_input += 1 << 0;
}
}
// B1: verde! Y8.6
if ((primoByte & 64) > 0)
{
B_input += 1 << 1;
}
// NON HO CONTAPEZZI ovvero 1 << 3
// B1: verde! Y8.4
if ((primoByte & 16) > 0)
{
B_input += 1 << 3;
}
// B1: verde! Y8.5
if ((primoByte & 32) > 0)
{
B_input += 1 << 4;
}
if (verboseLog) lg.Info(string.Format("Trasformazione B_input: {0}", B_input));
}
/// <summary>
/// Recupero programma in lavorazione
/// </summary>
/// <returns></returns>
public override string getPrgName()
{
string prgName = "";
CncLib.Focas1.ODBEXEPRG answ = FANUC_ref.getPrgName();
try
{
// nome prg bonificato!
string nome = utils.purgedChar2String(answ.name);
//string nome = new string(answ.name.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray());
prgName = string.Format("{0}|{1}", answ.o_num, nome);
}
catch (Exception exc)
{
lg.Error(exc, "Errore in getPrgName");
}
return prgName;
}
/// <summary>
/// Recupero programma in lavorazione come Dictionary FANUC...
/// - SYSINFO: (prima KEY globale) TUTTI i valori separati da # (x fare check modifica)
/// - altre stringhe: ogni singolo parametro / valore
/// </summary>
/// <returns></returns>
public override Dictionary<string, string> getSysInfo()
{
Dictionary<string, string> outVal = new Dictionary<string, string>();
CncLib.Focas1.ODBSYS answ = FANUC_ref.getSysInfo();
try
{
string cnc_type = new string(answ.cnc_type);
string mt_type = new string(answ.mt_type);
string series = new string(answ.series);
string version = new string(answ.version);
string axes = new string(answ.axes);
//short addInfo = answ.addinfo;
short max_axis = answ.max_axis;
// preparo i singoli valori dell'array...
outVal.Add("SYSINFO", string.Format("{0}#{1}#{2}#{3}#{4}#{5}", cnc_type, mt_type, series, version, axes, max_axis));
outVal.Add("CNC", cnc_type);
outVal.Add("MTT", mt_type);
outVal.Add("SER", series);
outVal.Add("VER", version);
outVal.Add("AXS", string.Format("{0}|{1}", axes, max_axis));
}
catch (Exception exc)
{
lg.Error(exc, "Errore in getSysInfo");
}
return outVal;
}
}
}