Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
Sign in
Sign up
Fetch the repository succeeded.
Open Source
>
Development Lib
>
Network Development Package
&&
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
344
Star
2.8K
Fork
826
GVP
若汝棋茗
/
TouchSocket
Code
Issues
27
Insights
Pipelines
Service
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
[SerialPortClient]串口组件数据接收回调事件异常"The port is closed"。
Done
#ICB8IN
Admin
Opened this issue
2025-05-28 18:26
### 组件 其他 ### 版本号 3.1.5 ### .NET SDK .NET6 ### 项目类型 WPF ### 操作系统 Windows 11 ### 运行环境 开发环境 (Development) ### 这个问题是否已经存在? - [x] 我已经搜索过现有的问题 (https://gitee.com/RRQM_Home/TouchSocket/issues) ### 如何复现 1. 创建串口连接并打开; 2. 以 SerialPortClientBase.LastReceivedTime 为基准对连接进行保活,超时5秒连接未活跃就发送心跳指令给下位机; 3. 如果下位机超时未响应,则调用 SerialPortClient 的Disconnect和Dispose,然后重新创建SerialPortClient实例并Connect; 4. 反复插拔串口线触发超时重连。 5. 插上串口线不再动作,且心跳正常工作,下位机正常响应。 6. 等待心跳工作10分钟左右,程序崩溃(非稳定复现),报错堆栈信息见“实际结果”。 ``` // See https://aka.ms/new-console-template for more information using TouchSocket.Core; using TouchSocket.SerialPorts; using TouchSocket.Sockets; bool isConnected = false; SerialPortClient? client = null; Action? Guardian = null; var Disconnect = async () => { if (client?.MainSerialPort?.IsOpen == true) { await client.CloseAsync("关闭旧连接"); client.SafeDispose(); } isConnected = false; }; var initConnect = async () => { try { await Disconnect(); client = new(); await client.SetupAsync(new TouchSocketConfig() .SetSerialPortOption(new() { PortName = "COM6", BaudRate = 1_500_000, })); client.Connected = (sender, e) => { Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 连接成功"); isConnected = true; Guardian?.Invoke(); return Task.CompletedTask; }; await client.ConnectAsync(); } catch { } }; Guardian = async () => { while (isConnected) { if ((DateTimeOffset.UtcNow - client!.LastReceivedTime).TotalSeconds < 5) { await Task.Delay(2500); continue; } try { await client.SendAsync(new byte[] { 0xEF, 0xEB, 0xE2, 0x05, 0x68, 0x36, 0xD6, 0x3A }); // 发送心跳包,下位机收到后直接回复 } catch { } Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 发送心跳"); await Task.Delay(50); if ((DateTimeOffset.UtcNow - client.LastReceivedTime).TotalSeconds > 5) { Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 响应超时,重新连接"); await Disconnect(); while (client?.MainSerialPort?.IsOpen != true) { await initConnect(); await Task.Delay(3000); } } } }; await initConnect(); Console.ReadKey(); ``` ### 预期结果 下位机可以正常响应心跳,证明设备正常,不应该在底层回调事件中报错“The port is closed”。 ### 实际结果 程序崩溃,报错“The port is closed”。 ### 异常信息   ### Demo 地址(仅限Git地址) #见“如何复现” ### 承诺支持 - [x] 我确定我已经对TouchSocket项目进行了“Star”操作。 ### 承诺规范 - [x] 我确定已完整阅读[Issue提问规范](https://touchsocket.net/docs/current/troubleshootissue),并按照要求填写。 ### 承诺友好 - [x] 我承诺将本着相互尊重、理解和友善的态度进行交流,共同维护好 TouchSocket 来之不易的良好的社区氛围。
### 组件 其他 ### 版本号 3.1.5 ### .NET SDK .NET6 ### 项目类型 WPF ### 操作系统 Windows 11 ### 运行环境 开发环境 (Development) ### 这个问题是否已经存在? - [x] 我已经搜索过现有的问题 (https://gitee.com/RRQM_Home/TouchSocket/issues) ### 如何复现 1. 创建串口连接并打开; 2. 以 SerialPortClientBase.LastReceivedTime 为基准对连接进行保活,超时5秒连接未活跃就发送心跳指令给下位机; 3. 如果下位机超时未响应,则调用 SerialPortClient 的Disconnect和Dispose,然后重新创建SerialPortClient实例并Connect; 4. 反复插拔串口线触发超时重连。 5. 插上串口线不再动作,且心跳正常工作,下位机正常响应。 6. 等待心跳工作10分钟左右,程序崩溃(非稳定复现),报错堆栈信息见“实际结果”。 ``` // See https://aka.ms/new-console-template for more information using TouchSocket.Core; using TouchSocket.SerialPorts; using TouchSocket.Sockets; bool isConnected = false; SerialPortClient? client = null; Action? Guardian = null; var Disconnect = async () => { if (client?.MainSerialPort?.IsOpen == true) { await client.CloseAsync("关闭旧连接"); client.SafeDispose(); } isConnected = false; }; var initConnect = async () => { try { await Disconnect(); client = new(); await client.SetupAsync(new TouchSocketConfig() .SetSerialPortOption(new() { PortName = "COM6", BaudRate = 1_500_000, })); client.Connected = (sender, e) => { Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 连接成功"); isConnected = true; Guardian?.Invoke(); return Task.CompletedTask; }; await client.ConnectAsync(); } catch { } }; Guardian = async () => { while (isConnected) { if ((DateTimeOffset.UtcNow - client!.LastReceivedTime).TotalSeconds < 5) { await Task.Delay(2500); continue; } try { await client.SendAsync(new byte[] { 0xEF, 0xEB, 0xE2, 0x05, 0x68, 0x36, 0xD6, 0x3A }); // 发送心跳包,下位机收到后直接回复 } catch { } Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 发送心跳"); await Task.Delay(50); if ((DateTimeOffset.UtcNow - client.LastReceivedTime).TotalSeconds > 5) { Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} 响应超时,重新连接"); await Disconnect(); while (client?.MainSerialPort?.IsOpen != true) { await initConnect(); await Task.Delay(3000); } } } }; await initConnect(); Console.ReadKey(); ``` ### 预期结果 下位机可以正常响应心跳,证明设备正常,不应该在底层回调事件中报错“The port is closed”。 ### 实际结果 程序崩溃,报错“The port is closed”。 ### 异常信息   ### Demo 地址(仅限Git地址) #见“如何复现” ### 承诺支持 - [x] 我确定我已经对TouchSocket项目进行了“Star”操作。 ### 承诺规范 - [x] 我确定已完整阅读[Issue提问规范](https://touchsocket.net/docs/current/troubleshootissue),并按照要求填写。 ### 承诺友好 - [x] 我承诺将本着相互尊重、理解和友善的态度进行交流,共同维护好 TouchSocket 来之不易的良好的社区氛围。
Comments (
2
)
Sign in
to comment
Status
Done
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
bug
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
Branches (10)
Tags (32)
master
v4.0.0-Alpha
v3.0
v2.1
v2.0
v1.3
v0.7
v0.6
v0.5
RRQMSocket终版(已弃用)
v3.1.16
v3.1.14
v3.1.13
v3.1.12
v3.1.11
v3.1.10
v3.1.9
v3.1.8
v3.0.3
v3.0.10
v3.0.11
v3.0.12
v3.0.13
v3.0.14
v3.0.15
v3.0.16
v3.0.17
v3.0.18
v3.0.19
v3.0.20
v3.0.21
v3.0.23
v3.0.24
v3.0.25
v3.0.26
v3.1.0
v3.1.1
v3.1.2
v3.1.3
v3.1.5
v3.1.6
v3.1.7
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
C#
1
https://gitee.com/RRQM_Home/TouchSocket.git
git@gitee.com:RRQM_Home/TouchSocket.git
RRQM_Home
TouchSocket
TouchSocket
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register