4

I want to use GNU screen to monitor to a serial USB port like this:

$ screen /dev/ttyUSB0 115200

But I need to tweak a few terminal line settings. I have made several attempts but none seem to work. For example, to send NL+CR for a newline character, not just NL, the terminal line setting is onlcr.

Attempt 1 - without any special settings:

$ screen /dev/ttyUSB0 115200
# only sends NL

Attempt 2 - via screen:

$ screen /dev/ttyUSB0 115200,onlcr
# still only sends NL

Attempt 3 - via ssty:

$ stty -F /dev/ttyUSB0 onlcr
$ screen /dev/ttyUSB0 115200
# still only sends NL

Attempt 4 - via both:

$ stty -F /dev/ttyUSB0 onlcr
$ screen /dev/ttyUSB0 115200,onlcr
# still only sends NL

Attempt 5 - in the other order:

$ screen /dev/ttyUSB0 115200,onlcr
# then ctrl+a, ctrl+z to pause the screen session
$ stty -F /dev/ttyUSB0 onlcr
stty: /dev/ttyUSB0: Device or resource busy

In all cases, if I run stty to check the terminal line settings I get this:

Before running screen - note the -onlcr is present:

$ stty -F /dev/ttyUSB0 
speed 115200 baud; line = 0;
kill = ^H; min = 100; time = 2;
-icrnl -imaxbel
-opost -onlcr
-isig -icanon -echo

Changing the stty setting - note the -onlcr has gone:

$ stty -F /dev/ttyUSB0 onlcr
$ stty -F /dev/ttyUSB0 
speed 115200 baud; line = 0;
kill = ^H; min = 100; time = 2;
-icrnl -imaxbel
-opost 
-isig -icanon -echo

After running screen - note the -onlcr is back again:

$ stty -F /dev/ttyUSB0 
speed 115200 baud; line = 0;
kill = ^H; min = 100; time = 2;
-icrnl -imaxbel
-opost -onlcr
-isig -icanon -echo

It's as if screen ignores any stty settings and resets them to it's own defaults. And this the same on both machines I tested it on; Debain 8.7 and macOS Sierra 10.12.4

I have seen posts with other people facing similar problems but none give a definitive answer. Many people end up recommending an alternative to screen like minicom but now I'm interested.

Can stty settings, like onlcr, be used with screen?

Adam Griffiths
  • 1,602
  • 1
  • 13
  • 17

3 Answers3

1

I just went through much the same process, only to find out the screen clobbers the stty settings no matter what. One comment here suggests changing the screen source code: How to toggle CR/LF in gnu screen?

In the end, I was very happy to find a more minimal dumb-terminal program that allows passing stty-style arguments on the command line: https://github.com/npat-efault/picocom

It also pretty-prints the serial port settings when it starts so you can easily check them.

Packages exist in Debian-derived distros (sudo apt-get install picocom), and for others it seems that compilation is straightforward. One dependency is the linenoise library, which can be disabled.

remcycles
  • 1,246
  • 13
  • 14
0

The way I handled this is to start screen running without any special terminal options using in your case: screen /dev/ttyUSB0 115200, and then leave it running, and switch over to another terminal window and do the stty commands from there.

If you stop screen, I think it locks the device somehow (or else its because you aren't using sudo). Once in the other window you can type the sudo stty -F /dev/ttyUSB0 onlcr and that will change the behavior of the screen session from then on. The problem is that screen is ignoring the setting passed to it and just configuring the tty its own way. You can type sudo stty -F /dev/ttyUSB0 -a to list the settings from another window while screen is running and then alter them as needed. (Note that on the Mac, the -F should be lower case.)

If you only have the one terminal window then it is problematic because putting screen in the background and leaving it running just mixes input and output streams between the shell and screen process.

Robotbugs
  • 4,307
  • 3
  • 22
  • 30
0

Another thing you can do if you have two terminal windows open is a low-level fallback hack when nothing is working: In one terminal type cat /dev/ttyUSB0. Then in the other set up the terminal options you want with sudo stty. Then from this same terminal you can send commands via echo "Some text" > /dev/ttyUSB0. You can translate the output of the cat command by piping it through sed or tr. For example cat /dev/ttyUSB0 | tr '\r' '\n' will translate any received carriage returns to line feeds.

Robotbugs
  • 4,307
  • 3
  • 22
  • 30