Skip to content

Commit a77d801

Browse files
authored
Merge pull request bugst#140 from cmaglie/initial_rts_dtr
Allow setting RTS and DTR on `Open`
2 parents 7ca3f7d + 3099149 commit a77d801

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

serial.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Port interface {
5353
// NoTimeout should be used as a parameter to SetReadTimeout to disable timeout.
5454
var NoTimeout time.Duration = -1
5555

56-
// ModemStatusBits contains all the modem status bits for a serial port (CTS, DSR, etc...).
56+
// ModemStatusBits contains all the modem input status bits for a serial port (CTS, DSR, etc...).
5757
// It can be retrieved with the Port.GetModemStatusBits() method.
5858
type ModemStatusBits struct {
5959
CTS bool // ClearToSend status
@@ -62,6 +62,16 @@ type ModemStatusBits struct {
6262
DCD bool // DataCarrierDetect status
6363
}
6464

65+
// ModemOutputBits contains all the modem output bits for a serial port.
66+
// This is used in the Mode.InitialStatusBits struct to specify the initial status of the bits.
67+
// Note: Linux and MacOSX (and basically all unix-based systems) can not set the status bits
68+
// before opening the port, even if the initial state of the bit is set to false they will go
69+
// anyway to true for a few milliseconds, resulting in a small pulse.
70+
type ModemOutputBits struct {
71+
RTS bool // ReadyToSend status
72+
DTR bool // DataTerminalReady status
73+
}
74+
6575
// Open opens the serial port using the specified modes
6676
func Open(portName string, mode *Mode) (Port, error) {
6777
return nativeOpen(portName, mode)
@@ -74,10 +84,11 @@ func GetPortsList() ([]string, error) {
7484

7585
// Mode describes a serial port configuration.
7686
type Mode struct {
77-
BaudRate int // The serial port bitrate (aka Baudrate)
78-
DataBits int // Size of the character (must be 5, 6, 7 or 8)
79-
Parity Parity // Parity (see Parity type for more info)
80-
StopBits StopBits // Stop bits (see StopBits type for more info)
87+
BaudRate int // The serial port bitrate (aka Baudrate)
88+
DataBits int // Size of the character (must be 5, 6, 7 or 8)
89+
Parity Parity // Parity (see Parity type for more info)
90+
StopBits StopBits // Stop bits (see StopBits type for more info)
91+
InitialStatusBits *ModemOutputBits // Initial output modem bits status (if nil defaults to DTR=true and RTS=true)
8192
}
8293

8394
// Parity describes a serial port parity setting

serial_unix.go

+20
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
240240
return nil, &PortError{code: InvalidSerialPort}
241241
}
242242

243+
if mode.InitialStatusBits != nil {
244+
status, err := port.getModemBitsStatus()
245+
if err != nil {
246+
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
247+
}
248+
if mode.InitialStatusBits.DTR {
249+
status |= unix.TIOCM_DTR
250+
} else {
251+
status &^= unix.TIOCM_DTR
252+
}
253+
if mode.InitialStatusBits.RTS {
254+
status |= unix.TIOCM_RTS
255+
} else {
256+
status &^= unix.TIOCM_RTS
257+
}
258+
if err := port.setModemBitsStatus(status); err != nil {
259+
return nil, &PortError{code: InvalidSerialPort, causedBy: err}
260+
}
261+
}
262+
243263
// MacOSX require that this operation is the last one otherwise an
244264
// 'Invalid serial port' error is returned... don't know why...
245265
if port.SetMode(mode) != nil {

serial_windows.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,19 @@ func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
450450
port.Close()
451451
return nil, &PortError{code: InvalidSerialPort}
452452
}
453-
params.Flags &= dcbRTSControlDisbaleMask
454-
params.Flags |= dcbRTSControlEnable
455453
params.Flags &= dcbDTRControlDisableMask
456-
params.Flags |= dcbDTRControlEnable
454+
params.Flags &= dcbRTSControlDisbaleMask
455+
if mode.InitialStatusBits == nil {
456+
params.Flags |= dcbDTRControlEnable
457+
params.Flags |= dcbRTSControlEnable
458+
} else {
459+
if mode.InitialStatusBits.DTR {
460+
params.Flags |= dcbDTRControlEnable
461+
}
462+
if mode.InitialStatusBits.RTS {
463+
params.Flags |= dcbRTSControlEnable
464+
}
465+
}
457466
params.Flags &^= dcbOutXCTSFlow
458467
params.Flags &^= dcbOutXDSRFlow
459468
params.Flags &^= dcbDSRSensitivity

0 commit comments

Comments
 (0)