This commit is contained in:
parallelbgls
2017-12-29 13:26:03 +08:00
parent 5b193409b1
commit 9740f2a03d
4 changed files with 24 additions and 9 deletions

View File

@@ -18,7 +18,6 @@ namespace Modbus.Net.Siemens
public SiemensPpiProtocol(byte slaveAddress, byte masterAddress) public SiemensPpiProtocol(byte slaveAddress, byte masterAddress)
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress) : this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
{ {
ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress);
} }
/// <summary> /// <summary>
@@ -31,6 +30,7 @@ namespace Modbus.Net.Siemens
: base(slaveAddress, masterAddress) : base(slaveAddress, masterAddress)
{ {
_com = com; _com = com;
ProtocolLinker = new SiemensPpiProtocolLinker(_com, SlaveAddress);
} }
/// <summary> /// <summary>
@@ -79,10 +79,14 @@ namespace Modbus.Net.Siemens
(await (await (await (await
ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocol)], ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocol)],
inputStruct)). inputStruct)).
SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer => SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer =>
answer != null
new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress) ? new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress)
)).Unwrap<ComConfirmMessageSiemensOutputStruct>(); : null)).Unwrap<ComConfirmMessageSiemensOutputStruct>();
if (outputStruct == null && ProtocolLinker.IsConnected)
{
ProtocolLinker.Disconnect();
}
return outputStruct != null; return outputStruct != null;
} }
} }

View File

@@ -18,7 +18,7 @@ namespace Modbus.Net.Siemens
public SiemensPpiProtocolLinker(string com, int slaveAddress) public SiemensPpiProtocolLinker(string com, int slaveAddress)
: base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress) : base(com, 9600, Parity.Even, StopBits.One, 8, slaveAddress)
{ {
((BaseConnector)BaseConnector).AddController(new MatchController(new ICollection<(int,int)>[] { new List<(int,int)> { (4,5) }, new List<(int,int)> {(5,4) }, new List<(int,int)> { (11 ,11), (12,12) } }, 0)); ((BaseConnector)BaseConnector).AddController(new FifoController(0));
} }
/// <summary> /// <summary>
@@ -37,6 +37,7 @@ namespace Modbus.Net.Siemens
new ComConfirmMessageSiemensProtocol().Format(inputStruct2)); new ComConfirmMessageSiemensProtocol().Format(inputStruct2));
} }
var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes); var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes);
if (receiveBytes == null) return null;
if (content.Length > 6 && receiveBytes.Length == 1 && receiveBytes[0] == 0xe5) if (content.Length > 6 && receiveBytes.Length == 1 && receiveBytes[0] == 0xe5)
{ {
var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]); var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]);
@@ -56,7 +57,7 @@ namespace Modbus.Net.Siemens
public override async Task<byte[]> SendReceiveWithoutExtAndDecAsync(byte[] content) public override async Task<byte[]> SendReceiveWithoutExtAndDecAsync(byte[] content)
{ {
var ans = await base.SendReceiveWithoutExtAndDecAsync(content); var ans = await base.SendReceiveWithoutExtAndDecAsync(content);
while (ans.Length == 1 && ans[0] == 0xf9) while (ans?.Length == 1 && ans[0] == 0xf9)
{ {
Thread.Sleep(500); Thread.Sleep(500);
if (content.Length <= 6) if (content.Length <= 6)

View File

@@ -152,10 +152,15 @@ namespace Modbus.Net.Siemens
//先建立连接,然后建立设备的引用 //先建立连接,然后建立设备的引用
(await (await (await (await
ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocol)], inputStruct)).SendReceiveAsync( ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocol)], inputStruct)).SendReceiveAsync(
this[typeof(EstablishAssociationSiemensProtocol)], answer => this[typeof(EstablishAssociationSiemensProtocol)], answer =>
answer != null ?
new EstablishAssociationSiemensInputStruct(0x0101, _maxCalling, new EstablishAssociationSiemensInputStruct(0x0101, _maxCalling,
_maxCalled, _maxCalled,
_maxPdu))).Unwrap<EstablishAssociationSiemensOutputStruct>(); _maxPdu) : null)).Unwrap<EstablishAssociationSiemensOutputStruct>();
if (outputStruct == null && ProtocolLinker.IsConnected)
{
ProtocolLinker.Disconnect();
}
return outputStruct != null; return outputStruct != null;
} }
} }

View File

@@ -66,6 +66,11 @@ namespace Modbus.Net
return bytesExtend?.BytesDecact(content); return bytesExtend?.BytesDecact(content);
} }
/// <summary>
/// 检查接收的数据是否正确
/// </summary>
/// <param name="content">接收协议的内容</param>
/// <returns>协议是否是正确的</returns>
public override bool? CheckRight(byte[] content) public override bool? CheckRight(byte[] content)
{ {
if (content == null) if (content == null)