2014-08-28 update 1
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
<Compile Include="AddressTranslator.cs" />
|
||||
<Compile Include="BaseProtocal.cs" />
|
||||
<Compile Include="ComConnector.cs" />
|
||||
<Compile Include="ModbusUtility.cs" />
|
||||
<Compile Include="RtuProtocalLinker.cs" />
|
||||
<Compile Include="ConfigurationManager.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
|
||||
@@ -583,14 +583,14 @@ namespace ModBus.Net
|
||||
|
||||
public class WriteMultiRegisterInputStruct : InputStruct
|
||||
{
|
||||
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, ushort[] writeValue)
|
||||
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, object[] writeValue)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
WriteCount = (ushort)writeValue.Length;
|
||||
WriteByteCount = (byte)(WriteCount * 2);
|
||||
WriteValue = writeValue.Clone() as ushort[];
|
||||
WriteValue = writeValue.Clone() as object[];
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
@@ -603,7 +603,7 @@ namespace ModBus.Net
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
|
||||
public ushort[] WriteValue { get; private set; }
|
||||
public object[] WriteValue { get; private set; }
|
||||
}
|
||||
|
||||
public class WriteMultiRegisterOutputStruct : OutputStruct
|
||||
|
||||
89
NA200H/ModBus.Net/ModbusUtility.cs
Normal file
89
NA200H/ModBus.Net/ModbusUtility.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Windows.Forms;
|
||||
|
||||
public enum ModbusType
|
||||
{
|
||||
Rtu = 0,
|
||||
Tcp = 1,
|
||||
}
|
||||
|
||||
namespace ModBus.Net
|
||||
{
|
||||
public class ModbusUtility
|
||||
{
|
||||
private BaseProtocal _wrapper;
|
||||
|
||||
private ModbusType _modbusType;
|
||||
|
||||
public ModbusType ModbusType
|
||||
{
|
||||
get
|
||||
{
|
||||
return _modbusType;
|
||||
}
|
||||
set
|
||||
{
|
||||
_modbusType = value;
|
||||
switch (_modbusType)
|
||||
{
|
||||
case ModbusType.Rtu:
|
||||
{
|
||||
_wrapper = new ModbusRtuProtocal();
|
||||
break;
|
||||
}
|
||||
case ModbusType.Tcp:
|
||||
{
|
||||
_wrapper = new ModbusTcpProtocal();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ModbusUtility _modbusUtility;
|
||||
private ModbusUtility()
|
||||
{
|
||||
ModbusType = ModbusType.Rtu;
|
||||
}
|
||||
|
||||
public static ModbusUtility GetInstance()
|
||||
{
|
||||
return _modbusUtility ?? (_modbusUtility = new ModbusUtility());
|
||||
}
|
||||
|
||||
public ushort[] ReadHoldRegister(byte belongAddress, string startAddress, ushort getCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(belongAddress, startAddress, getCount);
|
||||
var outputStruct =
|
||||
_wrapper.SendReceive(_wrapper["ReadHoldRegisterModbusProtocal"], inputStruct) as
|
||||
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct;
|
||||
return outputStruct.HoldRegisterStatus;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool WriteMultiRegister(byte belongAddress, string startAddress, object[] writeValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct(belongAddress,
|
||||
startAddress, writeValue);
|
||||
var outputStruct =
|
||||
_wrapper.SendReceive(_wrapper["WriteMultiRegisterModbusProtocal"], inputStruct) as
|
||||
WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct;
|
||||
if (outputStruct.WriteCount != writeValue.Length) return false;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user