2

bash works with STDIN, STDOUT and STDERR. When xterm is opened, it allocates the pseudo tty. Then xterm forks child process and STDIN, STDOUT, STDERR are tied to slave_fd via dup2 and exec is called with bash. This is good.

But how to make bash work with TTY directly? How to make bash execute open("/dev/ttyS0", O_RDWR)? So that I could attach a device to /dev/ttyS0 and execute commands in bash from the device.

  • 1
    Take a look into the agetty utility. It handles the TTY stuff, then does a login or starts an alternative program. – Janka Aug 21 '19 at 01:55
  • @Janka I see - TTY is opened by a separate program. So, NO program is supposed to open a TTY directly? Only a dedicated program? The other way around: what if I have a program which does interaction via TTY by doing open("/dev/ttyS0", O_RDWR) and sometimes I want to interact with the program via xterm, without changing the program itself - is this possible? – Igor Liferenko Aug 21 '19 at 02:21
  • 2
    exec 7<>/dev/ttyS0; stty <&7 [your stty settings]; cmd1 >&7; cmd2 <&7; cmd3 <&7 >&7 2>&1. Setting the right modes & speed and using serial devices from the shell and parsing responses is tricky, though. –  Aug 21 '19 at 04:07
  • That comment would merit being turned into an answer, @UncleBilly. – Chris Davies Aug 21 '19 at 06:55
  • What kind of a device is it? Is it a terminal and you want to log in from it (have a shell listening to commands from the serial line); or is it something like a switch that provides its own console on the serial line and you want to connect to it? – ilkkachu Aug 21 '19 at 06:55
  • @ilkkachu It is a device which sends characters and reads characters, one by one. This device interfaces with self-made program on PC, which does open on the TTY. I asked this question because I was curious about general mechanisms how programs connect to TTY. As it turned out, as a rule an intermediate program is used for that. – Igor Liferenko Aug 21 '19 at 07:33

1 Answers1

4

If you know the baud rate you can use screen to connect serial terminals like : screen /dev/ttyS0 115200

  • Can screen be used to connect to pseudo terminals? This does not work: screen /dev/pts/X (where /dev/pts/X was created by opening a new xterm window and issuing kill -STOP $$ in it to prevent bash from messing with the tty but at the same time keep terminal window open to use the tty). – Igor Liferenko Aug 22 '19 at 00:52