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

View File

@@ -18,7 +18,7 @@ namespace Modbus.Net.Siemens
public SiemensPpiProtocolLinker(string com, int 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>
@@ -37,6 +37,7 @@ namespace Modbus.Net.Siemens
new ComConfirmMessageSiemensProtocol().Format(inputStruct2));
}
var receiveBytes = await SendReceiveWithoutExtAndDecAsync(extBytes);
if (receiveBytes == null) return null;
if (content.Length > 6 && receiveBytes.Length == 1 && receiveBytes[0] == 0xe5)
{
var inputStruct2 = new ComConfirmMessageSiemensInputStruct(content[4], content[5]);
@@ -56,7 +57,7 @@ namespace Modbus.Net.Siemens
public override async Task<byte[]> SendReceiveWithoutExtAndDecAsync(byte[] content)
{
var ans = await base.SendReceiveWithoutExtAndDecAsync(content);
while (ans.Length == 1 && ans[0] == 0xf9)
while (ans?.Length == 1 && ans[0] == 0xf9)
{
Thread.Sleep(500);
if (content.Length <= 6)

View File

@@ -153,9 +153,14 @@ namespace Modbus.Net.Siemens
(await (await
ForceSendReceiveAsync(this[typeof(CreateReferenceSiemensProtocol)], inputStruct)).SendReceiveAsync(
this[typeof(EstablishAssociationSiemensProtocol)], answer =>
answer != null ?
new EstablishAssociationSiemensInputStruct(0x0101, _maxCalling,
_maxCalled,
_maxPdu))).Unwrap<EstablishAssociationSiemensOutputStruct>();
_maxPdu) : null)).Unwrap<EstablishAssociationSiemensOutputStruct>();
if (outputStruct == null && ProtocolLinker.IsConnected)
{
ProtocolLinker.Disconnect();
}
return outputStruct != null;
}
}

View File

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