Files
Mapo-IOB-WIN/IOB-WIN/IobFanuc.cs
T
2017-10-24 17:20:15 +02:00

256 lines
7.5 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");
FanucMemRW(R, FANUC.MemType.Y, memIndex, ref MemBlock);
if (verboseLog) lg.Info("END FanucMemRW");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-SEM", 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();
}
/// <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));
}
public override void readPrgName()
{
CncLib.Focas1.ODBEXEPRG answ = FANUC_ref.getPrgName();
string prgName = "";
try
{
string nome = new string(answ.name);
prgName= string.Format("{0} | {1}", answ.o_num, nome);
}
catch(Exception exc)
{
lg.Error(exc, "Errore in readPrgName");
}
if (verboseLog) lg.Info(string.Format("PrgName: {0}", prgName));
parentForm.dataMonitor_3 += prgName;
}
}
}