2017-02-06 update 1 Add AddressHelper and comments, reformat code.
This commit is contained in:
@@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
public class AddressFormaterNA200H : AddressFormater
|
||||
{
|
||||
@@ -28,7 +22,7 @@ namespace Modbus.Net.Modbus
|
||||
|
||||
public override string FormatAddress(string area, int address, int subAddress)
|
||||
{
|
||||
return area + " " + address + "." + subAddress;
|
||||
return area + " " + address + "." + subAddress;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// NA200H数据单元翻译器
|
||||
/// NA200H数据单元翻译器
|
||||
/// </summary>
|
||||
public class AddressTranslatorNA200H : AddressTranslator
|
||||
{
|
||||
protected Dictionary<string, int> TransDictionary;
|
||||
protected Dictionary<string, AreaOutputDef> ReadFunctionCodeDictionary;
|
||||
protected Dictionary<string, int> TransDictionary;
|
||||
protected Dictionary<string, AreaOutputDef> WriteFunctionCodeDictionary;
|
||||
|
||||
public AddressTranslatorNA200H()
|
||||
@@ -26,44 +23,136 @@ namespace Modbus.Net.Modbus
|
||||
{"S", 10000},
|
||||
{"IW", 0},
|
||||
{"SW", 5000},
|
||||
{"MW", 0},
|
||||
{"MW", 0},
|
||||
{"QW", 20000},
|
||||
{"NW", 21000},
|
||||
{"NW", 21000}
|
||||
};
|
||||
ReadFunctionCodeDictionary = new Dictionary<string, AreaOutputDef>
|
||||
{
|
||||
{"Q", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125}},
|
||||
{"M", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125}},
|
||||
{"N", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125}},
|
||||
{"I", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125}},
|
||||
{"S", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125}},
|
||||
{"IW", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}},
|
||||
{"SW", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}},
|
||||
{"MW", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}},
|
||||
{"NW", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}},
|
||||
{"QW", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}},
|
||||
{
|
||||
"Q",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"M",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"N",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"I",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"S",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"IW",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
|
||||
},
|
||||
{
|
||||
"SW",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
|
||||
},
|
||||
{
|
||||
"MW",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
|
||||
},
|
||||
{
|
||||
"NW",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
|
||||
},
|
||||
{
|
||||
"QW",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
|
||||
}
|
||||
};
|
||||
WriteFunctionCodeDictionary = new Dictionary<string, AreaOutputDef>
|
||||
{
|
||||
{"Q", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125}},
|
||||
{"M", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125}},
|
||||
{"N", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125}},
|
||||
{"MW", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2}},
|
||||
{"NW", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2}},
|
||||
{"QW", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2}},
|
||||
{
|
||||
"Q",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"M",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"N",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"MW",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister,
|
||||
AreaWidth = 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"NW",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister,
|
||||
AreaWidth = 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"QW",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister,
|
||||
AreaWidth = 2
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override AddressDef AddressTranslate(string address, bool isRead)
|
||||
{
|
||||
address = address.ToUpper();
|
||||
string[] splitString = address.Split(' ');
|
||||
string head = splitString[0];
|
||||
string tail = splitString[1];
|
||||
var splitString = address.Split(' ');
|
||||
var head = splitString[0];
|
||||
var tail = splitString[1];
|
||||
string sub;
|
||||
if (tail.Contains('.'))
|
||||
{
|
||||
string[] splitString2 = tail.Split('.');
|
||||
var splitString2 = tail.Split('.');
|
||||
sub = splitString2[1];
|
||||
tail = splitString2[0];
|
||||
}
|
||||
@@ -72,19 +161,19 @@ namespace Modbus.Net.Modbus
|
||||
sub = "0";
|
||||
}
|
||||
return isRead
|
||||
? new AddressDef()
|
||||
? new AddressDef
|
||||
{
|
||||
AreaString = head,
|
||||
Area = ReadFunctionCodeDictionary[head].Code,
|
||||
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
||||
SubAddress = int.Parse(sub),
|
||||
SubAddress = int.Parse(sub)
|
||||
}
|
||||
: new AddressDef()
|
||||
: new AddressDef
|
||||
{
|
||||
AreaString = head,
|
||||
Area = WriteFunctionCodeDictionary[head].Code,
|
||||
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
||||
SubAddress = int.Parse(sub),
|
||||
SubAddress = int.Parse(sub)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -95,7 +184,7 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modbus数据单元翻译器
|
||||
/// Modbus数据单元翻译器
|
||||
/// </summary>
|
||||
public class AddressTranslatorModbus : AddressTranslator
|
||||
{
|
||||
@@ -106,28 +195,62 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
ReadFunctionCodeDictionary = new Dictionary<string, AreaOutputDef>
|
||||
{
|
||||
{"0X", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadCoilStatus, AreaWidth = 0.125}},
|
||||
{"1X", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputStatus, AreaWidth = 0.125}},
|
||||
{"3X", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}},
|
||||
{"4X", new AreaOutputDef {Code = (int)ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}},
|
||||
{
|
||||
"0X",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadCoilStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"1X",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputStatus,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"3X",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
|
||||
},
|
||||
{
|
||||
"4X",
|
||||
new AreaOutputDef {Code = (int) ModbusProtocalReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
|
||||
}
|
||||
};
|
||||
WriteFunctionCodeDictionary = new Dictionary<string, AreaOutputDef>
|
||||
{
|
||||
{"0X", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiCoil, AreaWidth = 0.125}},
|
||||
{"4X", new AreaOutputDef {Code = (int)ModbusProtocalWriteDataFunctionCode.WriteMultiRegister, AreaWidth = 2}},
|
||||
{
|
||||
"0X",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiCoil,
|
||||
AreaWidth = 0.125
|
||||
}
|
||||
},
|
||||
{
|
||||
"4X",
|
||||
new AreaOutputDef
|
||||
{
|
||||
Code = (int) ModbusProtocalWriteDataFunctionCode.WriteMultiRegister,
|
||||
AreaWidth = 2
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override AddressDef AddressTranslate(string address, bool isRead)
|
||||
{
|
||||
address = address.ToUpper();
|
||||
string[] splitString = address.Split(' ');
|
||||
string head = splitString[0];
|
||||
string tail = splitString[1];
|
||||
var splitString = address.Split(' ');
|
||||
var head = splitString[0];
|
||||
var tail = splitString[1];
|
||||
string sub;
|
||||
if (tail.Contains('.'))
|
||||
{
|
||||
string[] splitString2 = tail.Split('.');
|
||||
var splitString2 = tail.Split('.');
|
||||
sub = splitString2[1];
|
||||
tail = splitString2[0];
|
||||
}
|
||||
@@ -136,19 +259,19 @@ namespace Modbus.Net.Modbus
|
||||
sub = "0";
|
||||
}
|
||||
return isRead
|
||||
? new AddressDef()
|
||||
? new AddressDef
|
||||
{
|
||||
AreaString = head,
|
||||
Area = ReadFunctionCodeDictionary[head].Code,
|
||||
Address = int.Parse(tail) - 1,
|
||||
SubAddress = int.Parse(sub),
|
||||
SubAddress = int.Parse(sub)
|
||||
}
|
||||
: new AddressDef()
|
||||
: new AddressDef
|
||||
{
|
||||
AreaString = head,
|
||||
Area = WriteFunctionCodeDictionary[head].Code,
|
||||
Address = int.Parse(tail) - 1,
|
||||
SubAddress = int.Parse(sub),
|
||||
SubAddress = int.Parse(sub)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -157,4 +280,4 @@ namespace Modbus.Net.Modbus
|
||||
return ReadFunctionCodeDictionary[area].AreaWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Rtu协议
|
||||
/// Modbus/Rtu协议
|
||||
/// </summary>
|
||||
public class ModbusAsciiProtocal : ModbusProtocal
|
||||
{
|
||||
public ModbusAsciiProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.COM, belongAddress, masterAddress)
|
||||
public ModbusAsciiProtocal(byte slaveAddress, byte masterAddress)
|
||||
: this(ConfigurationManager.COM, slaveAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusAsciiProtocal(string com, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
public ModbusAsciiProtocal(string com, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocalLinker = new ModbusAsciiProtocalLinker(com);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.IO.Ports;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
public class ModbusAsciiProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
public ModbusAsciiProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
//CRC校验失败
|
||||
string contentString = Encoding.ASCII.GetString(content);
|
||||
var contentString = Encoding.ASCII.GetString(content);
|
||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
||||
{
|
||||
throw new ModbusProtocalErrorException(501);
|
||||
@@ -25,9 +25,5 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ModbusAsciiProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,4 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,40 +7,44 @@ namespace Modbus.Net.Modbus
|
||||
internal enum ModbusProtocalVariableFunctionCode : byte
|
||||
{
|
||||
ReadVariable = 20,
|
||||
WriteVariable = 21,
|
||||
WriteVariable = 21
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 跟时间有关的功能码
|
||||
/// 跟时间有关的功能码
|
||||
/// </summary>
|
||||
public enum ModbusProtocalTimeFunctionCode : byte
|
||||
{
|
||||
GetSystemTime = 3,
|
||||
SetSystemTime = 16,
|
||||
SetSystemTime = 16
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 跟读数据有关的功能码
|
||||
/// 跟读数据有关的功能码
|
||||
/// </summary>
|
||||
public enum ModbusProtocalReadDataFunctionCode : byte
|
||||
{
|
||||
ReadCoilStatus = 1,
|
||||
ReadInputStatus = 2,
|
||||
ReadHoldRegister = 3,
|
||||
ReadInputRegister = 4,
|
||||
ReadInputRegister = 4
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 跟写数据有关的功能码
|
||||
/// 跟写数据有关的功能码
|
||||
/// </summary>
|
||||
internal enum ModbusProtocalWriteDataFunctionCode : byte
|
||||
{
|
||||
WriteMultiCoil = 15,
|
||||
WriteMultiRegister = 16,
|
||||
WriteMultiRegister = 16
|
||||
}
|
||||
|
||||
public abstract class ModbusProtocal : BaseProtocal
|
||||
{
|
||||
protected ModbusProtocal(byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool Connect()
|
||||
{
|
||||
return ProtocalLinker.Connect();
|
||||
@@ -50,45 +54,43 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
return await ProtocalLinker.ConnectAsync();
|
||||
}
|
||||
|
||||
protected ModbusProtocal(byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#region 读PLC数据
|
||||
|
||||
public class ReadDataModbusInputStruct : InputStruct
|
||||
{
|
||||
public ReadDataModbusInputStruct(byte belongAddress, string startAddress, ushort getCount, AddressTranslator addressTranslator)
|
||||
public ReadDataModbusInputStruct(byte slaveAddress, string startAddress, ushort getCount,
|
||||
AddressTranslator addressTranslator)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
var translateAddress = addressTranslator.AddressTranslate(startAddress, true);
|
||||
FunctionCode = (byte)translateAddress.Area;
|
||||
StartAddress = (ushort)translateAddress.Address;
|
||||
GetCount = (ushort)Math.Ceiling(getCount / addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
FunctionCode = (byte) translateAddress.Area;
|
||||
StartAddress = (ushort) translateAddress.Address;
|
||||
GetCount = (ushort) Math.Ceiling(getCount/addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
public byte FunctionCode { get; }
|
||||
|
||||
public ushort StartAddress { get; private set; }
|
||||
public ushort StartAddress { get; }
|
||||
|
||||
public ushort GetCount { get; private set; }
|
||||
public ushort GetCount { get; }
|
||||
}
|
||||
|
||||
public class ReadDataModbusOutputStruct : OutputStruct
|
||||
{
|
||||
public ReadDataModbusOutputStruct(byte belongAddress, byte functionCode,
|
||||
public ReadDataModbusOutputStruct(byte slaveAddress, byte functionCode,
|
||||
int dataCount, byte[] dataValue)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = functionCode;
|
||||
DataCount = dataCount;
|
||||
DataValue = dataValue.Clone() as byte[];
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
@@ -101,71 +103,74 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (ReadDataModbusInputStruct)message;
|
||||
return Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
var r_message = (ReadDataModbusInputStruct) message;
|
||||
return Format(r_message.SlaveAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int pos)
|
||||
{
|
||||
byte belongAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
byte functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
byte dataCount = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
byte[] dataValue = new byte[dataCount];
|
||||
var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
var dataCount = BigEndianValueHelper.Instance.GetByte(messageBytes, ref pos);
|
||||
var dataValue = new byte[dataCount];
|
||||
Array.Copy(messageBytes, 3, dataValue, 0, dataCount);
|
||||
if (functionCode == 1 || functionCode == 2)
|
||||
{
|
||||
for (int i = 0; i < dataValue.Length; i++)
|
||||
for (var i = 0; i < dataValue.Length; i++)
|
||||
{
|
||||
dataValue[i] = BigEndianValueHelper.Instance.ReverseByte(dataValue[i]);
|
||||
}
|
||||
}
|
||||
return new ReadDataModbusOutputStruct(belongAddress, functionCode, dataCount, dataValue);
|
||||
return new ReadDataModbusOutputStruct(slaveAddress, functionCode, dataCount, dataValue);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 写PLC数据
|
||||
|
||||
public class WriteDataModbusInputStruct : InputStruct
|
||||
{
|
||||
public WriteDataModbusInputStruct(byte belongAddress, string startAddress, object[] writeValue, AddressTranslator addressTranslator)
|
||||
public WriteDataModbusInputStruct(byte slaveAddress, string startAddress, object[] writeValue,
|
||||
AddressTranslator addressTranslator)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
var translateAddress = addressTranslator.AddressTranslate(startAddress, false);
|
||||
FunctionCode = (byte)translateAddress.Area;
|
||||
StartAddress = (ushort)translateAddress.Address;
|
||||
FunctionCode = (byte) translateAddress.Area;
|
||||
StartAddress = (ushort) translateAddress.Address;
|
||||
var writeByteValue = BigEndianValueHelper.Instance.ObjectArrayToByteArray(writeValue);
|
||||
WriteCount = (ushort)(writeByteValue.Length / addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
WriteByteCount = (byte)writeByteValue.Length;
|
||||
WriteCount =
|
||||
(ushort) (writeByteValue.Length/addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
WriteByteCount = (byte) writeByteValue.Length;
|
||||
WriteValue = writeByteValue;
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
public byte FunctionCode { get; }
|
||||
|
||||
public ushort StartAddress { get; private set; }
|
||||
public ushort StartAddress { get; }
|
||||
|
||||
public ushort WriteCount { get; private set; }
|
||||
public ushort WriteCount { get; }
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
public byte WriteByteCount { get; }
|
||||
|
||||
public byte[] WriteValue { get; private set; }
|
||||
public byte[] WriteValue { get; }
|
||||
}
|
||||
|
||||
public class WriteDataModbusOutputStruct : OutputStruct
|
||||
{
|
||||
public WriteDataModbusOutputStruct(byte belongAddress, byte functionCode,
|
||||
public WriteDataModbusOutputStruct(byte slaveAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeCount)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteCount = writeCount;
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
@@ -175,34 +180,34 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写多个寄存器状态
|
||||
/// 写多个寄存器状态
|
||||
/// </summary>
|
||||
public class WriteDataModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (WriteDataModbusInputStruct)message;
|
||||
var r_message = (WriteDataModbusInputStruct) message;
|
||||
var functionCode = r_message.FunctionCode;
|
||||
byte[] dataValue = Format(r_message.WriteValue);
|
||||
var dataValue = Format(r_message.WriteValue);
|
||||
if (functionCode == 5 || functionCode == 15)
|
||||
{
|
||||
for (int i = 0; i < dataValue.Length; i++)
|
||||
for (var i = 0; i < dataValue.Length; i++)
|
||||
{
|
||||
dataValue[i] = BigEndianValueHelper.Instance.ReverseByte(dataValue[i]);
|
||||
}
|
||||
}
|
||||
byte[] formattingBytes = Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
var formattingBytes = Format(r_message.SlaveAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, dataValue);
|
||||
return formattingBytes;
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
byte belongAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort startAddress = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
ushort writeCount = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new WriteDataModbusOutputStruct(belongAddress, functionCode, startAddress,
|
||||
var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var startAddress = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
var writeCount = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new WriteDataModbusOutputStruct(slaveAddress, functionCode, startAddress,
|
||||
writeCount);
|
||||
}
|
||||
}
|
||||
@@ -210,38 +215,39 @@ namespace Modbus.Net.Modbus
|
||||
#endregion
|
||||
|
||||
#region 读PLC时间
|
||||
|
||||
public class GetSystemTimeModbusInputStruct : InputStruct
|
||||
{
|
||||
public GetSystemTimeModbusInputStruct(byte belongAddress)
|
||||
public GetSystemTimeModbusInputStruct(byte slaveAddress)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (byte)ModbusProtocalTimeFunctionCode.GetSystemTime;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = (byte) ModbusProtocalTimeFunctionCode.GetSystemTime;
|
||||
StartAddress = 30000;
|
||||
GetCount = 5;
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
public byte FunctionCode { get; }
|
||||
|
||||
public ushort StartAddress { get; private set; }
|
||||
public ushort StartAddress { get; }
|
||||
|
||||
public ushort GetCount { get; private set; }
|
||||
public ushort GetCount { get; }
|
||||
}
|
||||
|
||||
public class GetSystemTimeModbusOutputStruct : OutputStruct
|
||||
{
|
||||
public GetSystemTimeModbusOutputStruct(byte belongAddress, byte functionCode,
|
||||
public GetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
|
||||
byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
|
||||
ushort millisecond)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = functionCode;
|
||||
WriteByteCount = writeByteCount;
|
||||
Time = new DateTime(year, month, day, hour, minute, second, millisecond);
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
@@ -251,30 +257,30 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读系统时间
|
||||
/// 读系统时间
|
||||
/// </summary>
|
||||
public class GetSystemTimeModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (GetSystemTimeModbusInputStruct)message;
|
||||
return Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
var r_message = (GetSystemTimeModbusInputStruct) message;
|
||||
return Format(r_message.SlaveAddress, r_message.FunctionCode,
|
||||
r_message.StartAddress, r_message.GetCount);
|
||||
}
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
byte belongAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte writeByteCount = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort year = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
byte day = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte month = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort hour = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
byte second = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte minute = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort millisecond = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new GetSystemTimeModbusOutputStruct(belongAddress, functionCode, writeByteCount, year, day,
|
||||
var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var writeByteCount = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var year = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
var day = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var month = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var hour = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
var second = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var minute = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var millisecond = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new GetSystemTimeModbusOutputStruct(slaveAddress, functionCode, writeByteCount, year, day,
|
||||
month, hour, second, minute, millisecond);
|
||||
}
|
||||
}
|
||||
@@ -282,61 +288,62 @@ namespace Modbus.Net.Modbus
|
||||
#endregion
|
||||
|
||||
#region 写PLC时间
|
||||
|
||||
public class SetSystemTimeModbusInputStruct : InputStruct
|
||||
{
|
||||
public SetSystemTimeModbusInputStruct(byte belongAddress, DateTime time)
|
||||
public SetSystemTimeModbusInputStruct(byte slaveAddress, DateTime time)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
FunctionCode = (byte)ModbusProtocalTimeFunctionCode.SetSystemTime;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = (byte) ModbusProtocalTimeFunctionCode.SetSystemTime;
|
||||
StartAddress = 30000;
|
||||
WriteCount = 5;
|
||||
WriteByteCount = 10;
|
||||
Year = (ushort)time.Year;
|
||||
Day = (byte)time.Day;
|
||||
Month = (byte)time.Month;
|
||||
Hour = (ushort)time.Hour;
|
||||
Second = (byte)time.Second;
|
||||
Minute = (byte)time.Minute;
|
||||
Millisecond = (ushort)time.Millisecond;
|
||||
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 SlaveAddress { get; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
public byte FunctionCode { get; }
|
||||
|
||||
public ushort StartAddress { get; private set; }
|
||||
public ushort StartAddress { get; }
|
||||
|
||||
public ushort WriteCount { get; private set; }
|
||||
public ushort WriteCount { get; }
|
||||
|
||||
public byte WriteByteCount { get; private set; }
|
||||
public byte WriteByteCount { get; }
|
||||
|
||||
public ushort Year { get; private set; }
|
||||
public ushort Year { get; }
|
||||
|
||||
public byte Day { get; private set; }
|
||||
public byte Day { get; }
|
||||
|
||||
public byte Month { get; private set; }
|
||||
public byte Month { get; }
|
||||
|
||||
public ushort Hour { get; private set; }
|
||||
public ushort Hour { get; }
|
||||
|
||||
public byte Second { get; private set; }
|
||||
public byte Second { get; }
|
||||
|
||||
public byte Minute { get; private set; }
|
||||
public byte Minute { get; }
|
||||
|
||||
public ushort Millisecond { get; private set; }
|
||||
public ushort Millisecond { get; }
|
||||
}
|
||||
|
||||
public class SetSystemTimeModbusOutputStruct : OutputStruct
|
||||
{
|
||||
public SetSystemTimeModbusOutputStruct(byte belongAddress, byte functionCode,
|
||||
public SetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
|
||||
ushort startAddress, ushort writeCount)
|
||||
{
|
||||
BelongAddress = belongAddress;
|
||||
SlaveAddress = slaveAddress;
|
||||
FunctionCode = functionCode;
|
||||
StartAddress = startAddress;
|
||||
WriteCount = writeCount;
|
||||
}
|
||||
|
||||
public byte BelongAddress { get; private set; }
|
||||
public byte SlaveAddress { get; private set; }
|
||||
|
||||
public byte FunctionCode { get; private set; }
|
||||
|
||||
@@ -346,14 +353,14 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写系统时间
|
||||
/// 写系统时间
|
||||
/// </summary>
|
||||
public class SetSystemTimeModbusProtocal : ProtocalUnit
|
||||
{
|
||||
public override byte[] Format(InputStruct message)
|
||||
{
|
||||
var r_message = (SetSystemTimeModbusInputStruct)message;
|
||||
return Format(r_message.BelongAddress, r_message.FunctionCode,
|
||||
var r_message = (SetSystemTimeModbusInputStruct) message;
|
||||
return Format(r_message.SlaveAddress, 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);
|
||||
@@ -361,22 +368,22 @@ namespace Modbus.Net.Modbus
|
||||
|
||||
public override OutputStruct Unformat(byte[] messageBytes, ref int flag)
|
||||
{
|
||||
byte belongAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
byte functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
ushort startAddress = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
ushort writeCount = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new SetSystemTimeModbusOutputStruct(belongAddress, functionCode, startAddress, writeCount);
|
||||
var slaveAddress = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var functionCode = BigEndianValueHelper.Instance.GetByte(messageBytes, ref flag);
|
||||
var startAddress = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
var writeCount = BigEndianValueHelper.Instance.GetUShort(messageBytes, ref flag);
|
||||
return new SetSystemTimeModbusOutputStruct(slaveAddress, functionCode, startAddress, writeCount);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Modbus协议错误表
|
||||
/// Modbus协议错误表
|
||||
/// </summary>
|
||||
public class ModbusProtocalErrorException : ProtocalErrorException
|
||||
{
|
||||
public int ErrorMessageNumber { get; private set; }
|
||||
private static readonly Dictionary<int, string> ProtocalErrorDictionary = new Dictionary<int, string>()
|
||||
private static readonly Dictionary<int, string> ProtocalErrorDictionary = new Dictionary<int, string>
|
||||
{
|
||||
{1, "ILLEGAL_FUNCTION"},
|
||||
{2, "ILLEGAL_DATA_ACCESS"},
|
||||
@@ -385,7 +392,7 @@ namespace Modbus.Net.Modbus
|
||||
{5, "ACKNOWLWDGE"},
|
||||
{6, "SLAVE_DEVICE_BUSY"},
|
||||
{500, "TCP_ILLEGAL_LENGTH"},
|
||||
{501, "RTU_ILLEGAL_CRC"},
|
||||
{501, "RTU_ILLEGAL_CRC"}
|
||||
};
|
||||
|
||||
public ModbusProtocalErrorException(int messageNumber)
|
||||
@@ -393,5 +400,7 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
ErrorMessageNumber = messageNumber;
|
||||
}
|
||||
|
||||
public int ErrorMessageNumber { get; private set; }
|
||||
}
|
||||
}
|
||||
@@ -6,16 +6,16 @@ using System.Text;
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Tcp协议字节伸缩
|
||||
/// Tcp协议字节伸缩
|
||||
/// </summary>
|
||||
public class ModbusTcpProtocalLinkerBytesExtend : ProtocalLinkerBytesExtend
|
||||
{
|
||||
public override byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
//Modbus/Tcp协议扩张,前面加6个字节,前面4个为0,后面两个为协议整体内容的长度
|
||||
byte[] newFormat = new byte[6 + content.Length];
|
||||
int tag = 0;
|
||||
ushort leng = (ushort)content.Length;
|
||||
var newFormat = new byte[6 + content.Length];
|
||||
var tag = 0;
|
||||
var leng = (ushort) content.Length;
|
||||
Array.Copy(BigEndianValueHelper.Instance.GetBytes(tag), 0, newFormat, 0, 4);
|
||||
Array.Copy(BigEndianValueHelper.Instance.GetBytes(leng), 0, newFormat, 4, 2);
|
||||
Array.Copy(content, 0, newFormat, 6, content.Length);
|
||||
@@ -25,7 +25,7 @@ namespace Modbus.Net.Modbus
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
//Modbus/Tcp协议收缩,抛弃前面6个字节的内容
|
||||
byte[] newContent = new byte[content.Length - 6];
|
||||
var newContent = new byte[content.Length - 6];
|
||||
Array.Copy(content, 6, newContent, 0, newContent.Length);
|
||||
return newContent;
|
||||
}
|
||||
@@ -35,9 +35,9 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
public override byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
byte[] crc = new byte[2];
|
||||
var crc = new byte[2];
|
||||
//Modbus/Rtu协议扩张,增加CRC校验
|
||||
byte[] newFormat = new byte[content.Length + 2];
|
||||
var newFormat = new byte[content.Length + 2];
|
||||
Crc16.GetInstance().GetCRC(content, ref crc);
|
||||
Array.Copy(content, 0, newFormat, 0, content.Length);
|
||||
Array.Copy(crc, 0, newFormat, newFormat.Length - 2, crc.Length);
|
||||
@@ -47,7 +47,7 @@ namespace Modbus.Net.Modbus
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
//Modbus/Rtu协议收缩,抛弃后面2个字节的内容
|
||||
byte[] newContent = new byte[content.Length - 2];
|
||||
var newContent = new byte[content.Length - 2];
|
||||
Array.Copy(content, 0, newContent, 0, newContent.Length);
|
||||
return newContent;
|
||||
}
|
||||
@@ -57,7 +57,7 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
public override byte[] BytesExtend(byte[] content)
|
||||
{
|
||||
List<byte> newContent = new List<byte>();
|
||||
var newContent = new List<byte>();
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(":"));
|
||||
foreach (var number in content)
|
||||
{
|
||||
@@ -71,17 +71,17 @@ namespace Modbus.Net.Modbus
|
||||
|
||||
public override byte[] BytesDecact(byte[] content)
|
||||
{
|
||||
List<byte> newContent = new List<byte>();
|
||||
string ans = Encoding.ASCII.GetString(content);
|
||||
var newContent = new List<byte>();
|
||||
var ans = Encoding.ASCII.GetString(content);
|
||||
var index = ans.IndexOf(Environment.NewLine);
|
||||
ans = ans.Substring(1, index - 1);
|
||||
for (int i = 0; i < ans.Length; i += 2)
|
||||
for (var i = 0; i < ans.Length; i += 2)
|
||||
{
|
||||
var number = byte.Parse(ans.Substring(i, 2), NumberStyles.HexNumber);
|
||||
newContent.Add(number);
|
||||
}
|
||||
newContent.RemoveAt(newContent.Count-1);
|
||||
newContent.RemoveAt(newContent.Count - 1);
|
||||
return newContent.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,19 @@
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Rtu协议
|
||||
/// Modbus/Rtu协议
|
||||
/// </summary>
|
||||
public class ModbusRtuProtocal : ModbusProtocal
|
||||
{
|
||||
public ModbusRtuProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.COM, belongAddress, masterAddress)
|
||||
public ModbusRtuProtocal(byte slaveAddress, byte masterAddress)
|
||||
: this(ConfigurationManager.COM, slaveAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusRtuProtocal(string com, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
public ModbusRtuProtocal(string com, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocalLinker = new ModbusRtuProtocalLinker(com);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
class ModbusRtuProtocalLinker : ComProtocalLinker
|
||||
public class ModbusRtuProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
public ModbusRtuProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
@@ -19,10 +23,5 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ModbusRtuProtocalLinker(string com) : base(com, 9600, Parity.None, StopBits.One, 8)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,22 @@
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Tcp协议
|
||||
/// Modbus/Tcp协议
|
||||
/// </summary>
|
||||
public class ModbusTcpProtocal : ModbusProtocal
|
||||
{
|
||||
public ModbusTcpProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.IP, belongAddress, masterAddress)
|
||||
public ModbusTcpProtocal(byte slaveAddress, byte masterAddress)
|
||||
: this(ConfigurationManager.IP, slaveAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusTcpProtocal(string ip, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
public ModbusTcpProtocal(string ip, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocalLinker = new ModbusTcpProtocalLinker(ip);
|
||||
}
|
||||
|
||||
public ModbusTcpProtocal(string ip, int port, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
public ModbusTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocalLinker = new ModbusTcpProtocalLinker(ip, port);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
{
|
||||
public class ModbusTcpProtocalLinker : TcpProtocalLinker
|
||||
{
|
||||
public ModbusTcpProtocalLinker(string ip) : base(ip, int.Parse(ConfigurationManager.ModbusPort))
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusTcpProtocalLinker(string ip, int port) : base(ip, port)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
@@ -17,15 +25,5 @@
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ModbusTcpProtocalLinker(string ip) : base(ip, int.Parse(ConfigurationManager.ModbusPort))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ModbusTcpProtocalLinker(string ip, int port) : base(ip, port)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,48 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus连接类型
|
||||
/// Modbus连接类型
|
||||
/// </summary>
|
||||
public enum ModbusType
|
||||
{
|
||||
/// <summary>
|
||||
/// Rtu连接
|
||||
/// Rtu连接
|
||||
/// </summary>
|
||||
Rtu = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Tcp连接
|
||||
/// Tcp连接
|
||||
/// </summary>
|
||||
Tcp = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Ascii连接
|
||||
/// Ascii连接
|
||||
/// </summary>
|
||||
Ascii = 2,
|
||||
Ascii = 2
|
||||
}
|
||||
|
||||
public class ModbusUtility : BaseUtility
|
||||
{
|
||||
private ModbusType _modbusType;
|
||||
|
||||
public ModbusUtility(int connectionType, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ConnectionString = null;
|
||||
ModbusType = (ModbusType) connectionType;
|
||||
AddressTranslator = new AddressTranslatorModbus();
|
||||
}
|
||||
|
||||
public ModbusUtility(ModbusType connectionType, string connectionString, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
ModbusType = connectionType;
|
||||
AddressTranslator = new AddressTranslatorModbus();
|
||||
}
|
||||
|
||||
public override bool GetLittleEndian => Wrapper[typeof (ReadDataModbusProtocal)].IsLittleEndian;
|
||||
public override bool SetLittleEndian => Wrapper[typeof (WriteDataModbusProtocal)].IsLittleEndian;
|
||||
|
||||
@@ -47,22 +64,18 @@ namespace Modbus.Net.Modbus
|
||||
var connectionStringSplit = ConnectionString.Split(':');
|
||||
try
|
||||
{
|
||||
return connectionStringSplit.Length < 2 ? (int?)null : int.Parse(connectionStringSplit[1]);
|
||||
return connectionStringSplit.Length < 2 ? (int?) null : int.Parse(connectionStringSplit[1]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public ModbusType ModbusType
|
||||
{
|
||||
get
|
||||
{
|
||||
return _modbusType;
|
||||
}
|
||||
get { return _modbusType; }
|
||||
set
|
||||
{
|
||||
_modbusType = value;
|
||||
@@ -70,37 +83,32 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
case ModbusType.Rtu:
|
||||
{
|
||||
Wrapper = ConnectionString == null ? new ModbusRtuProtocal(BelongAddress, MasterAddress) : new ModbusRtuProtocal(ConnectionString, BelongAddress, MasterAddress);
|
||||
Wrapper = ConnectionString == null
|
||||
? new ModbusRtuProtocal(SlaveAddress, MasterAddress)
|
||||
: new ModbusRtuProtocal(ConnectionString, SlaveAddress, MasterAddress);
|
||||
break;
|
||||
}
|
||||
case ModbusType.Tcp:
|
||||
{
|
||||
Wrapper = ConnectionString == null ? new ModbusTcpProtocal(BelongAddress, MasterAddress) : (ConnectionStringPort == null ? new ModbusTcpProtocal(ConnectionString, BelongAddress, MasterAddress) : new ModbusTcpProtocal(ConnectionStringIp,ConnectionStringPort.Value, BelongAddress, MasterAddress));
|
||||
Wrapper = ConnectionString == null
|
||||
? new ModbusTcpProtocal(SlaveAddress, MasterAddress)
|
||||
: (ConnectionStringPort == null
|
||||
? new ModbusTcpProtocal(ConnectionString, SlaveAddress, MasterAddress)
|
||||
: new ModbusTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
||||
MasterAddress));
|
||||
break;
|
||||
}
|
||||
case ModbusType.Ascii:
|
||||
{
|
||||
Wrapper = ConnectionString == null ? new ModbusAsciiProtocal(BelongAddress, MasterAddress) : new ModbusAsciiProtocal(ConnectionString, BelongAddress, MasterAddress);
|
||||
Wrapper = ConnectionString == null
|
||||
? new ModbusAsciiProtocal(SlaveAddress, MasterAddress)
|
||||
: new ModbusAsciiProtocal(ConnectionString, SlaveAddress, MasterAddress);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ModbusUtility(int connectionType, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
{
|
||||
ConnectionString = null;
|
||||
ModbusType = (ModbusType)connectionType;
|
||||
AddressTranslator = new AddressTranslatorModbus();
|
||||
}
|
||||
|
||||
public ModbusUtility(ModbusType connectionType, string connectionString, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
ModbusType = connectionType;
|
||||
AddressTranslator = new AddressTranslatorModbus();
|
||||
}
|
||||
|
||||
public override void SetConnectionType(int connectionType)
|
||||
{
|
||||
ModbusType = (ModbusType) connectionType;
|
||||
@@ -110,12 +118,12 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new ReadDataModbusInputStruct(BelongAddress, startAddress,
|
||||
(ushort)getByteCount, AddressTranslator);
|
||||
var inputStruct = new ReadDataModbusInputStruct(SlaveAddress, startAddress,
|
||||
(ushort) getByteCount, AddressTranslator);
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof (ReadDataModbusProtocal)], inputStruct) as
|
||||
ReadDataModbusOutputStruct;
|
||||
return outputStruct?.DataValue;
|
||||
return outputStruct?.DataValue;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -127,7 +135,7 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new WriteDataModbusInputStruct(BelongAddress, startAddress, setContents,
|
||||
var inputStruct = new WriteDataModbusInputStruct(SlaveAddress, startAddress, setContents,
|
||||
AddressTranslator);
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof (WriteDataModbusProtocal)], inputStruct) as
|
||||
@@ -145,7 +153,7 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new GetSystemTimeModbusInputStruct(BelongAddress);
|
||||
var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress);
|
||||
var outputStruct =
|
||||
Wrapper.SendReceive(Wrapper[typeof(GetSystemTimeModbusProtocal)], inputStruct) as
|
||||
GetSystemTimeModbusOutputStruct;
|
||||
@@ -161,7 +169,7 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new SetSystemTimeModbusInputStruct(BelongAddress, setTime);
|
||||
var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime);
|
||||
var outputStruct =
|
||||
Wrapper.SendReceive(Wrapper[typeof(SetSystemTimeModbusProtocal)], inputStruct) as
|
||||
SetSystemTimeModbusOutputStruct;
|
||||
@@ -174,4 +182,4 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user