DeviceId Change Support

This commit is contained in:
罗圣
2016-09-01 15:16:31 +08:00
parent a465d5b6ea
commit 0f0da2d585
22 changed files with 222 additions and 146 deletions

View File

@@ -11,11 +11,11 @@ namespace Modbus.Net.Modbus
/// </summary>
public class ModbusAsciiProtocal : ModbusProtocal
{
public ModbusAsciiProtocal() : this(ConfigurationManager.COM)
public ModbusAsciiProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.COM, belongAddress, masterAddress)
{
}
public ModbusAsciiProtocal(string com)
public ModbusAsciiProtocal(string com, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
ProtocalLinker = new ModbusAsciiProtocalLinker(com);
}

View File

@@ -5,17 +5,20 @@ namespace Modbus.Net.Modbus
public class ModbusMachine : BaseMachine
{
public ModbusMachine(ModbusType connectionType, string connectionString,
IEnumerable<AddressUnit> getAddresses, bool keepConnect) : base(getAddresses, keepConnect)
IEnumerable<AddressUnit> getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress)
: base(getAddresses, keepConnect)
{
BaseUtility = new ModbusUtility(connectionType, connectionString);
BaseUtility = new ModbusUtility(connectionType, connectionString, slaveAddress, masterAddress);
SlaveAddress = slaveAddress;
MasterAddress = masterAddress;
AddressFormater = new AddressFormaterModbus();
AddressCombiner = new AddressCombinerContinus(AddressTranslator);
AddressCombinerSet = new AddressCombinerContinus(AddressTranslator);
}
public ModbusMachine(ModbusType connectionType, string connectionString,
IEnumerable<AddressUnit> getAddresses)
: this(connectionType, connectionString, getAddresses, false)
IEnumerable<AddressUnit> getAddresses, byte slaveAddress, byte masterAddress)
: this(connectionType, connectionString, getAddresses, false, slaveAddress, masterAddress)
{
}
}

View File

@@ -50,6 +50,10 @@ namespace Modbus.Net.Modbus
{
return await ProtocalLinker.ConnectAsync();
}
protected ModbusProtocal(byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
}
}
#region PLC数据

View File

@@ -5,11 +5,11 @@
/// </summary>
public class ModbusRtuProtocal : ModbusProtocal
{
public ModbusRtuProtocal() : this(ConfigurationManager.COM)
public ModbusRtuProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.COM, belongAddress, masterAddress)
{
}
public ModbusRtuProtocal(string com)
public ModbusRtuProtocal(string com, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
ProtocalLinker = new ModbusRtuProtocalLinker(com);
}

View File

@@ -5,16 +5,16 @@
/// </summary>
public class ModbusTcpProtocal : ModbusProtocal
{
public ModbusTcpProtocal() : this(ConfigurationManager.IP)
public ModbusTcpProtocal(byte belongAddress, byte masterAddress) : this(ConfigurationManager.IP, belongAddress, masterAddress)
{
}
public ModbusTcpProtocal(string ip)
public ModbusTcpProtocal(string ip, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
ProtocalLinker = new ModbusTcpProtocalLinker(ip);
}
public ModbusTcpProtocal(string ip, int port)
public ModbusTcpProtocal(string ip, int port, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
ProtocalLinker = new ModbusTcpProtocalLinker(ip, port);
}

View File

@@ -70,31 +70,31 @@ namespace Modbus.Net.Modbus
{
case ModbusType.Rtu:
{
Wrapper = ConnectionString == null ? new ModbusRtuProtocal() : new ModbusRtuProtocal(ConnectionString);
Wrapper = ConnectionString == null ? new ModbusRtuProtocal(BelongAddress, MasterAddress) : new ModbusRtuProtocal(ConnectionString, BelongAddress, MasterAddress);
break;
}
case ModbusType.Tcp:
{
Wrapper = ConnectionString == null ? new ModbusTcpProtocal() : (ConnectionStringPort == null ? new ModbusTcpProtocal(ConnectionString) : new ModbusTcpProtocal(ConnectionStringIp,ConnectionStringPort.Value));
Wrapper = ConnectionString == null ? new ModbusTcpProtocal(BelongAddress, MasterAddress) : (ConnectionStringPort == null ? new ModbusTcpProtocal(ConnectionString, BelongAddress, MasterAddress) : new ModbusTcpProtocal(ConnectionStringIp,ConnectionStringPort.Value, BelongAddress, MasterAddress));
break;
}
case ModbusType.Ascii:
{
Wrapper = ConnectionString == null ? new ModbusAsciiProtocal() : new ModbusAsciiProtocal(ConnectionString);
Wrapper = ConnectionString == null ? new ModbusAsciiProtocal(BelongAddress, MasterAddress) : new ModbusAsciiProtocal(ConnectionString, BelongAddress, MasterAddress);
break;
}
}
}
}
public ModbusUtility(int connectionType)
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)
public ModbusUtility(ModbusType connectionType, string connectionString, byte belongAddress, byte masterAddress) : base(belongAddress, masterAddress)
{
ConnectionString = connectionString;
ModbusType = connectionType;
@@ -106,11 +106,11 @@ namespace Modbus.Net.Modbus
ModbusType = (ModbusType) connectionType;
}
public override async Task<byte[]> GetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, int getByteCount)
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
{
try
{
var inputStruct = new ReadDataModbusInputStruct(belongAddress, startAddress,
var inputStruct = new ReadDataModbusInputStruct(BelongAddress, startAddress,
getByteCount%2 == 0 ? (ushort) (getByteCount/2) : (ushort) (getByteCount/2 + 1), AddressTranslator);
var outputStruct = await
Wrapper.SendReceiveAsync(Wrapper[typeof (ReadDataModbusProtocal)], inputStruct) as
@@ -123,11 +123,11 @@ namespace Modbus.Net.Modbus
}
}
public override async Task<bool> SetDatasAsync(byte belongAddress, byte masterAddress, string startAddress, object[] setContents)
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
{
try
{
var inputStruct = new WriteDataModbusInputStruct(belongAddress, startAddress, setContents,
var inputStruct = new WriteDataModbusInputStruct(BelongAddress, startAddress, setContents,
AddressTranslator);
var outputStruct = await
Wrapper.SendReceiveAsync(Wrapper[typeof (WriteDataModbusProtocal)], inputStruct) as
@@ -141,11 +141,11 @@ namespace Modbus.Net.Modbus
}
/*
public override DateTime GetTime(byte belongAddress)
public override DateTime GetTime()
{
try
{
var inputStruct = new GetSystemTimeModbusInputStruct(belongAddress);
var inputStruct = new GetSystemTimeModbusInputStruct(BelongAddress);
var outputStruct =
Wrapper.SendReceive(Wrapper[typeof(GetSystemTimeModbusProtocal)], inputStruct) as
GetSystemTimeModbusOutputStruct;
@@ -157,11 +157,11 @@ namespace Modbus.Net.Modbus
}
}
public override bool SetTime(byte belongAddress, DateTime setTime)
public override bool SetTime(DateTime setTime)
{
try
{
var inputStruct = new SetSystemTimeModbusInputStruct(belongAddress, setTime);
var inputStruct = new SetSystemTimeModbusInputStruct(BelongAddress, setTime);
var outputStruct =
Wrapper.SendReceive(Wrapper[typeof(SetSystemTimeModbusProtocal)], inputStruct) as
SetSystemTimeModbusOutputStruct;