Skip to content

Commit 1976cc1

Browse files
committed
simplify read timeout
1 parent 9bd0960 commit 1976cc1

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

transport/uart/uart.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ func isWindows() bool {
6868
return runtime.GOOS == "windows"
6969
}
7070

71-
// getWindowsTimeout returns Windows-specific timeout values
72-
func getWindowsTimeout() time.Duration {
73-
if isWindows() {
74-
return 100 * time.Millisecond // Increased for Windows stability
75-
}
76-
// Relax non-Windows default read timeout to improve reliability with USB-UART
71+
// getReadTimeout returns the unified read timeout for all platforms
72+
// Originally Windows needed 100ms while other platforms used 50ms, but Linux users
73+
// reported intermittent empty data issues, so we unified to 100ms for all platforms
74+
func getReadTimeout() time.Duration {
7775
return 100 * time.Millisecond
7876
}
7977

@@ -96,9 +94,9 @@ func New(portName string) (*Transport, error) {
9694
return nil, fmt.Errorf("failed to open UART port %s: %w", portName, err)
9795
}
9896

99-
// Set platform-specific timeout - increased for Windows due to driver differences
100-
// 50ms proven to work on Linux/Mac, 100ms needed for Windows stability
101-
timeout := getWindowsTimeout()
97+
// Set unified read timeout - originally 50ms on Linux/Mac and 100ms on Windows,
98+
// but unified to 100ms on all platforms to resolve Linux USB-UART reliability issues
99+
timeout := getReadTimeout()
102100
if err := port.SetReadTimeout(timeout); err != nil {
103101
_ = port.Close()
104102
return nil, fmt.Errorf("failed to set UART read timeout: %w", err)

transport/uart/windows_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,15 @@ func TestWindowsPlatformDetection(t *testing.T) {
3838
}
3939
}
4040

41-
// TestWindowsSpecificTimeout tests Windows-specific timeout values
42-
func TestWindowsSpecificTimeout(t *testing.T) {
41+
// TestUnifiedTimeout tests the unified timeout value for all platforms
42+
func TestUnifiedTimeout(t *testing.T) {
4343
t.Parallel()
4444

45-
timeout := getWindowsTimeout()
45+
timeout := getReadTimeout()
46+
expectedTimeout := 100 * time.Millisecond
4647

47-
if runtime.GOOS == "windows" {
48-
expectedTimeout := 100 * time.Millisecond
49-
if timeout != expectedTimeout {
50-
t.Errorf("getWindowsTimeout() on Windows = %v, want %v", timeout, expectedTimeout)
51-
}
52-
} else {
53-
expectedTimeout := 50 * time.Millisecond
54-
if timeout != expectedTimeout {
55-
t.Errorf("getWindowsTimeout() on non-Windows = %v, want %v", timeout, expectedTimeout)
56-
}
48+
if timeout != expectedTimeout {
49+
t.Errorf("getReadTimeout() = %v, want %v", timeout, expectedTimeout)
5750
}
5851
}
5952

@@ -94,7 +87,7 @@ func TestWindowsIntegrationFunctions(t *testing.T) {
9487
}
9588

9689
// Test timeout function
97-
timeout := getWindowsTimeout()
90+
timeout := getReadTimeout()
9891
if timeout <= 0 {
9992
t.Errorf("Invalid timeout: %v", timeout)
10093
}

0 commit comments

Comments
 (0)