2014-10-08 update 1

This commit is contained in:
parallelbgls@outlook.com
2014-10-08 15:44:36 +08:00
parent 0533c8bebc
commit 6135675cf3
8 changed files with 254 additions and 717 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Dynamic;
namespace ModBus.Net
{
@@ -7,6 +8,30 @@ namespace ModBus.Net
/// </summary>
public abstract class AddressTranslator
{
protected static AddressTranslator _instance;
public static AddressTranslator Instance
{
get
{
if (_instance == null)
{
CreateTranslator(new AddressTranslatorBase());
}
return _instance;
}
protected set
{
if (value == null) CreateTranslator(new AddressTranslatorBase());
_instance = value;
}
}
public static void CreateTranslator(AddressTranslator instance)
{
Instance = instance;
}
public abstract ushort AddressTranslate(string address);
}

View File

@@ -10,9 +10,7 @@ namespace ModBus.Net
{
public abstract void SetConnectionString(string connectionString);
public abstract void SetConnectionType(int connectionType);
public abstract bool[] GetCoils(byte belongAddress, string startAddress, ushort getCount);
public abstract bool SetCoils(byte belongAddress, string startAddress, bool[] setContents);
public abstract ushort[] GetRegisters(byte belongAddress, string startAddress, ushort getCount);
public abstract bool SetRegisters(byte belongAddress, string startAddress, object[] setContents);
public abstract byte[] GetDatas(byte belongAddress, byte functionCode, string startAddress, ushort getCount);
public abstract bool SetDatas(byte belongAddress, byte functionCode, string startAddress, object[] setContents);
}
}

View File

@@ -1,22 +1,31 @@
using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
public enum ModbusProtocalReg
public enum ModbusProtocalFunctionCode
{
}
internal enum ModbusProtocalTimeFunctionCode
{
GetSystemTime = 3,
SetSystemTime = 16,
};
public enum ModbusProtocalReadDataFunctionCode
{
ReadCoilStatus = 1,
ReadInputStatus = 2,
ReadHoldRegister = 3,
ReadInputRegister = 4,
WriteOneCoil = 5,
WriteOneRegister = 6,
}
public enum ModbusProtocalWriteDataFunctionCode
{
WriteMultiCoil = 15,
WriteMultiRegister = 16,
GetSystemTime = 3,
SetSystemTime = 16,
ReadVariable = 20,
WriteVariable = 21,
};
}
namespace ModBus.Net
{
public abstract class ModbusProtocal : BaseProtocal
@@ -24,549 +33,118 @@ namespace ModBus.Net
}
/// <summary>
/// 读线圈状态
/// </summary>
public class ReadCoilStatusModbusProtocal : ProtocalUnit
public class ReadDataInputStruct : InputStruct
{
public ReadDataInputStruct(byte belongAddress, ModbusProtocalReadDataFunctionCode functionCode, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (byte)functionCode;
StartAddress = AddressTranslator.Instance.AddressTranslate(startAddress);
GetCount = getCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class ReadDataOutputStruct : OutputStruct
{
public ReadDataOutputStruct(byte belongAddress, byte functionCode,
int dataCount, byte[] dataValue)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
DataCount = dataCount;
DataValue = dataValue.Clone() as byte[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public int DataCount { get; private set; }
public byte[] DataValue { get; private set; }
}
public class ReadDataModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (ReadCoilStatusInputStruct)message;
var r_message = (ReadDataInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
public override OutputStruct Unformat(byte[] messageBytes, ref int pos)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte coilCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
var coilStatusArr = new bool[coilCount * 8];
for (int i = 0; i < coilCount; i++)
{
byte coilStatusGet = ValueHelper.Instance.GetByte(messageBytes, ref flag);
for (int j = 0; j < 8; j++)
{
if (coilStatusGet % 2 == 0) coilStatusArr[8 * i + j] = false;
else coilStatusArr[8 * i + j] = true;
coilStatusGet /= 2;
}
}
return new ReadCoilStatusOutputStruct(belongAddress, functionCode, coilCount * 8, coilStatusArr);
}
public class ReadCoilStatusInputStruct : InputStruct
{
public ReadCoilStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadCoilStatus;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
GetCount = getCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class ReadCoilStatusOutputStruct : OutputStruct
{
public ReadCoilStatusOutputStruct(byte belongAddress, byte functionCode,
int coilCount, bool[] coilStatus)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
CoilCount = coilCount;
CoilStatus = coilStatus.Clone() as bool[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public int CoilCount { get; private set; }
public bool[] CoilStatus { get; private set; }
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref pos);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref pos);
byte dataCount = ValueHelper.Instance.GetByte(messageBytes, ref pos);
byte[] dataValue = new byte[dataCount];
Array.Copy(messageBytes, 3, dataValue, 0, dataCount);
return new ReadDataOutputStruct(belongAddress, functionCode, dataCount, dataValue);
}
}
/// <summary>
/// 读输入状态协
/// </summary>
public class ReadInputStatusModbusProtocal : ProtocalUnit
public class WriteDataInputStruct : InputStruct
{
public override byte[] Format(InputStruct message)
public WriteDataInputStruct(byte belongAddress, ModbusProtocalWriteDataFunctionCode functionCode, string startAddress, object[] writeValue)
{
var r_message = (ReadInputStatusInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
BelongAddress = belongAddress;
FunctionCode = (byte)functionCode;
StartAddress = AddressTranslator.Instance.AddressTranslate(startAddress);
WriteCount = (ushort)writeValue.Length;
WriteByteCount = (byte)(WriteCount * 2);
WriteValue = writeValue.Clone() as object[];
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte inputCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
var inputStatusArr = new bool[inputCount * 8];
for (int i = 0; i < inputCount; i++)
{
byte inputStatusGet = ValueHelper.Instance.GetByte(messageBytes, ref flag);
for (int j = 0; j < 8; j++)
{
if (inputStatusGet % 2 == 0) inputStatusArr[8 * i + j] = false;
else inputStatusArr[8 * i + j] = true;
inputStatusGet /= 2;
}
}
return new ReadInputStatusOutputStruct(belongAddress, functionCode, inputCount * 8,
inputStatusArr);
}
public byte BelongAddress { get; private set; }
public class ReadInputStatusInputStruct : InputStruct
{
public ReadInputStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
GetCount = getCount;
}
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public byte BelongAddress { get; private set; }
public ushort WriteCount { get; private set; }
public byte FunctionCode { get; private set; }
public byte WriteByteCount { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class ReadInputStatusOutputStruct : OutputStruct
{
public ReadInputStatusOutputStruct(byte belongAddress, byte functionCode,
int inputCount, bool[] inputStatus)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
InputCount = inputCount;
InputStatus = inputStatus.Clone() as bool[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public int InputCount { get; private set; }
public bool[] InputStatus { get; private set; }
}
public object[] WriteValue { get; private set; }
}
/// <summary>
/// 读保持型寄存器
/// </summary>
public class ReadHoldRegisterModbusProtocal : ProtocalUnit
public class WriteDataOutputStruct : OutputStruct
{
public override byte[] Format(InputStruct message)
public WriteDataOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{
var r_message = (ReadHoldRegisterInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteCount = writeCount;
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte holdRegisterCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
var holdRegisterArr = new ushort[holdRegisterCount / 2];
for (int i = 0; i < holdRegisterCount / 2; i++)
{
holdRegisterArr[i] = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
}
return new ReadHoldRegisterOutputStruct(belongAddress, functionCode, holdRegisterCount / 2,
holdRegisterArr);
}
public byte BelongAddress { get; private set; }
public class ReadHoldRegisterInputStruct : InputStruct
{
public ReadHoldRegisterInputStruct(byte belongAddress, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadHoldRegister;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
GetCount = getCount;
}
public byte FunctionCode { get; private set; }
public byte BelongAddress { get; private set; }
public ushort StartAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class ReadHoldRegisterOutputStruct : OutputStruct
{
public ReadHoldRegisterOutputStruct(byte belongAddress, byte functionCode,
int holdRegisterCount, ushort[] holdRegisterStatus)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
HoldRegisterCount = holdRegisterCount;
HoldRegisterStatus = holdRegisterStatus.Clone() as ushort[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public int HoldRegisterCount { get; private set; }
public ushort[] HoldRegisterStatus { get; private set; }
}
}
/// <summary>
/// 读输入型寄存器
/// </summary>
public class ReadInputRegisterModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (ReadInputRegisterInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
if (messageBytes.Length == 19 && messageBytes[15] == 0 && messageBytes[17] == 0 && messageBytes[18] == 0)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte eventByteCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte soeProperty = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte soeEvent = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte month = ValueHelper.Instance.GetByte(messageBytes, ref flag);
int year = ValueHelper.Instance.GetByte(messageBytes, ref flag) + 2002;
byte hour = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte day = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte second = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte minute = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort millisecond = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort testPoint = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
flag += 1;
byte testValue = ValueHelper.Instance.GetByte(messageBytes, ref flag);
flag += 1;
flag += 1;
return new ReadEventOutputStruct(belongAddress, functionCode, eventByteCount, soeProperty,
soeEvent,
new DateTime(year, month == 0 ? 1 : 0, day == 0 ? 1 : 0, hour, minute, second, millisecond),
testPoint, testValue);
}
else
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte inputRegisterCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
var holdRegisterArr = new ushort[inputRegisterCount / 2];
for (int i = 0; i < inputRegisterCount / 2; i++)
{
holdRegisterArr[i] = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
}
return new ReadInputRegisterOutputStruct(belongAddress, functionCode, inputRegisterCount / 2,
holdRegisterArr);
}
}
public class ReadEventOutputStruct : OutputStruct
{
public ReadEventOutputStruct(byte belongAddress, byte functionCode,
byte eventByteCount, byte soeProperty, byte soeEvent, DateTime time, ushort testPoint,
byte testValue)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
EventByteCount = eventByteCount;
SoeProperty = soeProperty;
SoeEvent = soeEvent;
TestTime = time;
TestPoint = testPoint;
TestValue = testValue;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public byte EventByteCount { get; private set; }
public byte SoeProperty { get; private set; }
public byte SoeEvent { get; private set; }
public DateTime TestTime { get; private set; }
public ushort TestPoint { get; private set; }
public byte TestValue { get; private set; }
}
public class ReadInputRegisterInputStruct : InputStruct
{
public ReadInputRegisterInputStruct(byte belongAddress, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadInputRegister;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
GetCount = getCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class ReadInputRegisterOutputStruct : OutputStruct
{
public ReadInputRegisterOutputStruct(byte belongAddress, byte functionCode,
int inputRegisterCount, ushort[] inputRegisterStatus)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
InputRegisterCount = inputRegisterCount;
InputRegisterStatus = inputRegisterStatus.Clone() as ushort[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public int InputRegisterCount { get; private set; }
public ushort[] InputRegisterStatus { get; private set; }
}
}
/// <summary>
/// 读单个线圈状态
/// </summary>
public class WriteOneCoilModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (WriteOneCoilInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteValue ? new byte[] { 0xFF, 0x00 } : new byte[] { 0x00, 0x00 });
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort writeValue = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new WriteOneCoilOutputStruct(belongAddress, functionCode, startAddress,
writeValue != 0);
}
public class WriteOneCoilInputStruct : InputStruct
{
public WriteOneCoilInputStruct(byte belongAddress, string startAddress, bool writeValue)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.WriteOneCoil;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
WriteValue = writeValue;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public bool WriteValue { get; private set; }
}
public class WriteOneCoilOutputStruct : OutputStruct
{
public WriteOneCoilOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, bool writeValue)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteValue = writeValue;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public bool WriteValue { get; private set; }
}
}
/// <summary>
/// 写单个寄存器状态
/// </summary>
public class WriteOneRegisterModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (WriteOneRegisterInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteValue);
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort writeValue = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new WriteOneRegisterOutputStruct(belongAddress, functionCode, startAddress, writeValue);
}
public class WriteOneRegisterInputStruct : InputStruct
{
public WriteOneRegisterInputStruct(byte belongAddress, string startAddress, ushort writeValue)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.WriteOneRegister;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
WriteValue = writeValue;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteValue { get; private set; }
}
public class WriteOneRegisterOutputStruct : OutputStruct
{
public WriteOneRegisterOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeValue)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteValue = writeValue;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteValue { get; private set; }
}
}
/// <summary>
/// 写多个线圈状态
/// </summary>
public class WriteMultiCoilModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (WriteMultiCoilInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.WriteValue);
}
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new WriteMultiCoilOutputStruct(belongAddress, functionCode, startAddress, writeCount);
}
public class WriteMultiCoilInputStruct : InputStruct
{
public WriteMultiCoilInputStruct(byte belongAddress, string startAddress, bool[] writeValue)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.WriteMultiCoil;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
WriteCount = (ushort)writeValue.Length;
WriteByteCount = WriteCount % 8 > 0 ? (byte)(WriteCount / 8 + 1) : (byte)(WriteCount / 8);
WriteValue = new byte[WriteByteCount];
for (int i = 0; i < writeValue.Length; i += 8)
{
int bytenum = 0;
for (int j = 7; j >= 0; j--)
{
int t = i + j < writeValue.Length && writeValue[i + j] ? 1 : 0;
bytenum = bytenum * 2 + t;
}
WriteValue[i / 8] = (byte)bytenum;
}
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
public byte WriteByteCount { get; private set; }
public byte[] WriteValue { get; private set; }
}
public class WriteMultiCoilOutputStruct : OutputStruct
{
public WriteMultiCoilOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteCount = writeCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
}
public ushort WriteCount { get; private set; }
}
/// <summary>
/// 写多个寄存器状态
/// </summary>
public class WriteMultiRegisterModbusProtocal : ProtocalUnit
public class WriteDataModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (WriteMultiRegisterInputStruct)message;
var r_message = (WriteDataInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.WriteValue);
}
@@ -577,54 +155,49 @@ namespace ModBus.Net
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new WriteMultiRegisterOutputStruct(belongAddress, functionCode, startAddress,
return new WriteDataOutputStruct(belongAddress, functionCode, startAddress,
writeCount);
}
}
public class WriteMultiRegisterInputStruct : InputStruct
public class GetSystemTimeInputStruct : InputStruct
{
public GetSystemTimeInputStruct(byte belongAddress)
{
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, object[] writeValue)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
WriteCount = (ushort)writeValue.Length;
WriteByteCount = (byte)(WriteCount * 2);
WriteValue = writeValue.Clone() as object[];
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
public byte WriteByteCount { get; private set; }
public object[] WriteValue { get; private set; }
BelongAddress = belongAddress;
FunctionCode = (byte)ModbusProtocalTimeFunctionCode.GetSystemTime;
StartAddress = 30000;
GetCount = 5;
}
public class WriteMultiRegisterOutputStruct : OutputStruct
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
}
public class GetSystemTimeOutputStruct : OutputStruct
{
public GetSystemTimeOutputStruct(byte belongAddress, byte functionCode,
byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
ushort millisecond)
{
public WriteMultiRegisterOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteCount = writeCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
BelongAddress = belongAddress;
FunctionCode = functionCode;
WriteByteCount = writeByteCount;
Time = new DateTime(year, month, day, hour, minute, second, millisecond);
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public byte WriteByteCount { get; private set; }
public DateTime Time { get; private set; }
}
/// <summary>
@@ -654,46 +227,69 @@ namespace ModBus.Net
return new GetSystemTimeOutputStruct(belongAddress, functionCode, writeByteCount, year, day,
month, hour, second, minute, millisecond);
}
}
public class GetSystemTimeInputStruct : InputStruct
public class SetSystemTimeInputStruct : InputStruct
{
public SetSystemTimeInputStruct(byte belongAddress, DateTime time)
{
public GetSystemTimeInputStruct(byte belongAddress)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.GetSystemTime;
StartAddress = 30000;
GetCount = 5;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
BelongAddress = belongAddress;
FunctionCode = (byte)ModbusProtocalTimeFunctionCode.SetSystemTime;
StartAddress = 30000;
WriteCount = 5;
WriteByteCount = 10;
Year = (ushort)time.Year;
Day = (byte)time.Day;
Month = (byte)time.Month;
Hour = (ushort)time.Hour;
Second = (byte)time.Second;
Minute = (byte)time.Minute;
Millisecond = (ushort)time.Millisecond;
}
public class GetSystemTimeOutputStruct : OutputStruct
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
public byte WriteByteCount { get; private set; }
public ushort Year { get; private set; }
public byte Day { get; private set; }
public byte Month { get; private set; }
public ushort Hour { get; private set; }
public byte Second { get; private set; }
public byte Minute { get; private set; }
public ushort Millisecond { get; private set; }
}
public class SetSystemTimeOutputStruct : OutputStruct
{
public SetSystemTimeOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{
public GetSystemTimeOutputStruct(byte belongAddress, byte functionCode,
byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
ushort millisecond)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
WriteByteCount = writeByteCount;
Time = new DateTime(year, month, day, hour, minute, second, millisecond);
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public byte WriteByteCount { get; private set; }
public DateTime Time { get; private set; }
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteCount = writeCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
}
/// <summary>
@@ -718,70 +314,6 @@ namespace ModBus.Net
ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new SetSystemTimeOutputStruct(belongAddress, functionCode, startAddress, writeCount);
}
public class SetSystemTimeInputStruct : InputStruct
{
public SetSystemTimeInputStruct(byte belongAddress, DateTime time)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.SetSystemTime;
StartAddress = 30000;
WriteCount = 5;
WriteByteCount = 10;
Year = (ushort)time.Year;
Day = (byte)time.Day;
Month = (byte)time.Month;
Hour = (ushort)time.Hour;
Second = (byte)time.Second;
Minute = (byte)time.Minute;
Millisecond = (ushort)time.Millisecond;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
public byte WriteByteCount { get; private set; }
public ushort Year { get; private set; }
public byte Day { get; private set; }
public byte Month { get; private set; }
public ushort Hour { get; private set; }
public byte Second { get; private set; }
public byte Minute { get; private set; }
public ushort Millisecond { get; private set; }
}
public class SetSystemTimeOutputStruct : OutputStruct
{
public SetSystemTimeOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{
BelongAddress = belongAddress;
FunctionCode = functionCode;
StartAddress = startAddress;
WriteCount = writeCount;
}
public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public ushort StartAddress { get; private set; }
public ushort WriteCount { get; private set; }
}
}
public class ProtocalErrorException : Exception

