116 lines
4.2 KiB
C#
116 lines
4.2 KiB
C#
using EasyModbus;
|
|
using IOB_UT_NEXT;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IOB_WIN_NEXT
|
|
{
|
|
public class IobModbusTCP : IobGeneric
|
|
{
|
|
#region Protected Fields
|
|
|
|
protected string serverIp = "hampizzaferri.dyndns.org";
|
|
|
|
#endregion Protected Fields
|
|
|
|
//protected string serverIp = "127.0.0.1";
|
|
|
|
#region Public Constructors
|
|
|
|
/// Classe base con i metodi x Siemens
|
|
/// </summary>
|
|
/// <param name="caller"></param>
|
|
/// <param name="adpConf"></param>
|
|
public IobModbusTCP(AdapterForm caller, IobConfiguration IOBConf) : base(caller, IOBConf)
|
|
{
|
|
lgInfo("NEW IOB ModBus TCP");
|
|
|
|
testRead();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Private Methods
|
|
|
|
private void testConf()
|
|
{
|
|
BaseModbusConf.VarConf var01 = new BaseModbusConf.VarConf()
|
|
{
|
|
Name = "Giacenza Serbatoio",
|
|
Type = BaseModbusConf.DataType.Real,
|
|
MemAddr = 1,
|
|
Size = 2,
|
|
Direction = BaseModbusConf.MemDirection.R,
|
|
RangeMin = 0,
|
|
RangeMax = 100
|
|
};
|
|
BaseModbusConf.VarConf var02 = new BaseModbusConf.VarConf()
|
|
{
|
|
Name = "Pressione Serbatoio",
|
|
Type = BaseModbusConf.DataType.Real,
|
|
MemAddr = 3,
|
|
Size = 2,
|
|
Direction = BaseModbusConf.MemDirection.R,
|
|
RangeMin = 0,
|
|
RangeMax = 25
|
|
};
|
|
List<BaseModbusConf.VarConf> memList01 = new List<BaseModbusConf.VarConf>();
|
|
memList01.Add(var01);
|
|
memList01.Add(var02);
|
|
BaseModbusConf.ModbusMemArea block01 = new BaseModbusConf.ModbusMemArea()
|
|
{
|
|
RawAddr = "40001",
|
|
Type = BaseModbusConf.MemType.HoldingRegister,
|
|
BaseAddr = 1,
|
|
Size = 50,
|
|
VarList = memList01
|
|
};
|
|
List<BaseModbusConf.ModbusMemArea> DemoMem = new List<BaseModbusConf.ModbusMemArea>();
|
|
DemoMem.Add(block01);
|
|
|
|
// creo un area di memoria
|
|
BaseModbusConf demoConf = new BaseModbusConf()
|
|
{
|
|
MemoryList = DemoMem
|
|
};
|
|
// salvo conf con json x compilare...
|
|
var rawData = JsonConvert.SerializeObject(demoConf);
|
|
}
|
|
|
|
private void testRead()
|
|
{
|
|
//Ip-Address and Port of Modbus-TCP-Server
|
|
ModbusClient modbusClient = new ModbusClient(serverIp, 502);
|
|
//Connect to Server
|
|
modbusClient.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 = modbusClient.ReadHoldingRegisters(0, 34); //Read 10 Holding Registers from Server, starting with Address 1
|
|
|
|
int[] readHR1000 = modbusClient.ReadHoldingRegisters(0, 100); //Read 10 Holding Registers from Server, starting with Address 1
|
|
|
|
//Disconnect from Server
|
|
modbusClient.Disconnect();
|
|
|
|
//// Console Output
|
|
//for (int i = 0; i < readCoils.Length; i++)
|
|
// Console.WriteLine("Value of Coil " + (9 + i + 1) + " " + readCoils[i].ToString());
|
|
|
|
for (int i = 0; i < readHoldingRegisters.Length; i++)
|
|
{
|
|
Console.WriteLine("Value of HoldingRegister " + (i + 1) + " " + readHoldingRegisters[i].ToString());
|
|
int[] thisSet = new int[2];
|
|
Array.Copy(readHoldingRegisters, i, thisSet, 0, 2);
|
|
Console.WriteLine("Convert val HoldingRegister " + (i + 1) + " " + ModbusClient.ConvertRegistersToFloat(thisSet));
|
|
}
|
|
Console.Write("Press any key to continue . . . ");
|
|
Console.ReadKey(true);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |