33

I'm using screen to read the text from a serial console. The problem is the output seems to only have newline \n but not carriage return \r, so the display looks like this...

Line1
     Line2
          Line3

I wonder if there is any patch to fix this issue?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Patrick
  • 4,186
  • 9
  • 32
  • 45
  • 1
    Maybe some `stty` setting? Though I find it strange that you see that when _reading_. – ninjalj Oct 18 '11 at 19:21
  • @ninjalj, his was years ago now. However, it makes sense to observe this while reading. In this case the device is not sending carriage return characters, so `stty` instructs the terminal to treat a newline as if it were a newline _and_ carriage return. Otherwise we can see the terminal moves to the next line but does not return the index pointer to the beginning of the line, like it would if `\r` had been sent along with `\n`. – sherrellbc Feb 20 '18 at 16:12

4 Answers4

30

onlcr is for translating outgoing newlines to carriage returns.

stty -F /dev/ttyS0 inlcr will translate incoming newlines to carriage returns. You can run that from another terminal after starting screen to avoid any resetting that screen may do on startup. Unfortunately however, this will only change the problem. You'll then get only returns and no newlines.

What is needed is an option to append a return to an incoming newline so that the terminal receives \n\r, which is what the serial device should have output in the first place. There seems to be an onlret option to do this for outgoing data, but no inlret option as we would seem to need in this case.

I have the exact same problem (using picocom though) and I've been googling off and on for days trying to find the standard fix, but no one seems to have one. There are a number of serial devices out there which only output \n and simply can't be made to output \r\n and I refuse to believe that all of them belong to only two linux users. What gives!?

tacos
  • 356
  • 3
  • 4
  • 11
    In picocom you can add `--imap lfcrlf` to the command line options. This will translate LF to CR + LF after they are read from the serial port. – andreasw Jul 25 '14 at 19:44
  • Also, for those using Gtkterm check "CR LF auto" under the "Configuration" menu. – andreasw Aug 20 '14 at 00:19
  • 1
    microcom does handle the mapping automatically – stefanct Apr 06 '15 at 15:55
  • 1
    " You can run that from another terminal after starting screen to avoid any resetting that screen may do on startup. " --- this doesn't seem to be true in all cases. On my device, if I try to run `stty` after starting screen, I get `Device or resource busy`. – MRule Oct 06 '21 at 09:06
15

If you use the miniterm.py program that comes with pyserial it will interpret newlines as crlf. It is not the most fully-featured terminal emulator but for interacting with simple serial devices it gets the job done.

Usage syntax (on OSX):

miniterm.py /dev/tty.usbserial-XXXXXX 115200

Replace XXXXXX with whatever the device comes up on your system as.

Peter E.
  • 193
  • 2
  • 6
  • 1
    Thanks for sharing, screen was really getting on my nerves! – nic Jan 30 '15 at 05:50
  • Searched my linux distro and found `miniterm.py` in several git repos. Note I had to run `sudo python /path/to/miniterm.py ...` – Ari Aug 08 '23 at 14:12
4

Try stty onlcr.

The man page says it will translate newlines to carriage return / newline pairs on output, which seems to be what you need.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 2
    Yeah, I tried this, but it didn't work. stty -F /dev/ttyS0 onlcr – Patrick Oct 18 '11 at 21:19
  • 1
    Strangely, after I used screen to open the serial port, the stty setting change back to -onlcr. I wonder if screen override the setting set in stty? The command I used to run screen: screen /dev/ttyS0 115200 – Patrick Oct 18 '11 at 21:20
  • 3
    Interesting, looks like `screen` resets the TTY it's running on when starting. I don't know if you can get around that without patching and building your own binary. – Frédéric Hamidi Oct 18 '11 at 21:28
  • 1
    In screen source code, tty.c there is defined(ONLCR). I'll try it out – Patrick Oct 18 '11 at 21:48
  • 1
    Does anyone know how to configure tty.sh? – Patrick Oct 18 '11 at 21:54
  • 1
    In the past, onlcr didn't work on USB to serial adapters using the pl2303 driver. That bug has been fixed, but some devices use old Linux kernels. – dreamlayers Nov 01 '11 at 02:26
3

In my case worked: stty -F /dev/ttyACM0 -icrnl

Because the serial was implicitly set to translate CR to NL. This command set it back. Notice the minus character preceding icrnl.

Fabian Horlacher
  • 1,899
  • 1
  • 24
  • 31
jejdacz
  • 31
  • 2
  • This doesn't seem to work for me. Screen resets any options configured by stty when it starts, so this does nothing. Providing `-icrnl` to screen seems to be ignored? – MRule Oct 06 '21 at 09:04