Page 1 of 1

UART inheritance mystery

Posted: Thu Nov 29, 2018 9:10 am
by Hanilein
A "What a Terrible Failure"-moment (a.k.a WTF) for me...

I am developing a set of classes to be used with terminals. I am deriving from UART as follows:

Code: Select all

from pyb import UART

class serialTerminal(UART):
    
    def __init__(self,port,speed,maxCol,maxRow):
        self.maxCol = maxCol
        self.maxRow = maxRow
        
troubleTerminal = serialTerminal(6,9600,80,25) # this is line 61
The objective is, to create an object with the port and speed, and the lines and columns the terminal has.
I this example, port 6, speed 9600 baud, 80 columns and 25 lines. The last two values shall be stored in the serialTerminal object.

However, what happens, is this:
Traceback (most recent call last):
File "main.py", line 61, in <module>
ValueError: unsupported combination of bits and parity
MicroPython v1.9.2 on 2017-08-23; PYBv1.1 with STM32F405RG
I have read the documentation about inheritance in Python, also about usage of super and __init__. And i also invested hours in searching Google, but so far I don't understand, why the system obviously passes on the maxCol and maxRow parameters to the base class.

What am I overlooking here? Thanks for your help.

Re: UART inheritance mystery

Posted: Thu Nov 29, 2018 5:06 pm
by dhylands
Currently, trying to override builtin C based classes in python is problematic.

The best way is to make the UART object be a member rather than a parent class and then just forward to that.

You could also use this method:
https://github.com/micropython/micropyt ... -442371744

to make your own class have all of the same methods as the UART class.

Re: UART inheritance mystery

Posted: Sun Dec 02, 2018 12:22 am
by Hanilein
Thanks, Dave.

I stick for convenience to my Two-Parameter solution and set the columns and rows using properties.
That works (for now), but I'll keep an eye on the derived class...