This commit is contained in:
parallelbgls
2023-04-23 16:39:38 +08:00
parent 192c0eb9b2
commit 088016f8aa
6 changed files with 103 additions and 33 deletions

View File

@@ -73,25 +73,12 @@ namespace Modbus.Net.Siemens
/// <returns>是否连接成功</returns>
public override async Task<bool> ConnectAsync()
{
IOutputStruct outputStruct;
using (await _lock.LockAsync())
{
if (ProtocolLinker.IsConnected) return true;
if (!await ProtocolLinker.ConnectAsync()) return false;
var inputStruct = new ComCreateReferenceSiemensInputStruct(SlaveAddress, MasterAddress);
outputStruct =
(await (await
ForceSendReceiveAsync(this[typeof(ComCreateReferenceSiemensProtocol)],
inputStruct)).SendReceiveAsync(this[typeof(ComConfirmMessageSiemensProtocol)], answer =>
answer != null
? new ComConfirmMessageSiemensInputStruct(SlaveAddress, MasterAddress)
: null)).Unwrap<ComConfirmMessageSiemensOutputStruct>();
if (outputStruct == null && ProtocolLinker.IsConnected)
{
ProtocolLinker.Disconnect();
}
}
return outputStruct != null;
return true;
}
}
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;
namespace Modbus.Net.Siemens
{
@@ -16,11 +16,19 @@ namespace Modbus.Net.Siemens
/// <param name="com">串口地址</param>
/// <param name="slaveAddress">从站号</param>
public SiemensPpiProtocolLinker(string com, int slaveAddress)
: base(com, slaveAddress)
: base(com, slaveAddress, parity:Parity.Even)
{
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")),
lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 1 }, 6),
lengthCalc: content =>
{
if (content[0] == 0x10)
return 6;
else if (content[0] == 0xE5)
return 1;
else
return DuplicateWithCount.GetDuplcateFunc(new List<int> { 1 }, 6)(content);
},
checkRightFunc: ContentCheck.FcsCheckRight,
waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ?
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) :

View File

@@ -433,24 +433,36 @@ namespace Modbus.Net
/// </summary>
protected void CheckOpen()
{
if (!SerialPort.IsOpen)
if (SerialPort == null)
{
try
{
SerialPort.Open();
Connect();
}
catch (Exception err)
catch (Exception err1)
{
logger.LogError(err, "Com client {ConnectionToken} open error", _com);
logger.LogError(err1, "Com client {ConnectionToken} open error", _com);
Dispose();
}
if (!SerialPort.IsOpen)
{
try
{
SerialPort.Open();
Connect();
}
catch (Exception err2)
{
logger.LogError(err2, "Com client {ConnectionToken} open error", _com);
Dispose();
try
{
Connect();
}
catch (Exception err3)
{
logger.LogError(err3, "Com client {ConnectionToken} open error", _com);
Dispose();
}
}
}
}

View File

@@ -58,7 +58,9 @@ namespace Modbus.Net
public static bool? FcsCheckRight(byte[] content)
{
var fcsCheck = 0;
for (var i = 4; i < content.Length - 2; i++)
var start = content[0] == 0x10 ? 1 : 4;
if (content[0] == 0xE5) return true;
for (var i = start; i < content.Length - 2; i++)
fcsCheck += content[i];
fcsCheck = fcsCheck % 256;
if (fcsCheck != content[content.Length - 2]) return false;