Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get urwid to work over COM port #506

Open
nmartont opened this issue Jul 26, 2022 · 1 comment
Open

Cannot get urwid to work over COM port #506

nmartont opened this issue Jul 26, 2022 · 1 comment

Comments

@nmartont
Copy link

Hi all,

I am trying to have a urwid menu (Hello World example) available over a serial COM port instead of stdin/stdout, however, it does not seem to work.

I use two connected serial -> USB adapters on my PC running Ubuntu 20.04, the devices are ttyUSB0 and ttyUSB1.
urwid uses ttyUSB0, and the menu is supposed to be used via minicom on ttyUSB1.
Minicom works with other devices.

The simplest working example is the following:

import urwid
import sys
import serial


class COMScreen(urwid.raw_display.Screen):

    def __init__(self, input=sys.stdin, output=sys.stdout):
        super().__init__(input, output)

    def write(self, data):
        self._log("WRITE: "   " ".join(data)   "\n")
        super().write(data.encode())

    def _getch(self, timeout):
        c = super()._getch(timeout)
        self._log("CHAR: "   str(c))
        return c

    def _log(self, stuff):
        print(stuff)


ser = serial.Serial("/dev/ttyUSB0", baudrate=9600,
                    bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
                    xonxoff=False, rtscts=False)

txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'top')

with open('/dev/ttyUSB0', 'rb') as inf, open('/dev/ttyUSB0', 'wb') as outf:
    loop = urwid.MainLoop(fill, screen=COMScreen(input=inf, output=outf))
    loop.run()

When the program starts, it outputs this (note that the Hello World text is completely missing):

WRITE:   ? 4 7 h

WRITE:  ? 1 0 0 2 l  ? 1 0 0 0 l

WRITE:  ? 1 0 0 0 h  ? 1 0 0 2 h

WRITE: 

WRITE:  ? 2 5 l

WRITE:  0 ; 3 9 ; 4 9 m

WRITE:  H

WRITE:  1 ; 1 H

Keypresses in the minicom window are registered the following way:

CHAR: 102
CHAR: -1
WRITE:  ? 2 5 l

WRITE:  0 ; 3 9 ; 4 9 m

WRITE:  H

WRITE:  1 ; 1 H

Unfortunately I have no idea why the program does not print Hello World at all. Is it waiting for some event that the stdin/out terminal provides, but the serial port does not? Has anyone managed to get urwid working over COM port?

Thank you very much for the help!

@penguinolog
Copy link
Collaborator

urwid.raw_display.Screen is designed to interact with VT100-like terminals.
For "dumb" terminal need to make full Screen class: strip/convert most control sequences to the terminal compatible.
If you will make such PR - it will be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants