From 9710c814f629cf9836385479554d79822a1e1ec2 Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Wed, 7 May 2025 20:44:25 +0200 Subject: [PATCH 1/3] fix: incorrect masks for modem status bits on Windows --- serial_windows.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/serial_windows.go b/serial_windows.go index 7aa0123..54d2a89 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -280,16 +280,24 @@ func (port *windowsPort) SetRTS(rts bool) error { return nil } +// GetCommModemStatus constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommmodemstatus. +const ( + msCTSOn = 0x0010 + msDSROn = 0x0020 + msRingOn = 0x0040 + msRLSDOn = 0x0080 +) + func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) { var bits uint32 if err := windows.GetCommModemStatus(port.handle, &bits); err != nil { return nil, &PortError{} } return &ModemStatusBits{ - CTS: (bits & windows.EV_CTS) != 0, - DCD: (bits & windows.EV_RLSD) != 0, - DSR: (bits & windows.EV_DSR) != 0, - RI: (bits & windows.EV_RING) != 0, + CTS: (bits & msCTSOn) != 0, + DCD: (bits & msRLSDOn) != 0, + DSR: (bits & msDSROn) != 0, + RI: (bits & msRingOn) != 0, }, nil } @@ -366,7 +374,8 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) { 0, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_OVERLAPPED, - 0) + 0, + ) if err != nil { switch err { case windows.ERROR_ACCESS_DENIED: From 20a47944cfa974f7ff695f355056c22e62171c95 Mon Sep 17 00:00:00 2001 From: WarningImHack3r <43064022+WarningImHack3r@users.noreply.github.com> Date: Thu, 8 May 2025 13:17:50 +0200 Subject: [PATCH 2/3] refactor: rename variables to match docs --- serial_windows.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_windows.go b/serial_windows.go index 54d2a89..5c5a2a4 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -282,10 +282,10 @@ func (port *windowsPort) SetRTS(rts bool) error { // GetCommModemStatus constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommmodemstatus. const ( - msCTSOn = 0x0010 - msDSROn = 0x0020 - msRingOn = 0x0040 - msRLSDOn = 0x0080 + MS_CTS_ON = 0x0010 + MS_DSR_ON = 0x0020 + MS_RING_ON = 0x0040 + MS_RLSD_ON = 0x0080 ) func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) { @@ -294,10 +294,10 @@ func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) { return nil, &PortError{} } return &ModemStatusBits{ - CTS: (bits & msCTSOn) != 0, - DCD: (bits & msRLSDOn) != 0, - DSR: (bits & msDSROn) != 0, - RI: (bits & msRingOn) != 0, + CTS: (bits & MS_CTS_ON) != 0, + DCD: (bits & MS_RLSD_ON) != 0, + DSR: (bits & MS_DSR_ON) != 0, + RI: (bits & MS_RING_ON) != 0, }, nil } From 8f447ebc07eb5bec7d0654c8a44cc6f5ed05d48f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 19 May 2025 15:31:36 +0200 Subject: [PATCH 3/3] Make win32 internal constants private. --- serial_windows.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/serial_windows.go b/serial_windows.go index 5c5a2a4..d2800f8 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -280,15 +280,14 @@ func (port *windowsPort) SetRTS(rts bool) error { return nil } -// GetCommModemStatus constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommmodemstatus. -const ( - MS_CTS_ON = 0x0010 - MS_DSR_ON = 0x0020 - MS_RING_ON = 0x0040 - MS_RLSD_ON = 0x0080 -) - func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) { + // GetCommModemStatus constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommmodemstatus. + const ( + MS_CTS_ON = 0x0010 + MS_DSR_ON = 0x0020 + MS_RING_ON = 0x0040 + MS_RLSD_ON = 0x0080 + ) var bits uint32 if err := windows.GetCommModemStatus(port.handle, &bits); err != nil { return nil, &PortError{}