Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Aug 23, 2024
1 parent 98fcaf1 commit 51664a6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 52 deletions.
11 changes: 4 additions & 7 deletions src/FluentModbus/Client/ModbusRtuClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
{
// WARNING: IF YOU EDIT THIS METHOD, REFLECT ALL CHANGES ALSO IN TransceiveFrameAsync!

int frameLength;
byte rawFunctionCode;
ushort crc;

// build request
if (!(0 <= unitIdentifier && unitIdentifier <= 247))
throw new ModbusException(ErrorMessage.ModbusClient_InvalidUnitIdentifier);
Expand All @@ -177,10 +173,11 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
_frameBuffer.Writer.Seek(0, SeekOrigin.Begin);
_frameBuffer.Writer.Write(unitIdentifier); // 00 Unit Identifier
extendFrame(_frameBuffer.Writer);
frameLength = (int)_frameBuffer.Writer.BaseStream.Position;

var frameLength = (int)_frameBuffer.Writer.BaseStream.Position;

// add CRC
crc = ModbusUtils.CalculateCRC(_frameBuffer.Buffer.AsMemory()[..frameLength]);
var crc = ModbusUtils.CalculateCRC(_frameBuffer.Buffer.AsMemory()[..frameLength]);
_frameBuffer.Writer.Write(crc);
frameLength = (int)_frameBuffer.Writer.BaseStream.Position;

Expand Down Expand Up @@ -214,7 +211,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
}

_ = _frameBuffer.Reader.ReadByte();
rawFunctionCode = _frameBuffer.Reader.ReadByte();
var rawFunctionCode = _frameBuffer.Reader.ReadByte();

if (rawFunctionCode == (byte)ModbusFunctionCode.Error + (byte)functionCode)
ProcessError(functionCode, (ModbusExceptionCode)_frameBuffer.Buffer[2]);
Expand Down
2 changes: 1 addition & 1 deletion src/FluentModbus/Client/ModbusRtuClientAsync.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var match = Regex.Matches(csstring, @"(protected override Span<byte> TransceiveFrame\()(.*?)\)(.*?\n })", RegexOptions.Singleline)[0];

#>
/* This is automatically translated code. */
/* This is automatically translated code. */