View File

@@ -51,7 +51,7 @@ namespace ModBus.Net
ModbusType = (ModbusType)connectionType;
}
public ModbusUtility(string connectionString, int connectionType)
public ModbusUtility(int connectionType, string connectionString)
{
ConnectionString = connectionString;
ModbusType = (ModbusType)connectionType;
@@ -67,50 +67,14 @@ namespace ModBus.Net
ModbusType = (ModbusType) connectionType;
}
public override bool[] GetCoils(byte belongAddress, string startAddress, ushort getCount)
public override byte[] GetDatas(byte belongAddress, byte functionCode, string startAddress, ushort getCount)
{
try
{
var inputStruct = new ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct(belongAddress, startAddress,
getCount);
var inputStruct = new ReadDataInputStruct(belongAddress, (ModbusProtocalReadDataFunctionCode)functionCode, startAddress, getCount);
var outputStruct =
_wrapper.SendReceive(_wrapper["ReadCoilStatusModbusProtocal"], inputStruct) as
ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct;
return outputStruct.CoilStatus;
}
catch (Exception)
{
return null;
}
}
public override bool SetCoils(byte belongAddress, string startAddress, bool[] setContents)
{
try
{
var inputStruct = new WriteMultiCoilModbusProtocal.WriteMultiCoilInputStruct(belongAddress, startAddress,
setContents);
var outputStruct =
_wrapper.SendReceive(_wrapper["WriteMultiCoilModbusProtocal"], inputStruct) as
WriteMultiCoilModbusProtocal.WriteMultiCoilOutputStruct;
if (outputStruct.WriteCount != setContents.Length) return false;
return true;
}
catch (Exception)
{
return false;
}
}
public override ushort[] GetRegisters(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;
_wrapper.SendReceive(_wrapper["ReadDataModbusProtocal"], inputStruct) as ReadDataOutputStruct;
return outputStruct.DataValue;
}
catch
{
@@ -118,15 +82,15 @@ namespace ModBus.Net
}
}
public override bool SetRegisters(byte belongAddress, string startAddress, object[] setContents)
public override bool SetDatas(byte belongAddress, byte functionCode, string startAddress, object[] setContents)
{
try
{
var inputStruct = new WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct(belongAddress,
startAddress, setContents);
var inputStruct = new WriteDataInputStruct(belongAddress,
(ModbusProtocalWriteDataFunctionCode) functionCode, startAddress, setContents);
var outputStruct =
_wrapper.SendReceive(_wrapper["WriteMultiRegisterModbusProtocal"], inputStruct) as
WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct;
_wrapper.SendReceive(_wrapper["WriteDataModbusProtocal"], inputStruct) as
WriteDataOutputStruct;
if (outputStruct.WriteCount != setContents.Length) return false;
return true;
}

View File

@@ -8,9 +8,9 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTitle("ModBus.Net")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCompany("杭州德联科技股份有限公司")]
[assembly: AssemblyProduct("ModBus.Net")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyCopyright("Copyright © Chris L. 2014 HangZhou Delin Technology")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -35,5 +35,5 @@ using System.Runtime.InteropServices;
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.1.0.1008")]
[assembly: AssemblyFileVersion("0.1.0.1008")]

View File

@@ -7,11 +7,11 @@ namespace ModBus.Net
{
public abstract class ProtocalUnit : IProtocalFormatting
{
protected static AddressTranslator _addressTranslator = new AddressTranslatorBase();
//protected static AddressTranslator _addressTranslator = new AddressTranslatorBase();
public ProtocalUnit SetAddressTranslator(AddressTranslator addressTranslator)
public ProtocalUnit SetAddressTranslator(/*AddressTranslator addressTranslator*/)
{
_addressTranslator = addressTranslator;
//_addressTranslator = addressTranslator;
return this;
}

View File

@@ -379,6 +379,13 @@ namespace ModBus.Net
return translation.ToArray();
}
public T[] ObjectArrayToDestinationArray<T>(object[] contents)
{
T[] array = new T[contents.Length];
Array.Copy(contents,array,contents.Length);
return array;
}
public bool GetBit(ushort number, ref int pos)
{
if (pos < 0 && pos > 15) throw new IndexOutOfRangeException();

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using ModBus.Net;
using System.Windows;
@@ -18,9 +20,13 @@ namespace NA200H.UI.WPF
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
utility = new ModbusUtility((int)ModbusType.Rtu);
ushort[] getNum = utility.GetRegisters(0x02, "0", 4);
SetValue(getNum);
utility = new ModbusUtility((int) ModbusType.Tcp, "192.168.3.247");
byte[] getNum = utility.GetDatas(0x02, (byte) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, "10000", 4);
object[] getNumObjects =
ValueHelper.Instance.ByteArrayToObjectArray(getNum,
new List<KeyValuePair<Type, int>>(){{new KeyValuePair<Type, int>(typeof(ushort), 4)}});
ushort[] getNumUshorts = ValueHelper.Instance.ObjectArrayToDestinationArray<ushort>(getNumObjects);
SetValue(getNumUshorts);
}
private void SetValue(ushort[] getNum)
@@ -37,9 +43,14 @@ namespace NA200H.UI.WPF
ushort.TryParse(Add1.Text, out add1);
ushort.TryParse(Add2.Text, out add2);
ushort.TryParse(Add3.Text, out add3);
utility.SetRegisters(0x02, "0", new object[] {add1, add2, add3});
ushort[] getNum = utility.GetRegisters(0x02, "0", 4);
SetValue(getNum);
utility.SetDatas(0x02, (byte)ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, "10000", new object[] {add1, add2, add3});
Thread.Sleep(100);
byte[] getNum = utility.GetDatas(0x02, (byte)ModbusProtocalReadDataFunctionCode.ReadHoldRegister, "10000", 4);
object[] getNumObjects =
ValueHelper.Instance.ByteArrayToObjectArray(getNum,
new List<KeyValuePair<Type, int>>() { { new KeyValuePair<Type, int>(typeof(ushort), 4) } });
ushort[] getNumUshorts = ValueHelper.Instance.ObjectArrayToDestinationArray<ushort>(getNumObjects);
SetValue(getNumUshorts);
}
}
}