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.Collections.Generic;
using System.Dynamic;
namespace ModBus.Net namespace ModBus.Net
{ {
@@ -7,6 +8,30 @@ namespace ModBus.Net
/// </summary> /// </summary>
public abstract class AddressTranslator 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); public abstract ushort AddressTranslate(string address);
} }

View File

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

View File

@@ -1,22 +1,31 @@
using System; using System;
using System.Collections.Generic; 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, ReadCoilStatus = 1,
ReadInputStatus = 2, ReadInputStatus = 2,
ReadHoldRegister = 3, ReadHoldRegister = 3,
ReadInputRegister = 4, ReadInputRegister = 4,
WriteOneCoil = 5, }
WriteOneRegister = 6,
public enum ModbusProtocalWriteDataFunctionCode
{
WriteMultiCoil = 15, WriteMultiCoil = 15,
WriteMultiRegister = 16, WriteMultiRegister = 16,
GetSystemTime = 3, }
SetSystemTime = 16,
ReadVariable = 20,
WriteVariable = 21,
};
namespace ModBus.Net namespace ModBus.Net
{ {
public abstract class ModbusProtocal : BaseProtocal public abstract class ModbusProtocal : BaseProtocal
@@ -24,44 +33,13 @@ namespace ModBus.Net
} }
/// <summary> public class ReadDataInputStruct : InputStruct
/// 读线圈状态
/// </summary>
public class ReadCoilStatusModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public ReadDataInputStruct(byte belongAddress, ModbusProtocalReadDataFunctionCode functionCode, string startAddress, ushort getCount)
{
var r_message = (ReadCoilStatusInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
}
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 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; BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadCoilStatus; FunctionCode = (byte)functionCode;
StartAddress = _addressTranslator.AddressTranslate(startAddress); StartAddress = AddressTranslator.Instance.AddressTranslate(startAddress);
GetCount = getCount; GetCount = getCount;
} }
@@ -74,520 +52,53 @@ namespace ModBus.Net
public ushort GetCount { get; private set; } public ushort GetCount { get; private set; }
} }
public class ReadCoilStatusOutputStruct : OutputStruct public class ReadDataOutputStruct : OutputStruct
{ {
public ReadCoilStatusOutputStruct(byte belongAddress, byte functionCode, public ReadDataOutputStruct(byte belongAddress, byte functionCode,
int coilCount, bool[] coilStatus) int dataCount, byte[] dataValue)
{ {
BelongAddress = belongAddress; BelongAddress = belongAddress;
FunctionCode = functionCode; FunctionCode = functionCode;
CoilCount = coilCount; DataCount = dataCount;
CoilStatus = coilStatus.Clone() as bool[]; DataValue = dataValue.Clone() as byte[];
} }
public byte BelongAddress { get; private set; } public byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; } public byte FunctionCode { get; private set; }
public int CoilCount { get; private set; } public int DataCount { get; private set; }
public bool[] CoilStatus { get; private set; } public byte[] DataValue { get; private set; }
}
} }
/// <summary> public class ReadDataModbusProtocal : ProtocalUnit
/// 读输入状态协
/// </summary>
public class ReadInputStatusModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public override byte[] Format(InputStruct message)
{ {
var r_message = (ReadInputStatusInputStruct)message; var r_message = (ReadDataInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode, return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount); 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 belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref pos);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref pos);
byte inputCount = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte dataCount = ValueHelper.Instance.GetByte(messageBytes, ref pos);
var inputStatusArr = new bool[inputCount * 8]; byte[] dataValue = new byte[dataCount];
for (int i = 0; i < inputCount; i++) Array.Copy(messageBytes, 3, dataValue, 0, dataCount);
{ return new ReadDataOutputStruct(belongAddress, functionCode, dataCount, dataValue);
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 class ReadInputStatusInputStruct : InputStruct public class WriteDataInputStruct : InputStruct
{ {
public ReadInputStatusInputStruct(byte belongAddress, string startAddress, ushort getCount) public WriteDataInputStruct(byte belongAddress, ModbusProtocalWriteDataFunctionCode functionCode, string startAddress, object[] writeValue)
{ {
BelongAddress = belongAddress; BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus; FunctionCode = (byte)functionCode;
StartAddress = _addressTranslator.AddressTranslate(startAddress); 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 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; }
}
}
/// <summary>
/// 读保持型寄存器
/// </summary>
public class ReadHoldRegisterModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (ReadHoldRegisterInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount);
}
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 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 BelongAddress { 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; }
}
}
/// <summary>
/// 写多个寄存器状态
/// </summary>
public class WriteMultiRegisterModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (WriteMultiRegisterInputStruct)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 WriteMultiRegisterOutputStruct(belongAddress, functionCode, startAddress,
writeCount);
}
public class WriteMultiRegisterInputStruct : InputStruct
{
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, object[] writeValue)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
WriteCount = (ushort)writeValue.Length; WriteCount = (ushort)writeValue.Length;
WriteByteCount = (byte)(WriteCount * 2); WriteByteCount = (byte)(WriteCount * 2);
WriteValue = writeValue.Clone() as object[]; WriteValue = writeValue.Clone() as object[];
@@ -606,9 +117,9 @@ namespace ModBus.Net
public object[] WriteValue { get; private set; } public object[] WriteValue { get; private set; }
} }
public class WriteMultiRegisterOutputStruct : OutputStruct public class WriteDataOutputStruct : OutputStruct
{ {
public WriteMultiRegisterOutputStruct(byte belongAddress, byte functionCode, public WriteDataOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount) ushort startAddress, ushort writeCount)
{ {
BelongAddress = belongAddress; BelongAddress = belongAddress;
@@ -625,34 +136,28 @@ namespace ModBus.Net
public ushort WriteCount { get; private set; } public ushort WriteCount { get; private set; }
} }
}
/// <summary> /// <summary>
/// 读系统时间 /// 写多个寄存器状态
/// </summary> /// </summary>
public class GetSystemTimeModbusProtocal : ProtocalUnit public class WriteDataModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public override byte[] Format(InputStruct message)
{ {
var r_message = (GetSystemTimeInputStruct)message; var r_message = (WriteDataInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode, return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.GetCount); r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.WriteValue);
} }
public override OutputStruct Unformat(byte[] messageBytes, ref int flag) public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{ {
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte writeByteCount = ValueHelper.Instance.GetByte(messageBytes, ref flag); ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort year = ValueHelper.Instance.GetUShort(messageBytes, ref flag); ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
byte day = ValueHelper.Instance.GetByte(messageBytes, ref flag); return new WriteDataOutputStruct(belongAddress, functionCode, startAddress,
byte month = ValueHelper.Instance.GetByte(messageBytes, ref flag); writeCount);
ushort hour = ValueHelper.Instance.GetUShort(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);
return new GetSystemTimeOutputStruct(belongAddress, functionCode, writeByteCount, year, day,
month, hour, second, minute, millisecond);
} }
public class GetSystemTimeInputStruct : InputStruct public class GetSystemTimeInputStruct : InputStruct
@@ -660,7 +165,7 @@ namespace ModBus.Net
public GetSystemTimeInputStruct(byte belongAddress) public GetSystemTimeInputStruct(byte belongAddress)
{ {
BelongAddress = belongAddress; BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.GetSystemTime; FunctionCode = (byte)ModbusProtocalTimeFunctionCode.GetSystemTime;
StartAddress = 30000; StartAddress = 30000;
GetCount = 5; GetCount = 5;
} }
@@ -694,29 +199,34 @@ namespace ModBus.Net
public DateTime Time { get; private set; } public DateTime Time { get; private set; }
} }
}
/// <summary> /// <summary>
/// 系统时间 /// 系统时间
/// </summary> /// </summary>
public class SetSystemTimeModbusProtocal : ProtocalUnit public class GetSystemTimeModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public override byte[] Format(InputStruct message)
{ {
var r_message = (SetSystemTimeInputStruct)message; var r_message = (GetSystemTimeInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode, return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.Year, r_message.StartAddress, r_message.GetCount);
r_message.Day,
r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond);
} }
public override OutputStruct Unformat(byte[] messageBytes, ref int flag) public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
{ {
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag); byte writeByteCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag); ushort year = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new SetSystemTimeOutputStruct(belongAddress, functionCode, startAddress, writeCount); byte day = ValueHelper.Instance.GetByte(messageBytes, ref flag);
byte month = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort hour = ValueHelper.Instance.GetUShort(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);
return new GetSystemTimeOutputStruct(belongAddress, functionCode, writeByteCount, year, day,
month, hour, second, minute, millisecond);
}
} }
public class SetSystemTimeInputStruct : InputStruct public class SetSystemTimeInputStruct : InputStruct
@@ -724,7 +234,7 @@ namespace ModBus.Net
public SetSystemTimeInputStruct(byte belongAddress, DateTime time) public SetSystemTimeInputStruct(byte belongAddress, DateTime time)
{ {
BelongAddress = belongAddress; BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.SetSystemTime; FunctionCode = (byte)ModbusProtocalTimeFunctionCode.SetSystemTime;
StartAddress = 30000; StartAddress = 30000;
WriteCount = 5; WriteCount = 5;
WriteByteCount = 10; WriteByteCount = 10;
@@ -782,6 +292,28 @@ namespace ModBus.Net
public ushort WriteCount { get; private set; } public ushort WriteCount { get; private set; }
} }
/// <summary>
/// 写系统时间
/// </summary>
public class SetSystemTimeModbusProtocal : ProtocalUnit
{
public override byte[] Format(InputStruct message)
{
var r_message = (SetSystemTimeInputStruct)message;
return Format(r_message.BelongAddress, r_message.FunctionCode,
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.Year,
r_message.Day,
r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond);
}
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 SetSystemTimeOutputStruct(belongAddress, functionCode, startAddress, writeCount);
}
} }
public class ProtocalErrorException : Exception public class ProtocalErrorException : Exception

View File

@@ -51,7 +51,7 @@ namespace ModBus.Net
ModbusType = (ModbusType)connectionType; ModbusType = (ModbusType)connectionType;
} }
public ModbusUtility(string connectionString, int connectionType) public ModbusUtility(int connectionType, string connectionString)
{ {
ConnectionString = connectionString; ConnectionString = connectionString;
ModbusType = (ModbusType)connectionType; ModbusType = (ModbusType)connectionType;
@@ -67,50 +67,14 @@ namespace ModBus.Net
ModbusType = (ModbusType) connectionType; 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 try
{ {
var inputStruct = new ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct(belongAddress, startAddress, var inputStruct = new ReadDataInputStruct(belongAddress, (ModbusProtocalReadDataFunctionCode)functionCode, startAddress, getCount);
getCount);
var outputStruct = var outputStruct =
_wrapper.SendReceive(_wrapper["ReadCoilStatusModbusProtocal"], inputStruct) as _wrapper.SendReceive(_wrapper["ReadDataModbusProtocal"], inputStruct) as ReadDataOutputStruct;
ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct; return outputStruct.DataValue;
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;
} }
catch 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 try
{ {
var inputStruct = new WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct(belongAddress, var inputStruct = new WriteDataInputStruct(belongAddress,
startAddress, setContents); (ModbusProtocalWriteDataFunctionCode) functionCode, startAddress, setContents);
var outputStruct = var outputStruct =
_wrapper.SendReceive(_wrapper["WriteMultiRegisterModbusProtocal"], inputStruct) as _wrapper.SendReceive(_wrapper["WriteDataModbusProtocal"], inputStruct) as
WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct; WriteDataOutputStruct;
if (outputStruct.WriteCount != setContents.Length) return false; if (outputStruct.WriteCount != setContents.Length) return false;
return true; return true;
} }

View File

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

View File

@@ -7,11 +7,11 @@ namespace ModBus.Net
{ {
public abstract class ProtocalUnit : IProtocalFormatting 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; return this;
} }

View File

@@ -379,6 +379,13 @@ namespace ModBus.Net
return translation.ToArray(); 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) public bool GetBit(ushort number, ref int pos)
{ {
if (pos < 0 && pos > 15) throw new IndexOutOfRangeException(); if (pos < 0 && pos > 15) throw new IndexOutOfRangeException();

View File

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