Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
HakamFostok committed Jun 27, 2021
1 parent ee28971 commit 5dee6ce
Show file tree
Hide file tree
Showing 72 changed files with 5,467 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Modbus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NModbus4.IntegrationTests", "NModbus4.IntegrationTests\NModbus4.IntegrationTests.csproj", "{1AA055F2-68E1-409F-A744-6081D7A27A57}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NModbus4.TestDriver", "NModbus4.TestDriver\NModbus4.TestDriver.csproj", "{7E6D4E84-848F-444C-AD5C-852D0665E091}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NModbus4.UnitTests", "NModbus4.UnitTests\NModbus4.UnitTests.csproj", "{099155AF-8348-4829-B959-2F83AFCDD4E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,6 +28,18 @@ Global
{027A98D6-977B-4077-AD9B-8DCB05A8E60A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{027A98D6-977B-4077-AD9B-8DCB05A8E60A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{027A98D6-977B-4077-AD9B-8DCB05A8E60A}.Release|Any CPU.Build.0 = Release|Any CPU
{1AA055F2-68E1-409F-A744-6081D7A27A57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AA055F2-68E1-409F-A744-6081D7A27A57}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AA055F2-68E1-409F-A744-6081D7A27A57}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AA055F2-68E1-409F-A744-6081D7A27A57}.Release|Any CPU.Build.0 = Release|Any CPU
{7E6D4E84-848F-444C-AD5C-852D0665E091}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E6D4E84-848F-444C-AD5C-852D0665E091}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E6D4E84-848F-444C-AD5C-852D0665E091}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E6D4E84-848F-444C-AD5C-852D0665E091}.Release|Any CPU.Build.0 = Release|Any CPU
{099155AF-8348-4829-B959-2F83AFCDD4E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{099155AF-8348-4829-B959-2F83AFCDD4E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{099155AF-8348-4829-B959-2F83AFCDD4E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{099155AF-8348-4829-B959-2F83AFCDD4E3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 16 additions & 3 deletions Modbus/Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
<Description>This library is driven from this library https://github.com/NModbus4/NModbus4. It just target .NET Core instead of .NET Framework</Description>
<PackageProjectUrl>https://github.com/HakamFostok/NModbus4.NetCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/HakamFostok/NModbus4.NetCore</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<!--<PackageLicenseFile>LICENSE</PackageLicenseFile>-->
<AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.3.0.0</FileVersion>
<Version>1.3.0</Version>
</PropertyGroup>

<PropertyGroup>
<!--https://devblogs.microsoft.com/dotnet/producing-packages-with-source-link/-->

<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
Expand Down Expand Up @@ -44,5 +44,18 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>


<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>NModbus4.IntegrationTests</_Parameter1>
</AssemblyAttribute>

<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>NModbus4.TestDriver</_Parameter1>
</AssemblyAttribute>

<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>NModbus4.UnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Net;
using Modbus.Message;

namespace Modbus.IntegrationTests.CustomMessages
{
public class CustomReadHoldingRegistersRequest : IModbusMessage
{
private byte _functionCode;
private byte _slaveAddress;
private ushort _startAddress;
private ushort _numberOfPoints;
private ushort _transactionId;

public CustomReadHoldingRegistersRequest(byte functionCode, byte slaveAddress, ushort startAddress, ushort numberOfPoints)
{
_functionCode = functionCode;
_slaveAddress = slaveAddress;
_startAddress = startAddress;
_numberOfPoints = numberOfPoints;
}

public byte[] MessageFrame
{
get
{
List<byte> frame = new List<byte>();
frame.Add(SlaveAddress);
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
}
}

public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new List<byte>();

pdu.Add(FunctionCode);
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress)));
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints)));

return pdu.ToArray();
}
}

public ushort TransactionId
{
get { return _transactionId; }
set { _transactionId = value; }
}

public byte FunctionCode
{
get { return _functionCode; }
set { _functionCode = value; }
}

public byte SlaveAddress
{
get { return _slaveAddress; }
set { _slaveAddress = value; }
}

public ushort StartAddress
{
get { return _startAddress; }
set { _startAddress = value; }
}

public ushort NumberOfPoints
{
get { return _numberOfPoints; }
set { _numberOfPoints = value; }
}

