mirror of
https://github.com/zhaopeiym/IoTClient
synced 2025-10-26 22:15:44 +08:00
连接超时设置
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace IoTClient.Clients.Modbus
|
namespace IoTClient.Clients.Modbus
|
||||||
{
|
{
|
||||||
@@ -56,6 +57,8 @@ namespace IoTClient.Clients.Modbus
|
|||||||
this.plcAddresses = plcAddresses;
|
this.plcAddresses = plcAddresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
|
|
||||||
protected override Result Connect()
|
protected override Result Connect()
|
||||||
{
|
{
|
||||||
var result = new Result();
|
var result = new Result();
|
||||||
@@ -67,7 +70,12 @@ namespace IoTClient.Clients.Modbus
|
|||||||
socket.SendTimeout = 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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace IoTClient.Clients.Modbus
|
namespace IoTClient.Clients.Modbus
|
||||||
{
|
{
|
||||||
@@ -59,6 +60,8 @@ namespace IoTClient.Clients.Modbus
|
|||||||
this.plcAddresses = plcAddresses;
|
this.plcAddresses = plcAddresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 连接
|
/// 连接
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -75,7 +78,12 @@ namespace IoTClient.Clients.Modbus
|
|||||||
socket.SendTimeout = 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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -1165,7 +1173,7 @@ namespace IoTClient.Clients.Modbus
|
|||||||
/// 写入
|
/// 写入
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="address">写入地址</param>
|
/// <param name="address">写入地址</param>
|
||||||
/// <param name="values">批量读取的值</param>
|
/// <param name="values">写入字节数组</param>
|
||||||
/// <param name="stationNumber">站号</param>
|
/// <param name="stationNumber">站号</param>
|
||||||
/// <param name="functionCode">功能码</param>
|
/// <param name="functionCode">功能码</param>
|
||||||
/// <param name="byteFormatting">大小端设置</param>
|
/// <param name="byteFormatting">大小端设置</param>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace IoTClient.Clients.PLC
|
namespace IoTClient.Clients.PLC
|
||||||
{
|
{
|
||||||
@@ -66,6 +67,7 @@ namespace IoTClient.Clients.PLC
|
|||||||
0x00,0x00 //选项标记(0x0000
|
0x00,0x00 //选项标记(0x0000
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -83,7 +85,12 @@ namespace IoTClient.Clients.PLC
|
|||||||
socket.SendTimeout = 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("连接超时");
|
||||||
|
|
||||||
result.Requst = string.Join(" ", RegisteredCommand.Select(t => t.ToString("X2")));
|
result.Requst = string.Join(" ", RegisteredCommand.Select(t => t.ToString("X2")));
|
||||||
socket.Send(RegisteredCommand);
|
socket.Send(RegisteredCommand);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace IoTClient.Clients.PLC
|
namespace IoTClient.Clients.PLC
|
||||||
{
|
{
|
||||||
@@ -48,6 +49,7 @@ namespace IoTClient.Clients.PLC
|
|||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -63,7 +65,12 @@ namespace IoTClient.Clients.PLC
|
|||||||
socket.ReceiveTimeout = timeout;
|
socket.ReceiveTimeout = timeout;
|
||||||
socket.SendTimeout = 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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace IoTClient.Clients.PLC
|
namespace IoTClient.Clients.PLC
|
||||||
{
|
{
|
||||||
@@ -80,6 +81,7 @@ namespace IoTClient.Clients.PLC
|
|||||||
this.endianFormat = endianFormat;
|
this.endianFormat = endianFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -95,7 +97,12 @@ namespace IoTClient.Clients.PLC
|
|||||||
socket.ReceiveTimeout = timeout;
|
socket.ReceiveTimeout = timeout;
|
||||||
socket.SendTimeout = 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;
|
BasicCommand[19] = SA1;
|
||||||
result.Requst = string.Join(" ", BasicCommand.Select(t => t.ToString("X2")));
|
result.Requst = string.Join(" ", BasicCommand.Select(t => t.ToString("X2")));
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ namespace IoTClient.Clients.PLC
|
|||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private readonly ManualResetEvent TimeoutObject = new ManualResetEvent(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
/// 打开连接(如果已经是连接状态会先关闭再打开)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -106,7 +108,12 @@ namespace IoTClient.Clients.PLC
|
|||||||
socket.SendTimeout = 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("连接超时");
|
||||||
|
|
||||||
var Command1 = SiemensConstant.Command1;
|
var Command1 = SiemensConstant.Command1;
|
||||||
var Command2 = SiemensConstant.Command2;
|
var Command2 = SiemensConstant.Command2;
|
||||||
|
|||||||
@@ -7,9 +7,9 @@
|
|||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
|
||||||
<Authors>农码一生 - benny</Authors>
|
<Authors>农码一生 - benny</Authors>
|
||||||
<Company>擎呐科技</Company>
|
<Company>农码一生</Company>
|
||||||
<Product>物联网设备通讯协议实现</Product>
|
<Product>物联网设备通讯协议实现</Product>
|
||||||
<Copyright>Copyright (c) 2019 擎呐科技. All rights reserved.</Copyright>
|
<Copyright>Copyright (c) 2022 农码一生. All rights reserved.</Copyright>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
|
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
|
||||||
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>
|
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>
|
||||||
|
|||||||
@@ -1589,7 +1589,7 @@
|
|||||||
写入
|
写入
|
||||||
</summary>
|
</summary>
|
||||||
<param name="address">写入地址</param>
|
<param name="address">写入地址</param>
|
||||||
<param name="values">批量读取的值</param>
|
<param name="values">写入字节数组</param>
|
||||||
<param name="stationNumber">站号</param>
|
<param name="stationNumber">站号</param>
|
||||||
<param name="functionCode">功能码</param>
|
<param name="functionCode">功能码</param>
|
||||||
<param name="byteFormatting">大小端设置</param>
|
<param name="byteFormatting">大小端设置</param>
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/zhaopeiym/IoTClient</PackageProjectUrl>
|
||||||
<Authors>农码一生 - benny</Authors>
|
<Authors>农码一生 - benny</Authors>
|
||||||
<Company>擎呐科技</Company>
|
<Company>农码一生</Company>
|
||||||
<Product>物联网设备通讯协议实现</Product>
|
<Product>物联网设备通讯协议实现</Product>
|
||||||
<Copyright>Copyright (c) 2019 擎呐科技. All rights reserved.</Copyright>
|
<Copyright>Copyright (c) 2022 农码一生. All rights reserved.</Copyright>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
|
<RepositoryUrl>https://github.com/zhaopeiym/IoTClient</RepositoryUrl>
|
||||||
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>
|
<PackageTags>IoT,物联网,PLC,Modbus,Bacnet</PackageTags>
|
||||||
|
|||||||
Reference in New Issue
Block a user