UART communication between 2 Lopy4's



  • Good evening

    For the past few days ive been trying to understand how UART and I2C communication works since i will be using them to read sensor data from 2 sensors i have ( 1 uses UART, the other uses I2C)

    For testing purposes and to learn i decided to connect 2 Lopy4's toguether and try co comunicate betwen each other using UART however due to both lack of knoledge and experience ive had little success

    Here's what i've done so far:

    both lopys are connected in pin3 and pin4 in a cross-pattern since thats what i learned UART should be connected as

    in one Lopy4 (lets call it "LopySender") ive setup the following code:

    from machine import UART
    import pycom
    import utime
    
    pycom.heartbeat(False)
    uart=UART(1, 9600)
    uart.init(9600, bits=8, parity=None, stop=1, pins=('P3', 'P4'))
    
    while True:
        uart.write(b'hello')
        utime.sleep(5)
    

    so every 5 seconds it sends a mensage trough TX1/RX1 to the other Lopy4.

    The other Lopy4 (the "LopyReceiver") is connected to the PC so i can see the output and so far has the following code:

    from machine import UART
    import ubinascii
    import pycom
    import utime
    
    pycom.heartbeat(False)
    uart=UART(1, 9600)
    uart.init(9600, bits=8, parity=None, stop=1, pins=('P3','P4'))
    
    while True:
        if(uart.any()>0):
            buf=bytearray(20)
            uart.readinto(buf, 20)
            print(ubinascii.hexlify(buf))
            print(buf)
    

    i know im doing things wrong, however thats why im here. i wanna learn on how to properly use UART so i dont mess up when i change back to the sensors. How can i properly comunicate trough UART and what sould/shouldnt i do?

    Also, there are some questions i have about this way of comunication:

    -Lets assume i have 1 byte of data i wanna send. i do uart.write of it but on the other side, does it store in a buffer? is it time based? do i lose it if im not listening in time?

    thanks for reading this so far



  • @Alephus GND is the reference point for signal levels. Principally, if both devices are connected by USB cables to the same computer, there is a GND connection. That's why it worked to some extend. But its is a long connection though many connectors with a substantial impedance, which form a large loop. That large loop picks up a lot of noise. And that noise created the weird bytes. So always have a good GND connection.
    P.S.: You're not alone.



  • @robert-hh said in UART communication between 2 Lopy4's:

    @Alephus This inaccuracy is not normal, at least not at that low pace.. Did you also connect the GND pins of both units?

    no i did not. Was not aware it was required. all i had was the RX, TX pins connected bewteen both devices.

    now i connected and the output goes smootly:

    b'68656c6c6f'
    b'hello'
    ----------------------
    b'68656c6c6f'
    b'hello'
    ----------------------
    b'68656c6c6f'
    b'hello'
    ----------------------
    b'68656c6c6f'
    b'hello'
    ----------------------
    b'68656c6c6f'
    b'hello'
    

    interesting. Why does sharing GND remove the inacuracy? (im afraid my knowledge is limited)

    In any case, for anyone having the same problems:
    -Make sure RX/TX are crossed and not on the same as the usb serial for the PC
    -Make sure GND is shared

    thanks for the assistance @robert-hh



  • @Alephus This inaccuracy is not normal, at least not at that low pace.. Did you also connect the GND pins of both units?



  • thanks for the reply

    so any incomming information is stored until buffer is full or i use it correct? that simplifies things

    ive modified my test code according to your sugestion, removing the need of a bytearray and now im getting the following output (added a print('-----------') just to separate the values):

    b'68656c6c6f'
    b'hello'
    ----------------------
    b'68656c6cef'
    b'hell\xef'
    ----------------------
    b'68656c6d6f'
    b'helmo'
    ----------------------
    b'68656cec6f'
    b'hel\xeco'
    ----------------------
    b'68656c6c6f'
    b'hello'
    ----------------------
    b'6865b76cfb'
    b'he\xb7l\xfb'
    ----------------------
    b'7865feeeff'
    b'xe\xfe\xee\xff'
    ----------------------
    

    some of the reads are accurate however i dont uderstand is why the big ammount of inacuracy. Is it normal? is so how can i be sure the sensor will receive the commands i send or if the data i receive is accurate? (besides using a checkbyte)



  • @Alephus UART has by default an internal receive buffer of 512 byte, which stores the incoming data even if your script is not listening. If that size does not fit, you can change that with the option rx_buffer_size=nnn of the init() call. The receive script above could be simplified into:

    from machine import UART
    import ubinascii
    import pycom
    import utime
    
    pycom.heartbeat(False)
    uart=UART(1, 9600, bits=8, parity=None, stop=1, pins=('P3','P4'))
    
    while True:
        if(uart.any()>0):
            buf=uart.read()
            print(ubinascii.hexlify(buf))
            print(buf)
    

    Note: All parameters of the init() call can also be given in the constructor. But if you use init(), all settings of the constructor are overwritten, except the UART number. For options not specified the default values will be used.


Log in to reply
 

Pycom on Twitter