3

I would like to use screen as serial terminal but my output is not formatted well. I use this command:

sudo screen /dev/ttyACM0 9600,cs8

thinking the configuration above is like 9600 8N1 with no parity and no flow control configuration, and I got this output:

This program runs since 135 seconds.
                                                This program runs since 136 seconds.
                                                                                    This program runs since 137 seconds.
                    This program runs since 138 seconds.
                                                        This program runs since 139 seconds.
                                                                                            This program runs since 140 seconds.
                                This program runs since 141 seconds.

In QTKTerm I've got the same result until I set CR LF auto mode in the configuration menu:

This program runs since 759 seconds.
This program runs since 760 seconds.
This program runs since 761 seconds.

How to have the same result in screen ?

Thanks for your help.

3 Answers 3

3

TLDR: you can't; Screen will override any options set via stty when it starts. It doesn't support the ocrnl/onlcr/icrnl/inlcr options required to configure carriage return vs. newline. It opens the terminal device in exclusive mode, so you can't use stty to change these options after screen starts. This is true for Screen version 4.08.00 (GNU), 05-Feb-20.

Long form:

I've run into this same issue, and have searched through several related questions and answers across the stack exchange universe. I'm forced to conclude that You can't get screen to send Line Feed (LF; \n) instead of Carriage Return (CR; \r), at least not without changing something in the source and recompiling.

This question asks how to send LF instead of CR from screen. It doesn't have an answer yet. This answer is wrong (stty -F /dev/YOURSERIALDEVICE YOURBAUDRATE raw won't change how screen configures the tty). This answer suggests using picocom, but doesn't answer the question of how to get screen to send the correct newline character. It does, however, work:

picocom /dev/YOURSERIALDEVICE --baud YOURBAUDRATE --omap crcrlf --echo
# (press Control+a Control+x to exit)

There are hints for how one might address this for screen in the stty man page. Screen forwards the comma-delimited list of options after the tty file to stty, so some of these options might help:

> man stty
# (output abridged..)
Input settings:
       [-]icrnl    translate carriage return to newline
       [-]inlcr    translate newline to carriage return
Output settings:
       [-]ocrnl    translate carriage return to newline
       [-]onlcr    translate newline to carriage return-newline
       [-]onlret   newline performs a carriage return

However, setting both input and output to replace \r with \n, ensuring that \n isn't replaced by \r, and ensuring that onlret is not set ... doesn't make a difference,

screen /dev/YOURSERIALDEVICE YOURBAUDRATE,ocrnl,-onlcr,icrnl,-inlcr,-onlret

behaves the same as before. I'm not sure why screen doesn't seem to apply the options. This answer to a vaguely similar question suggests using stty to change the options.

> stty -F /dev/YOURSERIALDEVICE YOURBAUDRATE ocrnl -onlcr icrnl -inlcr -onlret
> screen /dev/YOURSERIALDEVICE

This does nothing, because screen resets any configuration performed by stty when it starts. This answer claims that one can run stty after screen starts, but when I do this I get the error Device or resource busy.

This answer also addresses the issue with CR/LF in screen. It refers to the Window Types section of screen's online documentation.

  • "an exclusive open is attempted on the node to mark the connection line as busy" -- so we shouldn't be able to change the tty options after starting screen.

  • Only baud_rate, cs8/cs7 (number of bits per byte), ixon/off, and istrip are documented options. This probably means that ocrnl etc. aren't recognized/supported by screen.

In summary: The question of how to get screen to send LF (\n) instead of CR (\r) has been asked several times on various stack-exchange sites. None of the provided answers work, and a thorough reading of the documentation suggests that it's not possible at this time.

3

According to the screen manual, in WINDOW TYPES:

If a tty (character special device) name (e.g. "/dev/ttya") is specified as the first parameter, then the window is directly connected to this device. This window type is similar to "screen cu -l /dev/ttya". Read and write access is required on the device node, an exclusive open is attempted on the node to mark the connection line as busy. An optional parameter is allowed consisting of a comma separated list of flags in the notation used by ‘stty(1)’:

It goes on to list a few of the possible flags, but adds

You may want to specify as many of these options as applicable. Unspecified options cause the terminal driver to make up the parameter values of the connection. These values are system-dependent and may be in defaults or values saved from a previous connection.

The usual place to look for more information is in the stty manual, because a pseudo-terminal connection supports those additional flags to stty.

The misbehavior that your question resembles most closely should be fixed by adding the onlcr flag, listed under "Output settings":

[-]onlcr
translate newline to carriage return-newline

For example:

sudo screen /dev/ttyACM0 9600,cs8,onlcr

However, as noted in a comment, screen does its own processing of this information. The tty.sh script generates the source-code (for SttyMode) which screen uses to process the command-line options such as cs8. It does nothing for any of the stty CR/NL mappings in that function. Based on a comment in the source,

    if (!ttyflag) {     /* may not even be good for ptys.. */
#if defined(ICRNL)
        m->tio.c_iflag |= ICRNL;
#endif              /* ICRNL */
#if defined(ONLCR)
        m->tio.c_oflag |= ONLCR;
#endif  

it seems that the developers found that sort of thing awkward to implement, and simply chose to not support it.

3
  • 2
    Thanks but why the option onlcr doesn't work for me : sudo screen /dev/ttyACM0 9600,cs8,-ixon,-ixoff,onlcr
    – dubis
    Commented Oct 27, 2015 at 18:19
  • I can confirm that the onlcr option does not appear to work in Screen version 4.08.00 (GNU) 05-Feb-20.
    – MRule
    Commented Oct 6, 2021 at 9:11
  • On the linked documentation, onl baud_rate, cs8/cs7 (number of bits per byte), ixon/off, and istrip are documented options. This probable means that ocrnl etc. aren't recognized/supported by screen. So this answer won't do! There might be no way to fix this...
    – MRule
    Commented Oct 6, 2021 at 9:15
0

This helped me, you can enable/disable crlf in screen:

https://gist.github.com/cabo/ce6358406f8175041813

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .