UART inheritance mystery

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Hanilein
Posts: 7
Joined: Thu Jul 12, 2018 11:40 am
Location: Christchurch, New Zealand

UART inheritance mystery

Post by Hanilein » Thu Nov 29, 2018 9:10 am

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.

User avatar
dhylands
Posts: 2905
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART inheritance mystery

Post by dhylands » Thu Nov 29, 2018 5:06 pm

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.

Hanilein
Posts: 7
Joined: Thu Jul 12, 2018 11:40 am
Location: Christchurch, New Zealand

Re: UART inheritance mystery

Post by Hanilein » Sun Dec 02, 2018 12:22 am

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...

Post Reply