namespace FluentModbus
{
Expand Down
23 changes: 8 additions & 15 deletions src/FluentModbus/Client/ModbusRtuOverTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class ModbusRtuOverTcpClient : ModbusClient, IDisposable
/// </summary>
public ModbusRtuOverTcpClient()
{
//
}

#endregion
Expand Down Expand Up @@ -201,18 +202,9 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
{
// WARNING: IF YOU EDIT THIS METHOD, REFLECT ALL CHANGES ALSO IN TransceiveFrameAsync!

int frameLength;
byte rawFunctionCode;

ModbusFrameBuffer frameBuffer;
ExtendedBinaryWriter writer;
ExtendedBinaryReader reader;

frameBuffer = _frameBuffer;
writer = _frameBuffer.Writer;
reader = _frameBuffer.Reader;

ushort crc;
var frameBuffer = _frameBuffer;
var writer = _frameBuffer.Writer;
var reader = _frameBuffer.Reader;

// build request
if (!(0 <= unitIdentifier && unitIdentifier <= 247))
Expand All @@ -238,10 +230,11 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
writer.Seek(0, SeekOrigin.Begin);
writer.Write(unitIdentifier); // 00 Unit Identifier
extendFrame(writer);
frameLength = (int)writer.BaseStream.Position;

var frameLength = (int)writer.BaseStream.Position;

// add CRC
crc = ModbusUtils.CalculateCRC(frameBuffer.Buffer.AsMemory()[..frameLength]);
var crc = ModbusUtils.CalculateCRC(frameBuffer.Buffer.AsMemory()[..frameLength]);
writer.Write(crc);
frameLength = (int)writer.BaseStream.Position;

Expand Down Expand Up @@ -307,7 +300,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
}

_ = reader.ReadByte();
rawFunctionCode = reader.ReadByte();
var rawFunctionCode = reader.ReadByte();

if (rawFunctionCode == (byte)ModbusFunctionCode.Error + (byte)functionCode)
ProcessError(functionCode, (ModbusExceptionCode)_frameBuffer.Buffer[2]);
Expand Down
5 changes: 0 additions & 5 deletions src/FluentModbus/Client/ModbusRtuOverTcpClientAsync.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@






/* This is automatically translated code. */

namespace FluentModbus
Expand Down
2 changes: 1 addition & 1 deletion src/FluentModbus/Client/ModbusRtuOverTcpClientAsync.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var match = Regex.Matches(csstring, @"(protected override Span<byte> TransceiveFrame\()(.*?)\)(.*?\n })", RegexOptions.Singleline)[0];

#>
/* This is automatically translated code. */
/* This is automatically translated code. */

namespace FluentModbus
{
Expand Down
32 changes: 10 additions & 22 deletions src/FluentModbus/Client/ModbusTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,16 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
{
// WARNING: IF YOU EDIT THIS METHOD, REFLECT ALL CHANGES ALSO IN TransceiveFrameAsync!

int frameLength;
int partialLength;
ushort bytesFollowing = 0;

ushort protocolIdentifier;
ushort bytesFollowing;

byte rawFunctionCode;

bool isParsed;

ModbusFrameBuffer frameBuffer;
ExtendedBinaryWriter writer;
ExtendedBinaryReader reader;

bytesFollowing = 0;
frameBuffer = _frameBuffer;
writer = _frameBuffer.Writer;
reader = _frameBuffer.Reader;
var frameBuffer = _frameBuffer;
var writer = _frameBuffer.Writer;
var reader = _frameBuffer.Reader;

// build request
writer.Seek(7, SeekOrigin.Begin);
extendFrame(writer);
frameLength = (int)writer.BaseStream.Position;
var frameLength = (int)writer.BaseStream.Position;

writer.Seek(0, SeekOrigin.Begin);

Expand All @@ -238,6 +225,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
writer.WriteReverse((ushort)0); // 02-03 Protocol Identifier
writer.WriteReverse((ushort)(frameLength - 6)); // 04-05 Length
}

else
{
writer.Write(GetTransactionIdentifier()); // 00-01 Transaction Identifier
Expand All @@ -252,7 +240,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio

// wait for and process response
frameLength = 0;
isParsed = false;
var isParsed = false;
reader.BaseStream.Seek(0, SeekOrigin.Begin);

while (true)
Expand All @@ -266,7 +254,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
// ASYNC-ONLY: {
// ASYNC-ONLY: try
// ASYNC-ONLY: {
partialLength = _networkStream.Read(frameBuffer.Buffer, frameLength, frameBuffer.Buffer.Length - frameLength);
var partialLength = _networkStream.Read(frameBuffer.Buffer, frameLength, frameBuffer.Buffer.Length - frameLength);
// ASYNC-ONLY: }
// ASYNC-ONLY: catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
// ASYNC-ONLY: {
Expand Down Expand Up @@ -302,7 +290,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
{
// read MBAP header
_ = reader.ReadUInt16Reverse(); // 00-01 Transaction Identifier
protocolIdentifier = reader.ReadUInt16Reverse(); // 02-03 Protocol Identifier
var protocolIdentifier = reader.ReadUInt16Reverse(); // 02-03 Protocol Identifier
bytesFollowing = reader.ReadUInt16Reverse(); // 04-05 Length
_ = reader.ReadByte(); // 06 Unit Identifier

Expand All @@ -318,7 +306,7 @@ protected override Span<byte> TransceiveFrame(byte unitIdentifier, ModbusFunctio
}
}

rawFunctionCode = reader.ReadByte();
var rawFunctionCode = reader.ReadByte();

if (rawFunctionCode == (byte)ModbusFunctionCode.Error + (byte)functionCode)
ProcessError(functionCode, (ModbusExceptionCode)frameBuffer.Buffer[8]);
Expand Down
2 changes: 1 addition & 1 deletion src/FluentModbus/Client/ModbusTcpClientAsync.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
var match = Regex.Matches(csstring, @"(protected override Span<byte> TransceiveFrame\()(.*?)\)(.*?\n })", RegexOptions.Singleline)[0];

#>
/* This is automatically translated code. */
/* This is automatically translated code. */

namespace FluentModbus
{
Expand Down

0 comments on commit 51664a6

Please sign in to comment.