Skip to content

Commit

Permalink
Delete comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Magniveo committed Nov 17, 2024
1 parent 2c04676 commit 3e07f11
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 281 deletions.
14 changes: 4 additions & 10 deletions NModbus.Serial/SerialPortAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ public void DiscardInBuffer()

public int Read(byte[] buffer, int offset, int count)
{
//var bytesRead = ReadAsync(buffer, offset, count,_serialPort.ReadTimeout).Result;
//return bytesRead;
//Task.Run(() => {Thread.Sleep(100) ;});
//return _serialPort.BaseStream.Read(buffer, offset, count);
//return (int)_serialPort.BaseStream?.Read(buffer, offset, count);
//_serialPort.BaseStream.BeginRead(buffer, offset, count)
return _serialPort.BaseStream.Read(buffer, offset, count);
}

Expand All @@ -61,12 +55,12 @@ public async Task<int> ReadAsync(byte[] buffer, int offset, int count)
await Task.Delay(50);
CancellationTokenSource cts = new CancellationTokenSource(_serialPort.ReadTimeout);
//var task = await _serialPort.BaseStream.ReadAsync(buffer, offset, count);
Task<int> readTask = Task.Run(async () =>
var readTask = Task.Run(async () =>
{
return await _serialPort.BaseStream.ReadAsync(buffer, offset, count, cts.Token);
}, cts.Token);

Task completedTask = await Task.WhenAny(readTask, Task.Delay(_serialPort.ReadTimeout, cts.Token));
var completedTask = await Task.WhenAny(readTask, Task.Delay(_serialPort.ReadTimeout, cts.Token));
if (completedTask == readTask)
{
return await readTask;
Expand Down Expand Up @@ -96,10 +90,10 @@ public async Task<int> ReadAsync(byte[] buffer, int offset, int count, int timeo
cts.Token.ThrowIfCancellationRequested();

// Otherwise, return the number of bytes read
var bytesRead = await readTask;
//var bytesRead = await readTask;

//await _serialPort.BaseStream.FlushAsync();
return bytesRead;
return await readTask;
}
catch (OperationCanceledException)
{
Expand Down
271 changes: 0 additions & 271 deletions NModbus/IO/ModbusTransport.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading;
Expand Down Expand Up @@ -116,276 +115,6 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public virtual T UnicastMessageOldNew<T>(IModbusMessage message)
where T : IModbusMessage, new()
{
IModbusMessage response = null;
int attempt = 1;
bool success = false;

do
{
try
{
lock (_syncLock)
{
Write(message);

bool readAgain;
do
{
readAgain = false;
response = ReadResponse<T>();
var exceptionResponse = response as SlaveExceptionResponse;

if (exceptionResponse != null)
{
// if SlaveExceptionCode == ACKNOWLEDGE we retry reading the response without resubmitting request
readAgain = exceptionResponse.SlaveExceptionCode == SlaveExceptionCodes.Acknowledge;

if (readAgain)
{
Logger.Debug($"Received ACKNOWLEDGE slave exception response, waiting {_waitToRetryMilliseconds} milliseconds and retrying to read response.");
Sleep(WaitToRetryMilliseconds);
}
else
{
throw new SlaveException(exceptionResponse);
}
}
else if (ShouldRetryResponse(message, response))
{
readAgain = true;
}
}
while (readAgain);
}

ValidateResponse(message, response);
success = true;
}
catch (SlaveException se)
{
if (se.SlaveExceptionCode != SlaveExceptionCodes.SlaveDeviceBusy)
{
throw;
}

if (SlaveBusyUsesRetryCount && attempt++ > _retries)
{
throw;
}

Logger.Warning($"Received SLAVE_DEVICE_BUSY exception response, waiting {_waitToRetryMilliseconds} milliseconds and resubmitting request.");

Sleep(WaitToRetryMilliseconds);
}
catch (Exception e)
{
switch (e)
{
case SocketException socketTimeoutException:
// Handle socket timeouts
Logger.Error($"Socket timeout occurred: {socketTimeoutException.Message}");
break;
case FormatException _:
case NotImplementedException _:
case TimeoutException _:
case NullReferenceException _:
case OperationCanceledException _:
case InvalidOperationException _:
case IOException _:
// Handle other types of exceptions
Logger.Error($"{e.GetType().Name}, {(_retries - attempt + 1)} retries remaining - {e}");

if (attempt++ > _retries)
{
throw;
}

Sleep(WaitToRetryMilliseconds);
break;
default:
// Handle other exceptions
throw;
}
}
}
while (!success);

return (T)response;
}
public virtual T UnicastMessageProblemGiga2<T>(IModbusMessage message)
where T : IModbusMessage, new()
{
IModbusMessage response = null;
int attempts = 0;

lock (_syncLock)
{
while (response == null && attempts < _retries)
{
try
{
Write(message);
response = ReadResponse<T>();
}
catch (SlaveException se) when (se.SlaveExceptionCode != SlaveExceptionCodes.SlaveDeviceBusy)
{
throw;
}
catch (Exception e)
{
if (e is SocketException || e is FormatException || e is NotImplementedException || e is TimeoutException || e is IOException || e is TaskCanceledException)
{
Logger.Error($"{e.GetType().Name}, {(_retries - ++attempts)} retries remaining - {e}");
}
else
{
throw;
}
}
}

ValidateResponse(message, response);
}

return (T)response;
}
public virtual T UnicastMessageProblemGiga<T>(IModbusMessage message)
where T : IModbusMessage, new()
{
IModbusMessage response = null;
int attempts = 0;

while (response == null && attempts < _retries)
{
try
{
lock (_syncLock)
{
Write(message);
response = ReadResponse<T>();
}

ValidateResponse(message, response);
}
catch (SlaveException se) when (se.SlaveExceptionCode != SlaveExceptionCodes.SlaveDeviceBusy)
{
throw;
}
catch (Exception e)
{
if (e is SocketException || e is FormatException || e is NotImplementedException || e is TimeoutException || e is IOException)
{
Logger.Error($"{e.GetType().Name}, {(_retries - ++attempts)} retries remaining - {e}");
}
else
{
throw;
}
}
}

return (T)response;
}
public virtual T UnicastMessageProblem<T>(IModbusMessage message)
where T : IModbusMessage, new()
{
IModbusMessage response = null;
int attempt = 1;
bool success = false;

do
{
try
{
lock (_syncLock)
{
Write(message);

bool readAgain;
do
{
readAgain = false;
response = ReadResponse<T>();
var exceptionResponse = response as SlaveExceptionResponse;

if (exceptionResponse != null)
{
// if SlaveExceptionCode == ACKNOWLEDGE we retry reading the response without resubmitting request
readAgain = exceptionResponse.SlaveExceptionCode == SlaveExceptionCodes.Acknowledge;

if (readAgain)
{
Logger.Debug($"Received ACKNOWLEDGE slave exception response, waiting {_waitToRetryMilliseconds} milliseconds and retrying to read response.");
Sleep(WaitToRetryMilliseconds);
}
else
{
throw new SlaveException(exceptionResponse);
}
}
else if (ShouldRetryResponse(message, response))
{
readAgain = true;
}
}
while (readAgain);
}

ValidateResponse(message, response);
success = true;
}
catch (SlaveException se)
{
if (se.SlaveExceptionCode != SlaveExceptionCodes.SlaveDeviceBusy)
{
throw;
}

if (SlaveBusyUsesRetryCount && attempt++ > _retries)
{
throw;
}

Logger.Warning($"Received SLAVE_DEVICE_BUSY exception response, waiting {_waitToRetryMilliseconds} milliseconds and resubmitting request.");

Sleep(WaitToRetryMilliseconds);
}
catch (Exception e)
{
if ((e is SocketException socketException && socketException.SocketErrorCode != SocketError.TimedOut)
|| (e.InnerException is SocketException innerSocketException && innerSocketException.SocketErrorCode != SocketError.TimedOut))
{
throw;
}
if (e is FormatException ||
e is NotImplementedException ||
e is TimeoutException ||
e is IOException ||
e is SocketException)
{
Logger.Error($"{e.GetType().Name}, {(_retries - attempt + 1)} retries remaining - {e}");

if (attempt++ > _retries)
{
throw;
}

Sleep(WaitToRetryMilliseconds);
}
else
{
throw;
}
}
}
while (!success);

return (T)response;
}

public virtual T UnicastMessage<T>(IModbusMessage message)
where T : IModbusMessage, new()
{
Expand Down

0 comments on commit 3e07f11

Please sign in to comment.