public void Initialize(byte[] frame)
{
if (frame == null)
{
throw new ArgumentNullException(nameof(frame));
}

if (frame.Length != 6)
{
throw new ArgumentException("Invalid frame.", nameof(frame));
}

SlaveAddress = frame[0];
FunctionCode = frame[1];
StartAddress = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 2));
NumberOfPoints = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 4));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Modbus.Data;
using Modbus.Message;

namespace Modbus.IntegrationTests.CustomMessages
{
public class CustomReadHoldingRegistersResponse : IModbusMessage
{
private byte _functionCode;
private byte _slaveAddress;
private byte _byteCount;
private ushort _transactionId;
private RegisterCollection _data;

public ushort[] Data => _data.ToArray();

public byte[] MessageFrame
{
get
{
List<byte> frame = new List<byte>();
frame.Add(SlaveAddress);
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
}
}

public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new List<byte>();

pdu.Add(_functionCode);
pdu.Add(ByteCount);
pdu.AddRange(_data.NetworkBytes);

return pdu.ToArray();
}
}

public ushort TransactionId
{
get { return _transactionId; }
set { _transactionId = value; }
}

public byte FunctionCode
{
get { return _functionCode; }
set { _functionCode = value; }
}

public byte SlaveAddress
{
get { return _slaveAddress; }
set { _slaveAddress = value; }
}

public byte ByteCount
{
get { return _byteCount; }
set { _byteCount = value; }
}

public void Initialize(byte[] frame)
{
if (frame == null)
{
throw new ArgumentNullException(nameof(frame));
}

if (frame.Length < 3 || frame.Length < 3 + frame[2])
{
throw new ArgumentException("Message frame does not contain enough bytes.", nameof(frame));
}

SlaveAddress = frame[0];
FunctionCode = frame[1];
ByteCount = frame[2];
_data = new RegisterCollection(frame.Skip(3).Take(ByteCount).ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Modbus.Data;
using Modbus.Message;

namespace Modbus.IntegrationTests.CustomMessages
{
public class CustomWriteMultipleRegistersRequest : IModbusMessage
{
private byte _functionCode;
private byte _slaveAddress;
private byte _byteCount;
private ushort _startAddress;
private ushort _numberOfPoints;
private ushort _transactionId;
private RegisterCollection _data;

public CustomWriteMultipleRegistersRequest(byte functionCode, byte slaveAddress, ushort startAddress, RegisterCollection data)
{
_functionCode = functionCode;
_slaveAddress = slaveAddress;
_startAddress = startAddress;
_numberOfPoints = (ushort)data.Count;
_byteCount = data.ByteCount;
_data = data;
}

public byte[] MessageFrame
{
get
{
List<byte> frame = new List<byte>();
frame.Add(SlaveAddress);
frame.AddRange(ProtocolDataUnit);

return frame.ToArray();
}
}

public byte[] ProtocolDataUnit
{
get
{
List<byte> pdu = new List<byte>();

pdu.Add(FunctionCode);
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)StartAddress)));
pdu.AddRange(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)NumberOfPoints)));
pdu.Add(ByteCount);
pdu.AddRange(Data.NetworkBytes);

return pdu.ToArray();
}
}

public ushort TransactionId
{
get { return _transactionId; }
set { _transactionId = value; }
}

public byte FunctionCode
{
get { return _functionCode; }
set { _functionCode = value; }
}

public byte SlaveAddress
{
get { return _slaveAddress; }
set { _slaveAddress = value; }
}

public ushort StartAddress
{
get { return _startAddress; }
set { _startAddress = value; }
}

public ushort NumberOfPoints
{
get { return _numberOfPoints; }
set { _numberOfPoints = value; }
}

public byte ByteCount
{
get { return _byteCount; }
set { _byteCount = value; }
}

public RegisterCollection Data
{
get { return _data; }
set { _data = value; }
}

public void Initialize(byte[] frame)
{
if (frame == null)
{
throw new ArgumentNullException(nameof(frame));
}

if (frame.Length < 7 || frame.Length < 7 + frame[6])
{
throw new FormatException("Message frame does not contain enough bytes.");
}

SlaveAddress = frame[0];
FunctionCode = frame[1];
StartAddress = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 2));
NumberOfPoints = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(frame, 4));
ByteCount = frame[6];
Data = new RegisterCollection(frame.Skip(7).Take(ByteCount).ToArray());
}
}
}
Loading

0 comments on commit 5dee6ce

Please sign in to comment.