using EasyModbus;
using IOB_UT_NEXT;
using MapoSDK;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace IOB_WIN_NEXT
{
/* --------------------------------------------------------------------------------
* Controlli ModBusTCP Centerfrigo
* - protocollo ModBus TCP
* - specifico comportamento impianti Centerfrigo
* - gestione allarmi
* - gestione stati salvati come dynData
* - gestione valori analogici
*
* STRUTTURA MEMORIA a banchi di UInt16, convertiti successivamente in int EVENTUALMENTE divisi per 10/100/1000
* lettura: xxx byte,
* scrittura yyy byte
* G:\Drive condivisi\30_Clienti\Giacovelli - WIL\CenterFrigo
*
* ATTENZIONE! leggere al max 44 byte alla volta
*
* -------------------------------------------------------------------------------- */
public class IobModbusTCPCenterfrigo : IobModbusTCP
{
#region Public Constructors
///
/// Classe base con i metodi x ModBusTCP
///
///
///
public IobModbusTCPCenterfrigo(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
{
lgInfo("NEW IOB ModBus TCP Centerfrigo");
// provo lettura una prima volta i dati DYN
if (currPLC != null && currPLC.Connected)
{
try
{
#if false
// test lettura da eliminare
testRead();
#endif
processDynData();
}
catch (Exception exc)
{
lgError($"Eccezione in processDynData iniziale x ModBus TCP Cedax:{Environment.NewLine}{exc}");
}
}
}
#endregion Public Constructors
#region Private Methods
private void testBlockRead(int baseAddr, int numByte)
{
int baseHold = 40001;
int[] readTestFull = new int[2];
lgTrace($"-------------------- Inizio test lettura {baseAddr} --------------------");
try
{
stopwatch.Restart();
readTestFull = currPLC.ReadHoldingRegisters(baseAddr - baseHold, numByte);
stopwatch.Stop();
lgTrace($"Stats lettura | {readTestFull.Length} val | {stopwatch.ElapsedMilliseconds}ms");
}
catch
{ }
for (int i = 0; i < readTestFull.Length / 2; i++)
{
int[] thisSet = new int[2];
Array.Copy(readTestFull, i * 2, thisSet, 0, 2);
lgTrace($"{baseAddr + i * 2:000} | HoldingRegisters: {thisSet[0]} / {thisSet[1]} | Val Real: {ModbusClient.ConvertRegistersToFloat(thisSet):N6}");
}
lgTrace("-------------------- Completato test lettura {baseAddr} --------------------");
}
private void testRead()
{
foreach (var item in memSetR)
{
testBlockRead(item.Key, item.Value);
}
}
#endregion Private Methods
#region Protected Methods
///
/// Effettua decodifica aree memoria alla bitmap usata x MAPO
///
protected override void decodeToBaseBitmap()
{
// init a zero...
B_input = 0;
/* -----------------------------------------------------
* bitmap MAPO STANDARD 60
* B0: POWER_ON
* B1: RUN
* B2: pzCount
* B3: allarme
* B4: manuale
* B5: slowTC
* B6: WarmUpCoolDown
* B7: EmergArmata
*
----------------------------------------------------- */
var MemInt = new byte[2];
int byteSignals = 0;
// bit 0 (poweron) imposto a 1 SE connected...
if (currPLC.Connected)
{
byteSignals += (1 << 0);
}
// processo dagli stati + gravi...
if (hasAlarms)
{
byteSignals += (1 << 3);
}
else
{
byteSignals += (1 << 1);
}
// segnalo NON emergenza
byteSignals += (1 << 7);
// salvo!
B_input = byteSignals;
}
#endregion Protected Methods
}
}