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,572 +1,150 @@
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
{ {
} }
/// <summary> public class ReadDataInputStruct : InputStruct
/// 读线圈状态 {
/// </summary> public ReadDataInputStruct(byte belongAddress, ModbusProtocalReadDataFunctionCode functionCode, string startAddress, ushort getCount)
public class ReadCoilStatusModbusProtocal : ProtocalUnit {
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) public override byte[] Format(InputStruct message)
{ {
var r_message = (ReadCoilStatusInputStruct)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 coilCount = ValueHelper.Instance.GetByte(messageBytes, ref flag); byte dataCount = ValueHelper.Instance.GetByte(messageBytes, ref pos);
var coilStatusArr = new bool[coilCount * 8]; byte[] dataValue = new byte[dataCount];
for (int i = 0; i < coilCount; i++) Array.Copy(messageBytes, 3, dataValue, 0, dataCount);
{ return new ReadDataOutputStruct(belongAddress, functionCode, dataCount, dataValue);
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; }
} }
} }
/// <summary> public class WriteDataInputStruct : InputStruct
/// 读输入状态协
/// </summary>
public class ReadInputStatusModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public WriteDataInputStruct(byte belongAddress, ModbusProtocalWriteDataFunctionCode functionCode, string startAddress, object[] writeValue)
{ {
var r_message = (ReadInputStatusInputStruct)message; BelongAddress = belongAddress;
return Format(r_message.BelongAddress, r_message.FunctionCode, FunctionCode = (byte)functionCode;
r_message.StartAddress, r_message.GetCount); 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) public byte BelongAddress { get; private set; }
{
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 class ReadInputStatusInputStruct : InputStruct public byte FunctionCode { get; private set; }
{
public ReadInputStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
{
BelongAddress = belongAddress;
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus;
StartAddress = _addressTranslator.AddressTranslate(startAddress);
GetCount = getCount;
}
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 object[] WriteValue { 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> public class WriteDataOutputStruct : OutputStruct
/// 读保持型寄存器
/// </summary>
public class ReadHoldRegisterModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public WriteDataOutputStruct(byte belongAddress, byte functionCode,
ushort startAddress, ushort writeCount)
{ {
var r_message = (ReadHoldRegisterInputStruct)message; BelongAddress = belongAddress;
return Format(r_message.BelongAddress, r_message.FunctionCode, FunctionCode = functionCode;
r_message.StartAddress, r_message.GetCount); StartAddress = startAddress;
WriteCount = writeCount;
} }
public override OutputStruct Unformat(byte[] messageBytes, ref int flag) public byte BelongAddress { get; private set; }
{
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 byte FunctionCode { get; private set; }
{
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 ushort StartAddress { get; private set; }
public byte FunctionCode { get; private set; } public ushort WriteCount { 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>
/// 写多个寄存器状态 /// 写多个寄存器状态
/// </summary> /// </summary>
public class WriteMultiRegisterModbusProtocal : ProtocalUnit public class WriteDataModbusProtocal : ProtocalUnit
{ {
public override byte[] Format(InputStruct message) public override byte[] Format(InputStruct message)
{ {
var r_message = (WriteMultiRegisterInputStruct)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.WriteCount, r_message.WriteByteCount, r_message.WriteValue); 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); byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag); ushort startAddress = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
ushort writeCount = 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); writeCount);
} }
}
public class WriteMultiRegisterInputStruct : InputStruct public class GetSystemTimeInputStruct : InputStruct
{
public GetSystemTimeInputStruct(byte belongAddress)
{ {
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, object[] writeValue) BelongAddress = belongAddress;
{ FunctionCode = (byte)ModbusProtocalTimeFunctionCode.GetSystemTime;
BelongAddress = belongAddress; StartAddress = 30000;
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister; GetCount = 5;
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; }
} }
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, BelongAddress = belongAddress;
ushort startAddress, ushort writeCount) FunctionCode = functionCode;
{ WriteByteCount = writeByteCount;
BelongAddress = belongAddress; Time = new DateTime(year, month, day, hour, minute, second, millisecond);
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 byte BelongAddress { get; private set; }
public byte FunctionCode { get; private set; }
public byte WriteByteCount { get; private set; }
public DateTime Time { get; private set; }
} }
/// <summary> /// <summary>
@@ -654,46 +227,69 @@ namespace ModBus.Net
return new GetSystemTimeOutputStruct(belongAddress, functionCode, writeByteCount, year, day, return new GetSystemTimeOutputStruct(belongAddress, functionCode, writeByteCount, year, day,
month, hour, second, minute, millisecond); 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 = (byte)ModbusProtocalTimeFunctionCode.SetSystemTime;
BelongAddress = belongAddress; StartAddress = 30000;
FunctionCode = (int)ModbusProtocalReg.GetSystemTime; WriteCount = 5;
StartAddress = 30000; WriteByteCount = 10;
GetCount = 5; Year = (ushort)time.Year;
} Day = (byte)time.Day;
Month = (byte)time.Month;
public byte BelongAddress { get; private set; } Hour = (ushort)time.Hour;
Second = (byte)time.Second;
public byte FunctionCode { get; private set; } Minute = (byte)time.Minute;
Millisecond = (ushort)time.Millisecond;
public ushort StartAddress { get; private set; }
public ushort GetCount { get; private set; }
} }
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, BelongAddress = belongAddress;
byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute, FunctionCode = functionCode;
ushort millisecond) StartAddress = startAddress;
{ WriteCount = writeCount;
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; }
} }
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>
@@ -718,70 +314,6 @@ namespace ModBus.Net
ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag); ushort writeCount = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
return new SetSystemTimeOutputStruct(belongAddress, functionCode, startAddress, writeCount); 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 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);
} }
} }
} }