Factorize a function to enable/disable of RTS/CTS handshake for unix

This commit is contained in:
Cristian Maglie
2016-10-20 23:57:09 +02:00
parent 9398f35b69
commit 50993cc77b

View File

@@ -72,13 +72,18 @@ func nativeOpen(portName string, mode *Mode) (*unixPort, error) {
return nil, &PortError{code: InvalidSerialPort}
}
// Set raw mode
settings, err := port.getTermSettings()
if err != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
}
// Set raw mode
setRawMode(settings)
// Explicitly disable RTS/CTS flow control
setTermSettingsCtsRts(false, settings)
if port.setTermSettings(settings) != nil {
port.Close()
return nil, &PortError{code: InvalidSerialPort}
@@ -199,13 +204,18 @@ func setTermSettingsStopBits(bits StopBits, settings *syscall.Termios) error {
return nil
}
func setTermSettingsCtsRts(enable bool, settings *syscall.Termios) {
if enable {
settings.Cflag |= termiosMask(tcCRTSCTS)
} else {
settings.Cflag &= ^termiosMask(tcCRTSCTS)
}
}
func setRawMode(settings *syscall.Termios) {
// Set local mode
settings.Cflag |= termiosMask(syscall.CREAD | syscall.CLOCAL)
// Explicitly disable RTS/CTS flow control
settings.Cflag &= ^termiosMask(tcCRTSCTS)
// Set raw mode
settings.Lflag &= ^termiosMask(syscall.ICANON | syscall.ECHO | syscall.ECHOE | syscall.ECHOK |
syscall.ECHONL | syscall.ECHOCTL | syscall.ECHOPRT | syscall.ECHOKE | syscall.ISIG | syscall.IEXTEN)