This commit is contained in:
2023-02-07 13:08:25 +01:00
parent 31bf148e0f
commit 5277c3518f
6 changed files with 584 additions and 217 deletions

View File

@@ -3,14 +3,35 @@ using System.Net.Sockets;
namespace TestApp.Driver
{
internal class VestelEvc04 : IChargerDriver
internal class VestelEvc04 : IChargerDriver, IDisposable
{
private IModbusMaster? _Modbus;
private bool _Running;
private Thread _HeartbeatThread;
public string HostAddress { get; set; } = "192.168.178.100";
public int Port { get; set; } = 502;
private string _HostAddress = "192.168.178.100";
public string HostAddress
{
get => _HostAddress;
set
{
if (_HostAddress == value) { return; }
_HostAddress = value;
Disconnect();
}
}
private int _Port = 502;
public int Port
{
get => _Port;
set
{
if (_Port == value) { return; }
_Port = value;
Disconnect();
}
}
public VestelEvc04()
{
@@ -22,6 +43,7 @@ namespace TestApp.Driver
private void Connect()
{
if (!_Running) { return; }
var factory = new ModbusFactory();
var tcpClient = new TcpClient(HostAddress, Port);
_Modbus = factory.CreateMaster(tcpClient);
@@ -36,9 +58,9 @@ namespace TestApp.Driver
private void HeartbeatLoop()
{
try
while (_Running)
{
while (_Running)
try
{
if (_Modbus is null)
{
@@ -47,22 +69,24 @@ namespace TestApp.Driver
if (_Modbus is null) { continue; }
var heardbeatValue = _Modbus.ReadHoldingRegisters(255, 6000, 1)[0];
if (heardbeatValue == 0) {
if (heardbeatValue == 0)
{
_Modbus.WriteSingleRegister(255, 6000, 1);
}
Thread.Sleep(100);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
Disconnect();
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
Disconnect();
}
Thread.Sleep(100);
}
}
public void SetLoadingCurrent(double value)
{
if (!_Running) { return; }
try
{
if (_Modbus is null)
@@ -80,5 +104,11 @@ namespace TestApp.Driver
Disconnect();
}
}
public void Dispose()
{
_Running = false;
Disconnect();
}
}
}