Skip to content

Commit

Permalink
Collection initialization simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
HakamFostok committed Jan 22, 2023
1 parent 2fa67cc commit f6eb07b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 37 deletions.
7 changes: 4 additions & 3 deletions Modbus/Message/ModbusMessageImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new();

pdu.Add(FunctionCode);
List<byte> pdu = new()
{
FunctionCode
};

if (ExceptionCode.HasValue)
{
Expand Down
23 changes: 12 additions & 11 deletions Modbus/Message/SlaveExceptionResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ public override string ToString()

internal static Dictionary<byte, string> CreateExceptionMessages()
{
Dictionary<byte, string> messages = new(9);

messages.Add(1, Resources.IllegalFunction);
messages.Add(2, Resources.IllegalDataAddress);
messages.Add(3, Resources.IllegalDataValue);
messages.Add(4, Resources.SlaveDeviceFailure);
messages.Add(5, Resources.Acknowlege);
messages.Add(6, Resources.SlaveDeviceBusy);
messages.Add(8, Resources.MemoryParityError);
messages.Add(10, Resources.GatewayPathUnavailable);
messages.Add(11, Resources.GatewayTargetDeviceFailedToRespond);
Dictionary<byte, string> messages = new(9)
{
{ 1, Resources.IllegalFunction },
{ 2, Resources.IllegalDataAddress },
{ 3, Resources.IllegalDataValue },
{ 4, Resources.SlaveDeviceFailure },
{ 5, Resources.Acknowlege },
{ 6, Resources.SlaveDeviceBusy },
{ 8, Resources.MemoryParityError },
{ 10, Resources.GatewayPathUnavailable },
{ 11, Resources.GatewayTargetDeviceFailedToRespond }
};

return messages;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public byte[] MessageFrame
{
get
{
List<byte> frame = new();
frame.Add(SlaveAddress);
List<byte> frame = new() { SlaveAddress };
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
Expand All @@ -31,9 +30,10 @@ public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new();

pdu.Add(FunctionCode);
List<byte> pdu = new()
{
FunctionCode
};
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress)));
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public byte[] MessageFrame
{
get
{
List<byte> frame = new();
frame.Add(SlaveAddress);
List<byte> frame = new()
{
SlaveAddress
};
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
Expand All @@ -28,10 +30,11 @@ public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new();

pdu.Add(FunctionCode);
pdu.Add(ByteCount);
List<byte> pdu = new()
{
FunctionCode,
ByteCount
};
pdu.AddRange(_data.NetworkBytes);

return pdu.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public byte[] MessageFrame
{
get
{
List<byte> frame = new();
frame.Add(SlaveAddress);
List<byte> frame = new()
{
SlaveAddress
};
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
Expand All @@ -35,9 +37,10 @@ public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new();

pdu.Add(FunctionCode);
List<byte> pdu = new()
{
FunctionCode
};
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress)));
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints)));
pdu.Add(ByteCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ public byte[] MessageFrame
{
get
{
List<byte> frame = new();
frame.Add(SlaveAddress);
List<byte> frame = new()
{
SlaveAddress
};
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
Expand All @@ -23,9 +25,10 @@ public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new();

pdu.Add(FunctionCode);
List<byte> pdu = new()
{
FunctionCode
};
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress)));
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints)));

Expand Down
6 changes: 4 additions & 2 deletions NModbus4.UnitTests/Data/RegisterCollectionFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public void RegisterCollectionEmpty()
[Fact]
public void ModifyRegister()
{
RegisterCollection col = new(1, 2, 3, 4);
col[0] = 5;
RegisterCollection col = new(1, 2, 3, 4)
{
[0] = 5
};
}

[Fact]
Expand Down

0 comments on commit f6eb07b

Please sign in to comment.