From 007072bd57d39a333a6771cde4570ed6b5d924fc Mon Sep 17 00:00:00 2001 From: Nick Patavalis Date: Sat, 20 Jan 2018 11:38:43 +0200 Subject: [PATCH] Better --lower/raise-dtr/rts handling Set the levels of DTR and RTS modem-control lines (according to the --lower/raise-dtr/rts command-line options) *twice*: Once immediatelly (as quickly as possible) after opening the port (before actually configuring it), and once after configuring it. On some systems configuring the port (i.e. calling tcsetattr) has the effect of reseting the DTR and RTS signals. --- picocom.c | 81 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/picocom.c b/picocom.c index f97faa6..644f3b3 100644 --- a/picocom.c +++ b/picocom.c @@ -1986,6 +1986,47 @@ parse_args(int argc, char *argv[]) /**********************************************************************/ +void +set_dtr_rts (void) +{ + int r; + if ( opts.lower_rts ) { + r = term_lower_rts(tty_fd); + if ( r < 0 ) + fatal("failed to lower RTS of port: %s", + term_strerror(term_errno, errno)); + rts_up = 0; + } else if ( opts.raise_rts ) { + r = term_raise_rts(tty_fd); + if ( r < 0 ) + fatal("failed to raise RTS of port: %s", + term_strerror(term_errno, errno)); + rts_up = 1; + } + + if ( opts.lower_dtr ) { + r = term_lower_dtr(tty_fd); + if ( r < 0 ) + fatal("failed to lower DTR of port: %s", + term_strerror(term_errno, errno)); + dtr_up = 0; + } else if ( opts.raise_dtr ) { + r = term_raise_dtr(tty_fd); + if ( r < 0 ) + fatal("failed to raise DTR of port: %s", + term_strerror(term_errno, errno)); + dtr_up = 1; + } + /* Try to read the status of the modem-conrtol lines from the + port. */ + r = term_get_mctl(tty_fd); + if ( r >= 0 && r != MCTL_UNAVAIL ) { + rts_up = (r & MCTL_RTS) != 0; + dtr_up = (r & MCTL_DTR) != 0; + } +} + + int main (int argc, char *argv[]) { @@ -2042,43 +2083,17 @@ main (int argc, char *argv[]) } if ( r < 0 ) fatal("failed to add port: %s", term_strerror(term_errno, errno)); - if ( opts.lower_rts ) { - r = term_lower_rts(tty_fd); - if ( r < 0 ) - fatal("failed to lower RTS of port: %s", - term_strerror(term_errno, errno)); - rts_up = 0; - } else if ( opts.raise_rts ) { - r = term_raise_rts(tty_fd); - if ( r < 0 ) - fatal("failed to raise RTS of port: %s", - term_strerror(term_errno, errno)); - rts_up = 1; - - } - if ( opts.lower_dtr ) { - r = term_lower_dtr(tty_fd); - if ( r < 0 ) - fatal("failed to lower DTR of port: %s", - term_strerror(term_errno, errno)); - dtr_up = 0; - } else if ( opts.raise_dtr ) { - r = term_raise_dtr(tty_fd); - if ( r < 0 ) - fatal("failed to raise DTR of port: %s", - term_strerror(term_errno, errno)); - dtr_up = 1; - } + /* Set DTR and RTS status, as quickly as possible after opening + the serial port (i.e. before configuring it) */ + set_dtr_rts(); r = term_apply(tty_fd, 0); if ( r < 0 ) fatal("failed to config port: %s", term_strerror(term_errno, errno)); - - r = term_get_mctl(tty_fd); - if ( r >= 0 && r != MCTL_UNAVAIL ) { - rts_up = (r & MCTL_RTS) != 0; - dtr_up = (r & MCTL_DTR) != 0; - } + /* Set DTR and RTS status *again* after configuring the port. On + some systems term_apply() resets the status of DTR and / or + RTS */ + set_dtr_rts(); set_tty_write_sz(term_get_baudrate(tty_fd, NULL));