dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.25k stars 4.73k forks source link

SerialPort: continuously `TimeoutException` when write bytes first time failed. #70130

Open KalibGao opened 2 years ago

KalibGao commented 2 years ago

Description

In windows 10, when the first TimeoutException has occurred at SerialStream => Write. then the next requests written to the serial port will fail and throw by the same exception.

Reproduction Steps

2022-06-02 09:03:27.122 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)
2022-06-02 09:03:27.351 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)
2022-06-02 09:03:27.604 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)
2022-06-02 09:03:27.840 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)
2022-06-02 09:03:28.070 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)
2022-06-02 09:03:28.081 +08:00 [WRN] runner has been stopped, connect point: A-1-1, response: Request time:  06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ], Reply: [ ], druation: 0.
2022-06-02 09:03:28.319 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time:  06-02 09:03:28:081, Cmd: [ 81 01 07 01 04 8E ]..
System.TimeoutException: The write timed out.
   at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd)
   at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd)
   at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken)

Expected behavior

Actual behavior

when I look into the source code, the write method throw TimeoutException is here.

https://github.com/dotnet/runtime/blob/f5d308df7f179a2e31d3ab49cc727832ef0a4a5d/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs#L1067-L1101

Regression?

No response

Known Workarounds

Configuration

No response

Other information

No response

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
### Description In windows 10, when the first TimeoutException has occurred at `SerialStream` => `Write`. then the next requests written to the serial port will fail and throw by the same exception. I look into the source code, the only write exception is here. the previous write error will affect the next write()? ### Reproduction Steps https://github.com/dotnet/runtime/blob/f5d308df7f179a2e31d3ab49cc727832ef0a4a5d/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs#L1067-L1101 ### Expected behavior The issue has some related to #20370, it is hanging still, any update? ### Actual behavior ``` 2022-06-02 09:03:27.122 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) 2022-06-02 09:03:27.351 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) 2022-06-02 09:03:27.604 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) 2022-06-02 09:03:27.840 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) 2022-06-02 09:03:28.070 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) 2022-06-02 09:03:28.081 +08:00 [WRN] runner has been stopped, connect point: A-1-1, response: Request time: 06-02 09:03:26:998, Cmd: [ 81 01 16 1E 46 19 11 4A 49 38 F6 E0 01 00 00 00 00 00 00 00 00 00 00 00 00 00 05 01 00 00 00 00 00 00 CE ], Reply: [ ], druation: 0. 2022-06-02 09:03:28.319 +08:00 [WRN] cmd execute error at COM19, request: Channel: COM19-1-1, Request time: 06-02 09:03:28:081, Cmd: [ 81 01 07 01 04 8E ].. System.TimeoutException: The write timed out. at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(Byte[] buffer, Int32 offset, Int32 count) at Learning.Devices.Infrastructure.Executors.BothExecutor.WriteAndReadCStarCmd(Cmd cmd) at Learning.Devices.Infrastructure.Executors.BothExecutor.Execute(Cmd cmd) at Learning.Devices.Infrastructure.Processers.PortProcesser.ProcessRequestInQueue(CancellationToken stoppingToken) ``` ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration _No response_ ### Other information _No response_
Author: KalibGao
Assignees: -
Labels: `area-System.IO`, `untriaged`
Milestone: -
valeriob commented 1 year ago

I'm having the same problem, thanks for reporting this.