142 lines
4.7 KiB
C#
142 lines
4.7 KiB
C#
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 COMECA
|
|
* - protocollo ModBus TCP HAM
|
|
* - specifico comportamento impianti HAM Pizzaferri
|
|
*
|
|
* STRUTTURA MEMORIA a banchi di byte, convertiti successivamente in bit/int/real:
|
|
* lettura: xxx byte,
|
|
* scrittura yyy byte
|
|
* G:\Drive condivisi\30_Clienti\Pizzaferri\Impianti\HAM
|
|
*
|
|
* -------------------------------------------------------------------------------- */
|
|
|
|
public class IobModbusTCPHam : IobModbusTCP
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// Classe base con i metodi x ModBusTCP
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="adpConf"></param>
|
|
public IobModbusTCPHam(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
lgInfo("NEW IOB ModBus TCP HAM");
|
|
|
|
// provo lettura una prima volta i dati DYN
|
|
if (currPLC != null && currPLC.Connected)
|
|
{
|
|
try
|
|
{
|
|
processDynData();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
lgError($"Eccezione in processDynData iniziale x ModBus TCP HAM:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Methods
|
|
|
|
private void testBlockRead(int baseAddr, int numByte)
|
|
{
|
|
int baseHold = 40001;
|
|
int[] readTestFull = new int[2];
|
|
lgInfo($"-------------------- Inizio test lettura {baseAddr} --------------------");
|
|
try
|
|
{
|
|
stopwatch.Restart();
|
|
readTestFull = currPLC.ReadHoldingRegisters(baseAddr - baseHold, numByte);
|
|
stopwatch.Stop();
|
|
lgInfo($"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);
|
|
lgInfo($"{baseAddr + i * 2:000} | HoldingRegisters: {thisSet[0]} / {thisSet[1]} | Val Real: {ModbusClient.ConvertRegistersToFloat(thisSet):N6}");
|
|
}
|
|
lgInfo("-------------------- Completato test lettura {baseAddr} --------------------");
|
|
}
|
|
|
|
private void testRead()
|
|
{
|
|
//Ip-Address and Port of Modbus-TCP-Server
|
|
//currPLC = new ModbusClient(cIobConf.cncIpAddr, 502);
|
|
////Connect to Server
|
|
//currPLC.Connect();
|
|
//modbusClient.WriteMultipleCoils(4, new bool[] { true, true, true, true, true, true, true, true, true, true }); //Write Coils starting with Address 5
|
|
//bool[] readCoils = modbusClient.ReadCoils(9, 10); //Read 10 Coils from Server, starting with address 10
|
|
//int[] readHoldingRegisters = currPLC.ReadHoldingRegisters(0, 34); //Read 10 Holding Registers from Server, starting with Address 1
|
|
foreach (var item in memSetR)
|
|
{
|
|
testBlockRead(item.Key, item.Value);
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Effettua decodifica aree memoria alla bitmap usata x MAPO/GWMS
|
|
/// - per lo scopo specifico IN REALTA' non conta lo stato macchina.... ma lo inviamo lo stesso
|
|
/// </summary>
|
|
protected override void decodeToBaseBitmap()
|
|
{
|
|
// init a zero...
|
|
B_input = 0;
|
|
|
|
/* -----------------------------------------------------
|
|
* bitmap MAPO STANDARD
|
|
* B0: POWER_ON
|
|
* B1: RUN
|
|
* B2: pzCount
|
|
* B3: allarme
|
|
*
|
|
----------------------------------------------------- */
|
|
|
|
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);
|
|
}
|
|
|
|
// salvo!
|
|
B_input = byteSignals;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
}
|
|
} |