Files
Mapo-IOB-WIN/IOB-WIN/IobFanuc.cs
T
2017-10-31 17:05:00 +01:00

531 lines
18 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
{
protected byte[] MemBlockF = new byte[2];
protected byte[] MemBlockF_pre = new byte[2];
/// <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[] 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");
// se cambiato loggo!
if (MemBlockF != MemBlockF_pre)
{
lg.Info(string.Format("Dati F: {0:X} | {1:X}", MemBlockF[0], MemBlockF[1]));
}
if (nReadIN % 50 == 0) lg.Info(string.Format("Dati F: {0:X} | {1:X}", MemBlockF[0], MemBlockF[1]));
MemBlockF_pre = MemBlockF;
//if (nReadIN % 50 == 0) lg.Info(string.Format("Dati G: {0:X}", MemBlockG));
// commento lettura X...
#if false
byte[] MemBlockX = new byte[2];
inizio = DateTime.Now;
FanucMemRW(R, FANUC.MemType.X, 6, ref MemBlockX);
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-X", MemBlockF.Length), DateTime.Now.Subtract(inizio).Ticks);
// output!
for (int i = 0; i < MemBlockX.Length; i++)
{
lg.Info(string.Format("MemBlockX[{0}]: {0} | {1}", i, utils.binaryForm((int)MemBlockX[i])));
}
#endif
}
}
/// <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 << 2
// B3: Rosso! Y8.4
if ((primoByte & 16) > 0)
{
B_input += 1 << 3;
}
// B4: giallo! 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;
}
/// <summary>
/// Effettua vero processing contapezzi
/// </summary>
public override void processContapezzi()
{
//base.processContapezzi();
if (utils.CRB("enableContapezzi"))
{
try
{
object output = new object();
// deve usare F_RW_Param_Integer
FANUC_ref.F_RW_Param_Integer(false, 6711, 3, ref output);
lg.Info(string.Format("Contapezzi 6711: {0}", output));
FANUC_ref.F_RW_Param_Integer(false, 6712, 3, ref output);
lg.Info(string.Format("Contapezzi 6712: {0}", output));
FANUC_ref.F_RW_Param_Integer(false, 6713, 3, ref output);
lg.Info(string.Format("Contapezzi 6713: {0}", output));
// 6711: pezzi lavorati
// 6712: pezzi lavorati totali
// 6713: pezzi richiesti
// output!
// posizioni DWord
//1602:543809: NUM PEZZI x ciclo (6408..6411)
//1603:5300: NUM PEZZI RICHIESTI (6412..6415)
//1604:5007: NUM PEZZI FATTI (6416..6419)
//1605:543809: NUM PEZZI TOTALI (6420..6423)
// aggiunta lettura area X
byte[] MemBlockX = new byte[2];
inizio = DateTime.Now;
FanucMemRW(R, FANUC.MemType.X, 6, ref MemBlockX);
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-X", MemBlockF.Length), DateTime.Now.Subtract(inizio).Ticks);
// output!
for (int i = 0; i < MemBlockX.Length; i++)
{
lg.Info(string.Format("MemBlockX_{0}: {0} | {1}", i, utils.binaryForm((int)MemBlockX[i])));
}
// la variabile X7.0 (da 1 a 0) oppure la X7.1 (da 0 a 1) indica il CONTAPEZZI
}
catch (Exception exc)
{
lg.Error(exc, "Errore in contapezzi FANUC");
}
}
}
public override void processMode()
{
//base.processMode();
if (utils.CRB("enableMode"))
{
try
{
inizio = DateTime.Now;
int memIndex = 43;
byte[] MemBlock = new byte[1];
if (verboseLog) lg.Info("START ProcessMode");
FanucMemRW(R, FANUC.MemType.G, memIndex, ref MemBlock);
if (verboseLog) lg.Info("END ProcessMode");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-G43", MemBlock.Length), DateTime.Now.Subtract(inizio).Ticks);
lg.Info(string.Format("Mode G43: {0:X}", MemBlock[0]));
}
catch (Exception exc)
{
lg.Error(exc, "Errore in process Mode G43");
}
}
}
/// <summary>
/// Override salvataggio valori in memoria...
/// </summary>
public override void saveMemDump()
{
dump_MemAreaD();
dump_MemAreaY();
}
public override void sampleMemDump()
{
dump_MemAreaD(6400, 400);
}
/// <summary>
/// Dump PERIODICO area D della memoria
/// </summary>
/// <param name="memIndex">area memoria di partenza</param>
/// <param name="memSyzeByte"></param>
private void dump_MemAreaD(int memIndex, int memSyzeByte)
{
// leggo TUTTI i 9999 byte della memoria D...
byte[] MemBlockD = new byte[memSyzeByte];
inizio = DateTime.Now;
if (verboseLog) lg.Info("START MemDump AreaD");
FanucMemRW(R, FANUC.MemType.D, memIndex, ref MemBlockD);
if (verboseLog) lg.Info("END MemDump AreaD");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-MemDumpD", MemBlockD.Length), DateTime.Now.Subtract(inizio).Ticks);
// file out!
string nomeFile = "";
Dictionary<string, string> mappaValori = new Dictionary<string, string>();
// salvo in file i dati letti come DWord (4byte)
nomeFile = string.Format(@"{0}\SAMPLES\MemDump_D_DW_{1:yyyyMMdd_HHmmss}.dat", utils.dataDatDir, DateTime.Now);
for (int i = 0; i < MemBlockD.Length / 4; i++)
{
mappaValori.Add(i.ToString("0000"), BitConverter.ToUInt32(MemBlockD, i * 4).ToString());
}
utils.WritePlain(mappaValori, nomeFile);
}
/// <summary>
/// Dump area D della memoria
/// </summary>
private void dump_MemAreaD()
{
// faccio chaimate e salvo in file dump...
int memIndex = 0;
// leggo TUTTI i 9999 byte della memoria D...
byte[] MemBlockD = new byte[9999];
inizio = DateTime.Now;
if (verboseLog) lg.Info("START MemDump AreaD");
FanucMemRW(R, FANUC.MemType.D, memIndex, ref MemBlockD);
if (verboseLog) lg.Info("END MemDump AreaD");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-MemDumpD", MemBlockD.Length), DateTime.Now.Subtract(inizio).Ticks);
//
string nomeFile = "";
// salvo in file i dati letti come BYTE
nomeFile = string.Format(@"{0}\MemDump_D_Byte.dat", utils.dataDatDir);
Dictionary<string, string> mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockD.Length; i++)
{
mappaValori.Add(i.ToString("0000"), MemBlockD[i].ToString());
}
utils.WritePlain(mappaValori, nomeFile);
// salvo in file i dati letti come DWord (4byte)
nomeFile = string.Format(@"{0}\MemDump_D_DW.dat", utils.dataDatDir);
mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockD.Length / 4; i++)
{
mappaValori.Add(i.ToString("0000"), BitConverter.ToUInt32(MemBlockD, i * 4).ToString());
}
utils.WritePlain(mappaValori, nomeFile);
// salvo in file i dati letti come DWord (4byte)
nomeFile = string.Format(@"{0}\MemDump_D_W.dat", utils.dataDatDir);
mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockD.Length / 2; i++)
{
mappaValori.Add(i.ToString("0000"), BitConverter.ToUInt16(MemBlockD, i * 2).ToString());
}
utils.WritePlain(mappaValori, nomeFile);
}
/// <summary>
/// Dump area Y della memoria
/// </summary>
private void dump_MemAreaY()
{
// faccio chaimate e salvo in file dump...
int memIndex = 0;
// leggo TUTTI i 9999 byte della memoria Y...
byte[] MemBlockY = new byte[10];
inizio = DateTime.Now;
if (verboseLog) lg.Info("START MemDump AreaY");
FanucMemRW(R, FANUC.MemType.Y, memIndex, ref MemBlockY);
if (verboseLog) lg.Info("END MemDump AreaY");
if (utils.CRB("recTime")) TimingData.addResult(string.Format("R{0}-MemDumpY", MemBlockY.Length), DateTime.Now.Subtract(inizio).Ticks);
//
string nomeFile = "";
// salvo in file i dati letti come BYTE
nomeFile = string.Format(@"{0}\MemDump_Y_Byte.dat", utils.dataDatDir);
Dictionary<string, string> mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockY.Length; i++)
{
mappaValori.Add(i.ToString("0000"), MemBlockY[i].ToString());
}
utils.WritePlain(mappaValori, nomeFile);
// salvo in file i dati letti come DWord (4byte)
nomeFile = string.Format(@"{0}\MemDump_Y_DW.dat", utils.dataDatDir);
mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockY.Length / 4; i++)
{
mappaValori.Add(i.ToString("0000"), BitConverter.ToUInt32(MemBlockY, i * 4).ToString());
}
utils.WritePlain(mappaValori, nomeFile);
// salvo in file i dati letti come DWord (4byte)
nomeFile = string.Format(@"{0}\MemDump_Y_W.dat", utils.dataDatDir);
mappaValori = new Dictionary<string, string>();
for (int i = 0; i < MemBlockY.Length / 2; i++)
{
mappaValori.Add(i.ToString("0000"), BitConverter.ToUInt16(MemBlockY, i * 2).ToString());
}
utils.WritePlain(mappaValori, nomeFile);
}
}
}