2018-01-03 Update 1 Fix reconnect
This commit is contained in:
@@ -260,14 +260,10 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
if (Linkers.Values.Count(p => p == _com) <= 1)
|
if (Linkers.Values.Count(p => p == _com) <= 1)
|
||||||
{
|
{
|
||||||
try
|
if (SerialPort.IsOpen)
|
||||||
{
|
{
|
||||||
SerialPort.Close();
|
SerialPort.Close();
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
|
||||||
//ignore
|
|
||||||
}
|
|
||||||
SerialPort.Dispose();
|
SerialPort.Dispose();
|
||||||
Log.Information("Com interface {Com} Disposed", _com);
|
Log.Information("Com interface {Com} Disposed", _com);
|
||||||
Connectors[_com] = null;
|
Connectors[_com] = null;
|
||||||
@@ -364,6 +360,7 @@ namespace Modbus.Net
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.Error(e, "Com client {ConnectionToken} connect error", ConnectionToken);
|
Log.Error(e, "Com client {ConnectionToken} connect error", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,11 +96,7 @@ namespace Modbus.Net
|
|||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
{
|
{
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
#if NET40 || NET45 || NET451 || NET452
|
_socketClient = null;
|
||||||
_socketClient.Close();
|
|
||||||
#else
|
|
||||||
_socketClient.Dispose();
|
|
||||||
#endif
|
|
||||||
Log.Debug("Tcp client {ConnectionToken} Disposed", ConnectionToken);
|
Log.Debug("Tcp client {ConnectionToken} Disposed", ConnectionToken);
|
||||||
}
|
}
|
||||||
m_disposed = true;
|
m_disposed = true;
|
||||||
@@ -123,7 +119,8 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
{
|
{
|
||||||
return true;
|
if (_socketClient.Connected)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -133,16 +130,10 @@ namespace Modbus.Net
|
|||||||
ReceiveTimeout = TimeoutTime
|
ReceiveTimeout = TimeoutTime
|
||||||
};
|
};
|
||||||
|
|
||||||
try
|
var cts = new CancellationTokenSource();
|
||||||
{
|
cts.CancelAfter(TimeoutTime);
|
||||||
var cts = new CancellationTokenSource();
|
await _socketClient.ConnectAsync(_host, _port).WithCancellation(cts.Token);
|
||||||
cts.CancelAfter(TimeoutTime);
|
|
||||||
await _socketClient.ConnectAsync(_host, _port).WithCancellation(cts.Token);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Error(e, "Tcp client {ConnectionToken} connect error", ConnectionToken);
|
|
||||||
}
|
|
||||||
if (_socketClient.Connected)
|
if (_socketClient.Connected)
|
||||||
{
|
{
|
||||||
_taskCancel = false;
|
_taskCancel = false;
|
||||||
@@ -152,11 +143,13 @@ namespace Modbus.Net
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken);
|
Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken);
|
Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,9 +242,9 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
//主动传输事件
|
//主动传输事件
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
RefreshReceiveCount();
|
RefreshReceiveCount();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
catch (ObjectDisposedException)
|
||||||
|
|||||||
@@ -83,12 +83,8 @@ namespace Modbus.Net
|
|||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
{
|
{
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
#if NET40 || NET45 || NET451 || NET452
|
_socketClient = null;
|
||||||
_socketClient.Close();
|
Log.Debug("Udp client {ConnectionToken} Disposed", ConnectionToken);
|
||||||
#else
|
|
||||||
_socketClient.Dispose();
|
|
||||||
#endif
|
|
||||||
Log.Debug("Tcp client {ConnectionToken} Disposed", ConnectionToken);
|
|
||||||
}
|
}
|
||||||
m_disposed = true;
|
m_disposed = true;
|
||||||
}
|
}
|
||||||
@@ -116,30 +112,27 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
_socketClient = new UdpClient();
|
_socketClient = new UdpClient();
|
||||||
|
|
||||||
try
|
var cts = new CancellationTokenSource();
|
||||||
{
|
cts.CancelAfter(TimeoutTime);
|
||||||
var cts = new CancellationTokenSource();
|
await Task.Run(() => _socketClient.Connect(_host, _port), cts.Token);
|
||||||
cts.CancelAfter(TimeoutTime);
|
|
||||||
await Task.Run(() => _socketClient.Connect(_host, _port), cts.Token);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Error(e, "Tcp client {ConnectionToken} connect error", ConnectionToken);
|
|
||||||
}
|
|
||||||
if (_socketClient.Client.Connected)
|
if (_socketClient.Client.Connected)
|
||||||
{
|
{
|
||||||
_taskCancel = false;
|
_taskCancel = false;
|
||||||
Controller.SendStart();
|
Controller.SendStart();
|
||||||
ReceiveMsgThreadStart();
|
ReceiveMsgThreadStart();
|
||||||
Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken);
|
Log.Information("Udp client {ConnectionToken} connected", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken);
|
|
||||||
|
Log.Error("Udp client {ConnectionToken} connect failed.", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken);
|
Log.Error(err, "Udp client {ConnectionToken} connect exception", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,12 +147,12 @@ namespace Modbus.Net
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Dispose();
|
Dispose();
|
||||||
Log.Information("Tcp client {ConnectionToken} disconnected successfully", ConnectionToken);
|
Log.Information("Udp client {ConnectionToken} disconnected successfully", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} disconnected exception", ConnectionToken);
|
Log.Error(err, "Udp client {ConnectionToken} disconnected exception", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -180,13 +173,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
RefreshSendCount();
|
RefreshSendCount();
|
||||||
|
|
||||||
Log.Verbose("Tcp client {ConnectionToken} send text len = {Length}", ConnectionToken, datagram.Length);
|
Log.Verbose("Udp client {ConnectionToken} send text len = {Length}", ConnectionToken, datagram.Length);
|
||||||
Log.Verbose($"Tcp client {ConnectionToken} send: {String.Concat(datagram.Select(p => " " + p.ToString("X2")))}");
|
Log.Verbose($"Udp client {ConnectionToken} send: {String.Concat(datagram.Select(p => " " + p.ToString("X2")))}");
|
||||||
await _socketClient.SendAsync(datagram, datagram.Length);
|
await _socketClient.SendAsync(datagram, datagram.Length);
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} send exception", ConnectionToken);
|
Log.Error(err, "Udp client {ConnectionToken} send exception", ConnectionToken);
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,19 +214,19 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
if (receive.Buffer.Clone() is byte[] receiveBytes)
|
if (receive.Buffer.Clone() is byte[] receiveBytes)
|
||||||
{
|
{
|
||||||
Log.Verbose("Tcp client {ConnectionToken} receive text len = {Length}", ConnectionToken,
|
Log.Verbose("Udp client {ConnectionToken} receive text len = {Length}", ConnectionToken,
|
||||||
receiveBytes.Length);
|
receiveBytes.Length);
|
||||||
Log.Verbose(
|
Log.Verbose(
|
||||||
$"Tcp client {ConnectionToken} receive: {String.Concat(receiveBytes.Select(p => " " + p.ToString("X2")))}");
|
$"Udp client {ConnectionToken} receive: {String.Concat(receiveBytes.Select(p => " " + p.ToString("X2")))}");
|
||||||
var isMessageConfirmed = Controller.ConfirmMessage(receiveBytes);
|
var isMessageConfirmed = Controller.ConfirmMessage(receiveBytes);
|
||||||
if (isMessageConfirmed == false)
|
if (isMessageConfirmed == false)
|
||||||
{
|
{
|
||||||
//主动传输事件
|
//主动传输事件
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
RefreshReceiveCount();
|
RefreshReceiveCount();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (ObjectDisposedException)
|
catch (ObjectDisposedException)
|
||||||
@@ -242,7 +235,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
Log.Error(err, "Tcp client {ConnectionToken} receive exception", ConnectionToken);
|
Log.Error(err, "Udp client {ConnectionToken} receive exception", ConnectionToken);
|
||||||
//CloseClientSocket();
|
//CloseClientSocket();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,19 +243,19 @@ namespace Modbus.Net
|
|||||||
private void RefreshSendCount()
|
private void RefreshSendCount()
|
||||||
{
|
{
|
||||||
_sendCount++;
|
_sendCount++;
|
||||||
Log.Verbose("Tcp client {ConnectionToken} send count: {SendCount}", ConnectionToken, _sendCount);
|
Log.Verbose("Udp client {ConnectionToken} send count: {SendCount}", ConnectionToken, _sendCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshReceiveCount()
|
private void RefreshReceiveCount()
|
||||||
{
|
{
|
||||||
_receiveCount++;
|
_receiveCount++;
|
||||||
Log.Verbose("Tcp client {ConnectionToken} receive count: {SendCount}", ConnectionToken, _receiveCount);
|
Log.Verbose("Udp client {ConnectionToken} receive count: {SendCount}", ConnectionToken, _receiveCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshErrorCount()
|
private void RefreshErrorCount()
|
||||||
{
|
{
|
||||||
_errorCount++;
|
_errorCount++;
|
||||||
Log.Verbose("Tcp client {ConnectionToken} error count: {ErrorCount}", ConnectionToken, _errorCount);
|
Log.Verbose("Udp client {ConnectionToken} error count: {ErrorCount}", ConnectionToken, _errorCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CloseClientSocket()
|
private void CloseClientSocket()
|
||||||
@@ -280,7 +273,11 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Error(ex, "Tcp client {ConnectionToken} client close exception", ConnectionToken);
|
Log.Error(ex, "Udp client {ConnectionToken} client close exception", ConnectionToken);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_socketClient = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Modbus.Net.PersistedTests
|
|||||||
{
|
{
|
||||||
Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().CreateLogger();
|
Log.Logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().CreateLogger();
|
||||||
|
|
||||||
IMachineProperty<int> machine = new ModbusMachine<int, string>(1, ModbusType.Rtu, "COM1",
|
IMachineProperty<int> machine = new ModbusMachine<int, string>(1, ModbusType.Tcp, "127.0.0.1",
|
||||||
new List<AddressUnit>()
|
new List<AddressUnit>()
|
||||||
{
|
{
|
||||||
new AddressUnit()
|
new AddressUnit()
|
||||||
@@ -40,65 +40,9 @@ namespace Modbus.Net.PersistedTests
|
|||||||
DataType = typeof(ushort)
|
DataType = typeof(ushort)
|
||||||
},
|
},
|
||||||
}, true, 2, 1);
|
}, true, 2, 1);
|
||||||
IMachineProperty<int> machine2 = new ModbusMachine<int, string>(2, ModbusType.Rtu, "COM1",
|
|
||||||
new List<AddressUnit>()
|
|
||||||
{
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "1",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 11,
|
|
||||||
Name = "test 1",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "2",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 12,
|
|
||||||
Name = "test 2",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "3",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 13,
|
|
||||||
Name = "test 3",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
}, true, 3, 1);
|
|
||||||
IMachineProperty<int> machine3 = new ModbusMachine<int, string>(3, ModbusType.Rtu, "COM1",
|
|
||||||
new List<AddressUnit>()
|
|
||||||
{
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "1",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 21,
|
|
||||||
Name = "test 1",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "2",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 22,
|
|
||||||
Name = "test 2",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
new AddressUnit()
|
|
||||||
{
|
|
||||||
Id = "3",
|
|
||||||
Area = "4X",
|
|
||||||
Address = 23,
|
|
||||||
Name = "test 3",
|
|
||||||
DataType = typeof(ushort)
|
|
||||||
},
|
|
||||||
}, true, 4, 1);
|
|
||||||
|
|
||||||
TaskManager<int> manager = new TaskManager<int>(20, true);
|
TaskManager<int> manager = new TaskManager<int>(20, true);
|
||||||
manager.AddMachines<string>(new List<IMachineProperty<int>> { machine, machine2, machine3 });
|
manager.AddMachines<string>(new List<IMachineProperty<int>> { machine });
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
manager.InvokeTimerForMachine(1, new TaskItemSetData(() => new Dictionary<string, double>
|
manager.InvokeTimerForMachine(1, new TaskItemSetData(() => new Dictionary<string, double>
|
||||||
{
|
{
|
||||||
@@ -112,38 +56,16 @@ namespace Modbus.Net.PersistedTests
|
|||||||
"4X 3.0", r.Next() % 65536
|
"4X 3.0", r.Next() % 65536
|
||||||
},
|
},
|
||||||
}, MachineSetDataType.Address, 10000, 10000));
|
}, MachineSetDataType.Address, 10000, 10000));
|
||||||
manager.InvokeTimerForMachine(2, new TaskItemSetData(() => new Dictionary<string, double>
|
|
||||||
{
|
|
||||||
{
|
|
||||||
"4X 11.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"4X 12.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"4X 13.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
}, MachineSetDataType.Address, 10000, 10000));
|
|
||||||
manager.InvokeTimerForMachine(3, new TaskItemSetData(() => new Dictionary<string, double>
|
|
||||||
{
|
|
||||||
{
|
|
||||||
"4X 21.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"4X 22.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"4X 23.0", r.Next() % 65536
|
|
||||||
},
|
|
||||||
}, MachineSetDataType.Address, 10000, 10000));
|
|
||||||
Thread.Sleep(5000);
|
Thread.Sleep(5000);
|
||||||
manager.InvokeTimerAll(new TaskItemGetData(data =>
|
manager.InvokeTimerAll(new TaskItemGetData(data =>
|
||||||
{
|
{
|
||||||
foreach (var dataInner in data.ReturnValues)
|
if (data?.ReturnValues != null)
|
||||||
{
|
{
|
||||||
Console.WriteLine(dataInner.Key + " " + dataInner.Value.PlcValue);
|
foreach (var dataInner in data.ReturnValues)
|
||||||
|
{
|
||||||
|
Console.WriteLine(dataInner.Key + " " + dataInner.Value.PlcValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}, MachineGetDataType.Address, 10000, 10000));
|
}, MachineGetDataType.Address, 10000, 10000));
|
||||||
|
|
||||||
Console.Read();
|
Console.Read();
|
||||||
|
|||||||
Reference in New Issue
Block a user