0

I was looking to modify some terminal code to make it portable and work nicely with windows clients and encountered this issue.

When I press enter in a windows terminal, MicroPython (v1.19.1 on a Raspberry Pi Pico) appears to recieve 2 linefeed characters (0x0A + 0x0A) instead of a carriage return then a linefeed (0x0D + 0x0A).

Code:

import sys

while(True):
    b = sys.stdin.read(1)
    sys.stdout.write("(" + hex(ord(b)) + ")")

Run the code, connect to port using a modified PuTTY configured to send a CRLF, hit enter and you get:

enter image description here

I get similar results when using the Thonny Shell or the REPL shell included in the Micropython plugin for PyCharm.

Am I doing something wrong here or is this a quirk (or bug) with MicroPython?

sandyscott
  • 181
  • 9

1 Answers1

0

It turns out this is standard behaviour for Python, so has been implemented in MicroPython.

It's related to PEP 278 – Universal Newline Support, which translates all newlines to a single linefeed "\n".

In this particular case, because I'm reading the characters one-by-one it has no chance to detect the CR + LF together and translate them to a single LF, so each is read, and the CR gets turned into an LF, and the LF makes it through unmodified.

You can use sys.stdin.buffer.read(1) to get the unmodified stream, in which case my test code becomes:

import sys

while(True):
    b = sys.stdin.buffer.read(1)
    print("(" + hex(ord(b)) + ")")

This produces the expected output:

enter image description here

sandyscott
  • 181
  • 9