2014-08-01 update 1
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
public enum ModbusProtocalReg
|
||||
using System;
|
||||
|
||||
public enum ModbusProtocalReg
|
||||
{
|
||||
ReadCoilStatus = 1,
|
||||
ReadInputStatus = 2,
|
||||
@@ -18,5 +20,736 @@ namespace ModBus.Net
|
||||
{
|
||||
public abstract class ModbusProtocal : BaseProtocal
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ReadCoilStatusModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
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;
|
||||
FunctionCode = (int)ModbusProtocalReg.ReadCoilStatus;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadInputStatusModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadInputStatusInputStruct)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 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 ReadInputStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
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 = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
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 = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
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 = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
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 = AddressTranslatorNA200H.GetInstance().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; }
|
||||
}
|
||||
}
|
||||
|
||||
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 = AddressTranslatorNA200H.GetInstance().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 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, ushort[] writeValue)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
WriteCount = (ushort)writeValue.Length;
|
||||
WriteByteCount = (byte)(WriteCount * 2);
|
||||
WriteValue = writeValue.Clone() as ushort[];
|
||||
}
|
||||
|
||||
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[] WriteValue { get; private set; }
|
||||
}
|
||||
|
||||
public class WriteMultiRegisterOutputStruct : OutputStruct
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
||||
public class GetSystemTimeModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (GetSystemTimeInputStruct)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 writeByteCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort year = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
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 GetSystemTimeInputStruct : InputStruct
|
||||
{
|
||||
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; }
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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 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 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; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,884 +2,11 @@
|
||||
|
||||
namespace ModBus.Net
|
||||
{
|
||||
public class ModbusTCPProtocal : ModbusProtocal
|
||||
public class ModbusTcpProtocal : ModbusProtocal
|
||||
{
|
||||
public ModbusTCPProtocal()
|
||||
public ModbusTcpProtocal()
|
||||
{
|
||||
_protocalLinker = new TCPProtocalLinker();
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadCoilStatusTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadCoilStatusInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, coilCount, coilStatusArr);
|
||||
}
|
||||
|
||||
public class ReadCoilStatusInputStruct : InputStruct
|
||||
{
|
||||
public ReadCoilStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.ReadCoilStatus;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
GetCount = getCount;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte coilCount, bool[] coilStatus)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
CoilCount = coilCount*8;
|
||||
CoilStatus = coilStatus.Clone() as bool[];
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
public int CoilCount { get; private set; }
|
||||
|
||||
public bool[] CoilStatus { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadInputStatusTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadInputStatusInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, inputCount, inputStatusArr);
|
||||
}
|
||||
|
||||
public class ReadInputStatusInputStruct : InputStruct
|
||||
{
|
||||
public ReadInputStatusInputStruct(byte belongAddress, string startAddress, ushort getCount)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.ReadInputStatus;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
GetCount = getCount;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte inputCount, bool[] inputStatus)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
InputCount = inputCount*8;
|
||||
InputStatus = inputStatus.Clone() as bool[];
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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 class ReadHoldRegisterTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadHoldRegisterInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, holdRegisterCount,
|
||||
holdRegisterArr);
|
||||
}
|
||||
|
||||
public class ReadHoldRegisterInputStruct : InputStruct
|
||||
{
|
||||
public ReadHoldRegisterInputStruct(byte belongAddress, string startAddress, ushort getCount)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.ReadHoldRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
GetCount = getCount;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte holdRegisterCount, ushort[] holdRegisterStatus)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
HoldRegisterCount = holdRegisterCount/2;
|
||||
HoldRegisterStatus = holdRegisterStatus.Clone() as ushort[];
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
public int HoldRegisterCount { get; private set; }
|
||||
|
||||
public ushort[] HoldRegisterStatus { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadInputRegisterTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadInputRegisterInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
if (messageBytes.Length == 25)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
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(tag, leng, belongAddress, functionCode, eventByteCount, soeProperty,
|
||||
soeEvent,
|
||||
new DateTime(year, month == 0 ? 1 : 0, day == 0 ? 1 : 0, hour, minute, second, millisecond),
|
||||
testPoint, testValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
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(tag, leng, belongAddress, functionCode, inputRegisterCount,
|
||||
holdRegisterArr);
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadEventOutputStruct : OutputStruct
|
||||
{
|
||||
public ReadEventOutputStruct(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte eventByteCount, byte soeProperty, byte soeEvent, DateTime time, ushort testPoint, byte testValue)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
EventByteCount = eventByteCount;
|
||||
SoeProperty = soeProperty;
|
||||
SoeEvent = soeEvent;
|
||||
TestTime = time;
|
||||
TestPoint = testPoint;
|
||||
TestValue = testValue;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.ReadInputRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
GetCount = getCount;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte inputRegisterCount, ushort[] inputRegisterStatus)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
InputRegisterCount = inputRegisterCount/2;
|
||||
InputRegisterStatus = inputRegisterStatus.Clone() as ushort[];
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
public int InputRegisterCount { get; private set; }
|
||||
|
||||
public ushort[] InputRegisterStatus { get; private set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class WriteOneCoilTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteOneCoilInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, 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)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, startAddress,
|
||||
writeValue == 0 ? false : true);
|
||||
}
|
||||
|
||||
public class WriteOneCoilInputStruct : InputStruct
|
||||
{
|
||||
public WriteOneCoilInputStruct(byte belongAddress, string startAddress, bool writeValue)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.WriteOneCoil;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
WriteValue = writeValue;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
ushort startAddress, bool writeValue)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteValue = writeValue;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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 WriteOneRegisterTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteOneRegisterInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.WriteValue);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, startAddress, writeValue);
|
||||
}
|
||||
|
||||
public class WriteOneRegisterInputStruct : InputStruct
|
||||
{
|
||||
public WriteOneRegisterInputStruct(byte belongAddress, string startAddress, ushort writeValue)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.WriteOneRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
WriteValue = writeValue;
|
||||
Leng = 6;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeValue)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteValue = writeValue;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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 WriteMultiCoilTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteMultiCoilInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, 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)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, startAddress, writeCount);
|
||||
}
|
||||
|
||||
public class WriteMultiCoilInputStruct : InputStruct
|
||||
{
|
||||
public WriteMultiCoilInputStruct(byte belongAddress, string startAddress, bool[] writeValue)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.WriteMultiCoil;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().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;
|
||||
}
|
||||
Leng = (ushort) (7 + WriteByteCount);
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
|
||||
public byte[] WriteValue { get; private set; }
|
||||
}
|
||||
|
||||
public class WriteMultiCoilOutputStruct : OutputStruct
|
||||
{
|
||||
public WriteMultiCoilOutputStruct(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeCount)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteCount = writeCount;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
}
|
||||
}
|
||||
|
||||
public class WriteMultiRegisterTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteMultiRegisterInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, 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)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, startAddress, writeCount);
|
||||
}
|
||||
|
||||
public class WriteMultiRegisterInputStruct : InputStruct
|
||||
{
|
||||
public WriteMultiRegisterInputStruct(byte belongAddress, string startAddress, ushort[] writeValue)
|
||||
{
|
||||
Tag = 0;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.WriteMultiRegister;
|
||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
||||
WriteCount = (ushort) writeValue.Length;
|
||||
WriteByteCount = (byte) (WriteCount*2);
|
||||
WriteValue = writeValue.Clone() as ushort[];
|
||||
Leng = (ushort) (7 + WriteByteCount);
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
|
||||
public ushort[] WriteValue { get; private set; }
|
||||
}
|
||||
|
||||
public class WriteMultiRegisterOutputStruct : OutputStruct
|
||||
{
|
||||
public WriteMultiRegisterOutputStruct(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeCount)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteCount = writeCount;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
}
|
||||
}
|
||||
|
||||
public class GetSystemTimeTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (GetSystemTimeInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, r_message.BelongAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
byte belongAddress = ValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte functionCode = ValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte writeByteCount = ValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort year = ValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
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(tag, leng, belongAddress, functionCode, writeByteCount, year, day,
|
||||
month, hour, second, minute, millisecond);
|
||||
}
|
||||
|
||||
public class GetSystemTimeInputStruct : InputStruct
|
||||
{
|
||||
public GetSystemTimeInputStruct(byte belongAddress)
|
||||
{
|
||||
Tag = 0;
|
||||
Leng = 6;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (int) ModbusProtocalReg.GetSystemTime;
|
||||
StartAddress = 30000;
|
||||
GetCount = 5;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { get; private set; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
|
||||
ushort millisecond)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
WriteByteCount = writeByteCount;
|
||||
Time = new DateTime(year, month, day, hour, minute, second, millisecond);
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
}
|
||||
}
|
||||
|
||||
public class SetSystemTimeTCPProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (SetSystemTimeInputStruct) message;
|
||||
return Format(r_message.Tag, r_message.Leng, 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)
|
||||
{
|
||||
int tag = ValueHelper.Instance.GetInt(messageBytes, ref flag);
|
||||
ushort leng = ValueHelper.Instance.GetUShort(messageBytes, ref 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(tag, leng, belongAddress, functionCode, startAddress, writeCount);
|
||||
}
|
||||
|
||||
public class SetSystemTimeInputStruct : InputStruct
|
||||
{
|
||||
public SetSystemTimeInputStruct(byte belongAddress, DateTime time)
|
||||
{
|
||||
Tag = 0;
|
||||
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;
|
||||
Leng = 17;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public short Leng { 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; }
|
||||
|
||||
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(int tag, ushort leng, byte belongAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeCount)
|
||||
{
|
||||
Tag = tag;
|
||||
Leng = leng;
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteCount = writeCount;
|
||||
}
|
||||
|
||||
public int Tag { get; private set; }
|
||||
|
||||
public ushort Leng { 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; }
|
||||
_protocalLinker = new TcpProtocalLinker();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,27 @@
|
||||
namespace ModBus.Net
|
||||
using System.Reflection;
|
||||
|
||||
namespace ModBus.Net
|
||||
{
|
||||
public abstract class ProtocalLinker
|
||||
{
|
||||
public abstract byte[] SendReceive(byte[] content);
|
||||
|
||||
public abstract bool SendOnly(byte[] content);
|
||||
|
||||
public byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
ProtocalLinkerBytesExtend bytesExtend =
|
||||
Assembly.Load("ModBus.Net").CreateInstance(this.GetType().FullName + "BytesExtend") as
|
||||
ProtocalLinkerBytesExtend;
|
||||
return bytesExtend.BytesExtend(content);
|
||||
}
|
||||
|
||||
public byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
ProtocalLinkerBytesExtend bytesExtend =
|
||||
Assembly.Load("ModBus.Net").CreateInstance(this.GetType().FullName + "BytesExtend") as
|
||||
ProtocalLinkerBytesExtend;
|
||||
return bytesExtend.BytesDecact(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,57 @@
|
||||
namespace ModBus.Net
|
||||
{
|
||||
public class TCPProtocalLinker : ProtocalLinker
|
||||
{
|
||||
private static TCPSocket _socket;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public TCPProtocalLinker()
|
||||
namespace ModBus.Net
|
||||
{
|
||||
public class TcpProtocalLinker : ProtocalLinker
|
||||
{
|
||||
private static TcpSocket _socket;
|
||||
|
||||
public TcpProtocalLinker()
|
||||
{
|
||||
if (_socket == null)
|
||||
{
|
||||
_socket = new TCPSocket(ConfigurationManager.IP, int.Parse(ConfigurationManager.Port), false);
|
||||
_socket = new TcpSocket(ConfigurationManager.IP, int.Parse(ConfigurationManager.Port), false);
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] SendReceive(byte[] content)
|
||||
{
|
||||
return _socket.SendMsg(content);
|
||||
return BytesDecact(_socket.SendMsg(BytesExtend(content)));
|
||||
}
|
||||
|
||||
public override bool SendOnly(byte[] content)
|
||||
{
|
||||
return _socket.SendMsgWithoutReturn(content);
|
||||
return _socket.SendMsgWithoutReturn(BytesExtend(content));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract class ProtocalLinkerBytesExtend
|
||||
{
|
||||
public abstract byte[] BytesExtend(byte[] content);
|
||||
|
||||
public abstract byte[] BytesDecact(byte[] content);
|
||||
}
|
||||
|
||||
public class TcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend
|
||||
{
|
||||
public override byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
byte[] newFormat = new byte[6 + content.Length];
|
||||
int tag = 0;
|
||||
ushort leng = (ushort)content.Length;
|
||||
Array.Copy(ValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4);
|
||||
Array.Copy(ValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2);
|
||||
Array.Copy(content, 0, newFormat, 6, content.Length);
|
||||
return newFormat;
|
||||
}
|
||||
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
byte[] newContent = new byte[content.Length - 6];
|
||||
Array.Copy(content, 6, newContent, 0, newContent.Length);
|
||||
return newContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ namespace ModBus.Net
|
||||
/// Socket收发类
|
||||
/// 作者:本类来源于CSDN,并由罗圣(Chris L.)根据实际需要修改
|
||||
/// </summary>
|
||||
public class TCPSocket : IDisposable
|
||||
public class TcpSocket : IDisposable
|
||||
{
|
||||
public delegate void ErrorShutdownEventHandler(object sender, EventArgs e);
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace ModBus.Net
|
||||
public int m_sendCount = 0;
|
||||
private TcpClient m_socketClient;
|
||||
|
||||
public TCPSocket(string ipaddress, int port, bool isAsync)
|
||||
public TcpSocket(string ipaddress, int port, bool isAsync)
|
||||
{
|
||||
host = ipaddress;
|
||||
this.port = port;
|
||||
|
||||
@@ -21,10 +21,10 @@ namespace NA200H.UI.ConsoleApp
|
||||
{
|
||||
public void Do()
|
||||
{
|
||||
BaseProtocal wrapper = new ModbusTCPProtocal();
|
||||
BaseProtocal wrapper = new ModbusTcpProtocal();
|
||||
|
||||
ReadCoilStatusTCPProtocal.ReadCoilStatusInputStruct readCoilStatusInputStruct = new ReadCoilStatusTCPProtocal.ReadCoilStatusInputStruct(0x11, "Q20", 0x25);
|
||||
ReadCoilStatusTCPProtocal.ReadCoilStatusOutputStruct readCoilStatusOutputStruct = (ReadCoilStatusTCPProtocal.ReadCoilStatusOutputStruct)wrapper.SendReceive(wrapper["ReadCoilStatusTCPProtocal"],readCoilStatusInputStruct);
|
||||
ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct readCoilStatusInputStruct = new ReadCoilStatusModbusProtocal.ReadCoilStatusInputStruct(0x11, "Q20", 0x25);
|
||||
ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct readCoilStatusOutputStruct = (ReadCoilStatusModbusProtocal.ReadCoilStatusOutputStruct)wrapper.SendReceive(wrapper["ReadCoilStatusModbusProtocal"], readCoilStatusInputStruct);
|
||||
for (int i = 0; i < readCoilStatusOutputStruct.CoilStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readCoilStatusOutputStruct.CoilStatus[i]);
|
||||
@@ -32,8 +32,8 @@ namespace NA200H.UI.ConsoleApp
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
|
||||
ReadInputStatusTCPProtocal.ReadInputStatusInputStruct readInputStatusInputStruct = new ReadInputStatusTCPProtocal.ReadInputStatusInputStruct(0x11, "I20", 0x25);
|
||||
ReadInputStatusTCPProtocal.ReadInputStatusOutputStruct readInputStatusOutputStruct = (ReadInputStatusTCPProtocal.ReadInputStatusOutputStruct)wrapper.SendReceive(wrapper["ReadInputStatusTCPProtocal"],readInputStatusInputStruct);
|
||||
ReadInputStatusModbusProtocal.ReadInputStatusInputStruct readInputStatusInputStruct = new ReadInputStatusModbusProtocal.ReadInputStatusInputStruct(0x11, "I20", 0x25);
|
||||
ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct readInputStatusOutputStruct = (ReadInputStatusModbusProtocal.ReadInputStatusOutputStruct)wrapper.SendReceive(wrapper["ReadInputStatusModbusProtocal"],readInputStatusInputStruct);
|
||||
for (int i = 0; i < readInputStatusOutputStruct.InputStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readInputStatusOutputStruct.InputStatus[i]);
|
||||
@@ -42,8 +42,8 @@ namespace NA200H.UI.ConsoleApp
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadHoldRegisterTCPProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterTCPProtocal.ReadHoldRegisterInputStruct(0x11, "MW1", 8);
|
||||
ReadHoldRegisterTCPProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterTCPProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterTCPProtocal"],readHoldRegisterInputStruct);
|
||||
ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct readHoldRegisterInputStruct = new ReadHoldRegisterModbusProtocal.ReadHoldRegisterInputStruct(0x11, "MW1", 8);
|
||||
ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct readHoldRegisterOutputStruct = (ReadHoldRegisterModbusProtocal.ReadHoldRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadHoldRegisterModbusProtocal"],readHoldRegisterInputStruct);
|
||||
for (int i = 0; i < readHoldRegisterOutputStruct.HoldRegisterStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readHoldRegisterOutputStruct.HoldRegisterStatus[i]);
|
||||
@@ -52,8 +52,8 @@ namespace NA200H.UI.ConsoleApp
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadInputRegisterTCPProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct = new ReadInputRegisterTCPProtocal.ReadInputRegisterInputStruct(0x11, "IW1", 3);
|
||||
ReadInputRegisterTCPProtocal.ReadInputRegisterOutputStruct readInputRegisterOutputStruct = (ReadInputRegisterTCPProtocal.ReadInputRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterTCPProtocal"], readInputRegisterInputStruct);
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct = new ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct(0x11, "IW1", 3);
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterOutputStruct readInputRegisterOutputStruct = (ReadInputRegisterModbusProtocal.ReadInputRegisterOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterModbusProtocal"], readInputRegisterInputStruct);
|
||||
for (int i = 0; i < readInputRegisterOutputStruct.InputRegisterStatus.Length; i++)
|
||||
{
|
||||
Console.WriteLine(readInputRegisterOutputStruct.InputRegisterStatus[i]);
|
||||
@@ -62,54 +62,54 @@ namespace NA200H.UI.ConsoleApp
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
ReadInputRegisterTCPProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct2 = new ReadInputRegisterTCPProtocal.ReadInputRegisterInputStruct(0x11, "E38", 8);
|
||||
ReadInputRegisterTCPProtocal.ReadEventOutputStruct readEventOutputStruct = (ReadInputRegisterTCPProtocal.ReadEventOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterTCPProtocal"], readInputRegisterInputStruct2);
|
||||
ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct readInputRegisterInputStruct2 = new ReadInputRegisterModbusProtocal.ReadInputRegisterInputStruct(0x11, "E38", 8);
|
||||
ReadInputRegisterModbusProtocal.ReadEventOutputStruct readEventOutputStruct = (ReadInputRegisterModbusProtocal.ReadEventOutputStruct)wrapper.SendReceive(wrapper["ReadInputRegisterModbusProtocal"], readInputRegisterInputStruct2);
|
||||
Console.WriteLine(readEventOutputStruct.SoeEvent);
|
||||
Console.WriteLine(readEventOutputStruct.TestTime);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteOneCoilTCPProtocal.WriteOneCoilInputStruct writeOneCoilInputStruct = new WriteOneCoilTCPProtocal.WriteOneCoilInputStruct(0x11, "Q173", true);
|
||||
WriteOneCoilTCPProtocal.WriteOneCoilOutputStruct writeOneCoilOutputStruct = (WriteOneCoilTCPProtocal.WriteOneCoilOutputStruct)wrapper.SendReceive(wrapper["WriteOneCoilTCPProtocal"], writeOneCoilInputStruct);
|
||||
WriteOneCoilModbusProtocal.WriteOneCoilInputStruct writeOneCoilInputStruct = new WriteOneCoilModbusProtocal.WriteOneCoilInputStruct(0x11, "Q173", true);
|
||||
WriteOneCoilModbusProtocal.WriteOneCoilOutputStruct writeOneCoilOutputStruct = (WriteOneCoilModbusProtocal.WriteOneCoilOutputStruct)wrapper.SendReceive(wrapper["WriteOneCoilModbusProtocal"], writeOneCoilInputStruct);
|
||||
Console.WriteLine(writeOneCoilOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeOneCoilOutputStruct.WriteValue);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteOneRegisterTCPProtocal.WriteOneRegisterInputStruct writeOneRegisterInputStruct = new WriteOneRegisterTCPProtocal.WriteOneRegisterInputStruct(0x11, "NW1", 100);
|
||||
WriteOneRegisterTCPProtocal.WriteOneRegisterOutputStruct writeOneRegisterOutputStruct = (WriteOneRegisterTCPProtocal.WriteOneRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteOneRegisterTCPProtocal"], writeOneRegisterInputStruct);
|
||||
WriteOneRegisterModbusProtocal.WriteOneRegisterInputStruct writeOneRegisterInputStruct = new WriteOneRegisterModbusProtocal.WriteOneRegisterInputStruct(0x11, "NW1", 100);
|
||||
WriteOneRegisterModbusProtocal.WriteOneRegisterOutputStruct writeOneRegisterOutputStruct = (WriteOneRegisterModbusProtocal.WriteOneRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteOneRegisterModbusProtocal"], writeOneRegisterInputStruct);
|
||||
Console.WriteLine(writeOneRegisterOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeOneRegisterOutputStruct.WriteValue);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteMultiCoilTCPProtocal.WriteMultiCoilInputStruct writeMultiCoilInputStruct = new WriteMultiCoilTCPProtocal.WriteMultiCoilInputStruct(0x11, "Q20", new bool[]{true, false, true, true, false, false, true, true, true, false});
|
||||
WriteMultiCoilTCPProtocal.WriteMultiCoilOutputStruct writeMultiCoilOutputStruct = (WriteMultiCoilTCPProtocal.WriteMultiCoilOutputStruct)wrapper.SendReceive(wrapper["WriteMultiCoilTCPProtocal"], writeMultiCoilInputStruct);
|
||||
WriteMultiCoilModbusProtocal.WriteMultiCoilInputStruct writeMultiCoilInputStruct = new WriteMultiCoilModbusProtocal.WriteMultiCoilInputStruct(0x11, "Q20", new bool[]{true, false, true, true, false, false, true, true, true, false});
|
||||
WriteMultiCoilModbusProtocal.WriteMultiCoilOutputStruct writeMultiCoilOutputStruct = (WriteMultiCoilModbusProtocal.WriteMultiCoilOutputStruct)wrapper.SendReceive(wrapper["WriteMultiCoilModbusProtocal"], writeMultiCoilInputStruct);
|
||||
Console.WriteLine(writeMultiCoilOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeMultiCoilOutputStruct.WriteCount);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
WriteMultiRegisterTCPProtocal.WriteMultiRegisterInputStruct writeMultiRegisterInputStruct = new WriteMultiRegisterTCPProtocal.WriteMultiRegisterInputStruct(0x11, "MW2", new ushort[]{0x000A,0x0102});
|
||||
WriteMultiRegisterTCPProtocal.WriteMultiRegisterOutputStruct writeMultiRegisterOutputStruct = (WriteMultiRegisterTCPProtocal.WriteMultiRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteMultiRegisterTCPProtocal"], writeMultiRegisterInputStruct);
|
||||
WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct writeMultiRegisterInputStruct = new WriteMultiRegisterModbusProtocal.WriteMultiRegisterInputStruct(0x11, "MW2", new ushort[]{0x000A,0x0102});
|
||||
WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct writeMultiRegisterOutputStruct = (WriteMultiRegisterModbusProtocal.WriteMultiRegisterOutputStruct)wrapper.SendReceive(wrapper["WriteMultiRegisterModbusProtocal"], writeMultiRegisterInputStruct);
|
||||
Console.WriteLine(writeMultiRegisterOutputStruct.StartAddress);
|
||||
Console.WriteLine(writeMultiRegisterOutputStruct.WriteCount);
|
||||
Console.WriteLine();
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
GetSystemTimeTCPProtocal.GetSystemTimeInputStruct getSystemTimeInputStruct = new GetSystemTimeTCPProtocal.GetSystemTimeInputStruct(0x11);
|
||||
GetSystemTimeTCPProtocal.GetSystemTimeOutputStruct getSystemTimeOutputStruct = (GetSystemTimeTCPProtocal.GetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["GetSystemTimeTCPProtocal"], getSystemTimeInputStruct);
|
||||
GetSystemTimeModbusProtocal.GetSystemTimeInputStruct getSystemTimeInputStruct = new GetSystemTimeModbusProtocal.GetSystemTimeInputStruct(0x11);
|
||||
GetSystemTimeModbusProtocal.GetSystemTimeOutputStruct getSystemTimeOutputStruct = (GetSystemTimeModbusProtocal.GetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["GetSystemTimeModbusProtocal"], getSystemTimeInputStruct);
|
||||
Console.WriteLine(getSystemTimeOutputStruct.Time);
|
||||
Console.Read();
|
||||
Console.Read();
|
||||
|
||||
SetSystemTimeTCPProtocal.SetSystemTimeInputStruct setSystemTimeInputStruct = new SetSystemTimeTCPProtocal.SetSystemTimeInputStruct(0x11, DateTime.Now);
|
||||
SetSystemTimeTCPProtocal.SetSystemTimeOutputStruct setSystemTimeOutputStruct = (SetSystemTimeTCPProtocal.SetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["SetSystemTimeTCPProtocal"], setSystemTimeInputStruct);
|
||||
SetSystemTimeModbusProtocal.SetSystemTimeInputStruct setSystemTimeInputStruct = new SetSystemTimeModbusProtocal.SetSystemTimeInputStruct(0x11, DateTime.Now);
|
||||
SetSystemTimeModbusProtocal.SetSystemTimeOutputStruct setSystemTimeOutputStruct = (SetSystemTimeModbusProtocal.SetSystemTimeOutputStruct)wrapper.SendReceive(wrapper["SetSystemTimeModbusProtocal"], setSystemTimeInputStruct);
|
||||
Console.WriteLine(setSystemTimeOutputStruct.StartAddress);
|
||||
Console.WriteLine(setSystemTimeOutputStruct.WriteCount);
|
||||
Console.Read();
|
||||
|
||||
Reference in New Issue
Block a user