uart 1 and 2 L04 module

  • Good morning everybody,

    i am developing a python script for L04 module where i need to enable UART1 and UART2 at the same time.

    line 1: uart1 = UART(1, 115200) # init with given baudrate
    line2: uart1.init(115200, bits=8, parity=None, stop=1, pins=('P3', 'P4')) # init with given parameters

    line3: uart2 = UART(2, 115200) # init with given baudrate
    line4: uart2.init(115200, bits=8, parity=None, stop=1, pins=('P19', 'P20')) # init with given parameters

    I need to read data from UART1 and redirect it to UART2.
    Everything works if i do no enable uart2 (line 3 and 4) but unfortunately, when i enable the second uart, i do not receive anything from uart1 (i print the result in REPL).

    Do you have suggestions?

  • @Alessandro-Cortese That's how it is implemented. It is not the init call, it is the constructor. if you call:
    uart2 = UART(2, 115200) # init with given baudrate
    the default pins for UART 2 are used. These are P3 and P4, which are then deassigned from UART 1. Putting all settings into the constructor call avoids that pitfall, and it is shorter code.

  • @robert-hh it works!! Many thanks for your help :) what could be the reason of that behaviour about init calls?

  • @Alessandro-Cortese Do not use the init calls. put the arguments from the init call into the constructor. Line 3 will set up uart2 at the default pins, which are changed in line 4.

  • What exactly do you mean by "enable the second uart"?

    Reading from a serial port is usually a blocking operation unless you specified some timeout time before the read. Did you?

    Another possible way is to check if there is something to read like

    while true:
       if uart1.any():   msg1 = uart1.readline()
       if uart2.any():   msg2 = uart2.readline()

    That way, the readline() operations will never block the loop.

Log in to reply

Pycom on Twitter