1
0
mirror of https://github.com/zhaopeiym/IoTClient synced 2025-10-19 22:14:31 +08:00

连接超时设置

This commit is contained in:
BennyZhao 2022-06-29 19:47:00 +08:00
parent cd13a28c56
commit fc1354adc5
9 changed files with 56 additions and 12 deletions

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace IoTClient.Clients.Modbus
{
@ -56,6 +57,8 @@ namespace IoTClient.Clients.Modbus
this.plcAddresses = plcAddresses;
}
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
protected override Result Connect()
{
var result = new Result();
@ -67,7 +70,12 @@ namespace IoTClient.Clients.Modbus
socket.SendTimeout = timeout;
//连接
socket.Connect(ipEndPoint);
//socket.Connect(ipEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(ipEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
}
catch (Exception ex)
{

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace IoTClient.Clients.Modbus
{
@ -59,6 +60,8 @@ namespace IoTClient.Clients.Modbus
this.plcAddresses = plcAddresses;
}
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
/// <summary>
/// 连接
/// </summary>
@ -75,7 +78,12 @@ namespace IoTClient.Clients.Modbus
socket.SendTimeout = timeout;
//连接
socket.Connect(ipEndPoint);
//socket.Connect(ipEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(ipEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
}
catch (Exception ex)
{
@ -1165,7 +1173,7 @@ namespace IoTClient.Clients.Modbus
/// 写入
/// </summary>
/// <param name="address">写入地址</param>
/// <param name="values">批量读取的值</param>
/// <param name="values">写入字节数组</param>
/// <param name="stationNumber">站号</param>
/// <param name="functionCode">功能码</param>
/// <param name="byteFormatting">大小端设置</param>

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace IoTClient.Clients.PLC
{
@ -66,6 +67,7 @@ namespace IoTClient.Clients.PLC
0x00,0x00 //选项标记0x0000
};
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
/// <summary>
/// 打开连接(如果已经是连接状态会先关闭再打开)
/// </summary>
@ -83,7 +85,12 @@ namespace IoTClient.Clients.PLC
socket.SendTimeout = timeout;
//连接
socket.Connect(IpEndPoint);
//socket.Connect(IpEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(IpEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
result.Requst = string.Join(" ", RegisteredCommand.Select(t => t.ToString("X2")));
socket.Send(RegisteredCommand);

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace IoTClient.Clients.PLC
{
@ -48,6 +49,7 @@ namespace IoTClient.Clients.PLC
this.timeout = timeout;
}
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
/// <summary>
/// 打开连接(如果已经是连接状态会先关闭再打开)
/// </summary>
@ -63,7 +65,12 @@ namespace IoTClient.Clients.PLC
socket.ReceiveTimeout = timeout;
socket.SendTimeout = timeout;
socket.Connect(IpEndPoint);
//socket.Connect(IpEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(IpEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
}
catch (Exception ex)
{

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace IoTClient.Clients.PLC
{
@ -80,6 +81,7 @@ namespace IoTClient.Clients.PLC
this.endianFormat = endianFormat;
}
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
/// <summary>
/// 打开连接(如果已经是连接状态会先关闭再打开)
/// </summary>
@ -95,7 +97,12 @@ namespace IoTClient.Clients.PLC
socket.ReceiveTimeout = timeout;
socket.SendTimeout = timeout;
socket.Connect(IpEndPoint);
//socket.Connect(IpEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(IpEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
BasicCommand[19] = SA1;
result.Requst = string.Join(" ", BasicCommand.Select(t => t.ToString("X2")));

View File

@ -90,6 +90,8 @@ namespace IoTClient.Clients.PLC
this.timeout = timeout;
}
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
/// <summary>
/// 打开连接(如果已经是连接状态会先关闭再打开)
/// </summary>
@ -106,7 +108,12 @@ namespace IoTClient.Clients.PLC
socket.SendTimeout = timeout;
//连接
socket.Connect(IpEndPoint);
//socket.Connect(IpEndPoint);
TimeoutObject.Reset();
socket.BeginConnect(IpEndPoint, (asyncresult) => { TimeoutObject.Set(); }, socket);
//阻塞当前线程
if (!TimeoutObject.WaitOne(timeout, false))
throw new Exception("连接超时");
var Command1 = SiemensConstant.Command1;
var Command2 = SiemensConstant.Command2;

View File

@ -7,9 +7,9 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
<Authors>农码一生 - benny</Authors>
<Company>擎呐科技</Company>
<Company>农码一生</Company>
<Product>物联网设备通讯协议实现</Product>
<Copyright>Copyright (c) 2019 擎呐科技. All rights reserved.</Copyright>
<Copyright>Copyright (c) 2022 农码一生. All rights reserved.</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>

View File

@ -1589,7 +1589,7 @@
写入
</summary>
<param name="address">写入地址</param>
<param name="values">批量读取的值</param>
<param name="values">写入字节数组</param>
<param name="stationNumber">站号</param>
<param name="functionCode">功能码</param>
<param name="byteFormatting">大小端设置</param>

View File

@ -6,9 +6,9 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
<Authors>农码一生 - benny</Authors>
<Company>擎呐科技</Company>
<Company>农码一生</Company>
<Product>物联网设备通讯协议实现</Product>
<Copyright>Copyright (c) 2019 擎呐科技. All rights reserved.</Copyright>
<Copyright>Copyright (c) 2022 农码一生. All rights reserved.</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>