Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sjalex
committed
Mar 28, 2009
1 parent
646a866
commit 88ba70b
Showing
7 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using Modbus.Device; | ||
using Modbus.Utility; | ||
|
||
namespace Modbus.Extensions.Enron | ||
{ | ||
/// <summary> | ||
/// Utility extensions for the Enron Modbus dialect. | ||
/// </summary> | ||
public static class EnronModbus | ||
{ | ||
/// <summary> | ||
/// Read contiguous block of 32 bit holding registers. | ||
/// </summary> | ||
/// <param name="master">The Modbus master.</param> | ||
/// <param name="slaveAddress">Address of device to read values from.</param> | ||
/// <param name="startAddress">Address to begin reading.</param> | ||
/// <param name="numberOfPoints">Number of holding registers to read.</param> | ||
/// <returns>Holding registers status</returns> | ||
public static uint[] ReadHoldingRegisters32(this ModbusMaster master, byte slaveAddress, ushort startAddress, ushort numberOfPoints) | ||
{ | ||
ValidateNumberOfPoints(numberOfPoints, 62); | ||
|
||
// read 16 bit chunks and perform conversion | ||
var rawRegisters = master.ReadHoldingRegisters(slaveAddress, startAddress, (ushort) (numberOfPoints * 2)); | ||
|
||
return Convert(rawRegisters).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Read contiguous block of 32 bit input registers. | ||
/// </summary> | ||
/// <param name="master">The Modbus master.</param> | ||
/// <param name="slaveAddress">Address of device to read values from.</param> | ||
/// <param name="startAddress">Address to begin reading.</param> | ||
/// <param name="numberOfPoints">Number of holding registers to read.</param> | ||
/// <returns>Input registers status</returns> | ||
public static uint[] ReadInputRegisters32(this ModbusMaster master, byte slaveAddress, ushort startAddress, ushort numberOfPoints) | ||
{ | ||
ValidateNumberOfPoints(numberOfPoints, 62); | ||
|
||
var rawRegisters = master.ReadInputRegisters(slaveAddress, startAddress, (ushort) (numberOfPoints * 2)); | ||
|
||
return Convert(rawRegisters).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Write a block of contiguous 32 bit holding registers. | ||
/// </summary> | ||
/// <param name="master">The Modbus master.</param> | ||
/// <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 static void WriteMultipleRegisters32(this ModbusMaster master, byte slaveAddress, ushort startAddress, uint[] data) | ||
{ | ||
if (data == null) | ||
throw new ArgumentNullException("data"); | ||
|
||
if (data.Length == 0 || data.Length > 61) | ||
{ | ||
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, | ||
"The length of argument data must be between 1 and 61 inclusive.")); | ||
} | ||
|
||
master.WriteMultipleRegisters(slaveAddress, startAddress, Convert(data).ToArray()); | ||
} | ||
|
||
/// <summary> | ||
/// Convert the 32 bit registers to two 16 bit values. | ||
/// </summary> | ||
internal static IEnumerable<ushort> Convert(uint[] registers) | ||
{ | ||
foreach (var register in registers) | ||
{ | ||
// low order value | ||
yield return BitConverter.ToUInt16(BitConverter.GetBytes(register), 0); | ||
|
||
// high order value | ||
yield return BitConverter.ToUInt16(BitConverter.GetBytes(register), 2); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Convert the 16 bit registers to 32 bit registers. | ||
/// </summary> | ||
internal static IEnumerable<uint> Convert(ushort[] registers) | ||
{ | ||
for (int i = 0; i < registers.Length; i++) | ||
{ | ||
yield return ModbusUtility.GetUInt32(registers[i], registers[i + 1]); | ||
i++; | ||
} | ||
} | ||
|
||
internal static void ValidateNumberOfPoints(ushort numberOfPoints, ushort maxNumberOfPoints) | ||
{ | ||
if (numberOfPoints < 1 || numberOfPoints > maxNumberOfPoints) | ||
{ | ||
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, | ||
"Argument numberOfPoints must be between 1 and {0} inclusive.", | ||
maxNumberOfPoints)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProductVersion>9.0.30729</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{667F4D2C-C9DF-4CC4-A1A6-768F6E879CE5}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Modbus.Extensions</RootNamespace> | ||
<AssemblyName>Modbus.Extensions</AssemblyName> | ||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<DocumentationFile>bin\Debug\Modbus.Extensions.XML</DocumentationFile> | ||
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<DocumentationFile>bin\Release\Modbus.Extensions.XML</DocumentationFile> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'SignedRelease|AnyCPU' "> | ||
<OutputPath>bin\SignedRelease\</OutputPath> | ||
<DefineConstants>TRACE;SIGNED</DefineConstants> | ||
<DocumentationFile>bin\Release\Modbus.Extensions.XML</DocumentationFile> | ||
<Optimize>true</Optimize> | ||
<DebugType>pdbonly</DebugType> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression> | ||
<CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile> | ||
<ErrorReport>prompt</ErrorReport> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\NModbusPublic.snk</AssemblyOriginatorKeyFile> | ||
<DelaySign>true</DelaySign> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core"> | ||
<RequiredTargetFramework>3.5</RequiredTargetFramework> | ||
</Reference> | ||
<Reference Include="System.Xml.Linq"> | ||
<RequiredTargetFramework>3.5</RequiredTargetFramework> | ||
</Reference> | ||
<Reference Include="System.Data.DataSetExtensions"> | ||
<RequiredTargetFramework>3.5</RequiredTargetFramework> | ||
</Reference> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
<Reference Include="Unme.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=da57c01ae4598fdd, processorArchitecture=MSIL"> | ||
<SpecificVersion>False</SpecificVersion> | ||
<HintPath>..\..\lib\Unme\Unme.Common.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="EnronModbus.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Modbus\Modbus.csproj"> | ||
<Project>{FF96B252-2B24-44A1-A09C-6401B5D7ED23}</Project> | ||
<Name>Modbus</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\..\NModbusPublic.snk"> | ||
<Link>NModbusPublic.snk</Link> | ||
</None> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: AssemblyTitle("Modbus.Extensions")] | ||
[assembly: AssemblyProduct("NModbus")] | ||
[assembly: AssemblyCopyright("Licensed under MIT License.")] | ||
[assembly: AssemblyDescription("Provides utiltiy extensions for the NModbus library.")] | ||
[assembly: ComVisible(false)] | ||
[assembly: CLSCompliant(false)] | ||
[assembly: Guid("A0218720-E559-434d-AB0D-CC03E6D69F46")] | ||
[assembly: AssemblyVersion("1.9.0.0")] | ||
[assembly: AssemblyFileVersion("1.9.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters