Сначала сюда
/// <summary>
/// Write a block of 1 to 123 contiguous 16 bit holding registers.
/// </summary>
/// <param name="slaveAddress">Address of the device to write to.</param>
/// <param name="startAddress">Address to begin writing values.</param>
/// <param name="data">Values to write.</param>
public void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, ushort[] data)
{
ValidateData("data", data, 123);
var request = new WriteMultipleRegistersRequest(slaveAddress, startAddress, new RegisterCollection(data));
Transport.UnicastMessage<WriteMultipleRegistersRes ponse>(request);
}
Потом сюда
internal virtual T UnicastMessage<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 == Modbus.Acknowledge;
if (readAgain)
{
Debug.WriteLine(
"Received ACKNOWLEDGE slave exception response, waiting {0} milliseconds and retrying to read response.",
_waitToRetryMilliseconds);
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 != Modbus.SlaveDeviceBusy)
throw;
if (SlaveBusyUsesRetryCount && attempt++ > _retries)
throw;
Debug.WriteLine(
"Received SLAVE_DEVICE_BUSY exception response, waiting {0} milliseconds and resubmitting request.",
_waitToRetryMilliseconds);
Sleep(WaitToRetryMilliseconds);
}
catch (Exception e)
{
if (e is FormatException ||
e is NotImplementedException ||
e is TimeoutException ||
e is IOException)
{
Debug.WriteLine("{0}, {1} retries remaining - {2}", e.GetType().Name, _retries - attempt + 1, e);
if (attempt++ > _retries)
throw;
}
else
{
throw;
}
}
} while (!success);
return (T)response;
}
Дальше
internal override IModbusMessage ReadResponse<T>()
{
byte[] frameStart = Read(ResponseFrameStartLength);
byte[] frameEnd = Read(ResponseBytesToRead(frameStart));
byte[] frame = Enumerable.Concat(frameStart, frameEnd).ToArray();
Debug.WriteLine("RX: {0}", string.Join(", ", frame));
return CreateResponse<T>(frame);
}
Дальше
public virtual byte[] Read(int count)
{
byte[] frameBytes = new byte[count];
int numBytesRead = 0;
while (numBytesRead != count)
numBytesRead += StreamResource.Read(frameBytes, numBytesRead, count - numBytesRead);
return frameBytes;
}
дальше сюда
public int Read(byte[] buffer, int offset, int count)
{
try
{
return _serialPort.Read(buffer, offset, count);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace, ex.Message, ex.InnerException);
}
return 0;
}
и тут вылетают Exception и застревает, потому как прошлая функция ждет ответ величиной 4