Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    UART communication between 2 Lopy4's

    Scheduled Pinned Locked Moved Discussion
    6 Posts 2 Posters 279 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      Alephus
      last edited by Alephus

      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

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @Alephus
        last edited by robert-hh

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

        1 Reply Last reply Reply Quote 1
        • A Offline
          Alephus
          last edited by

          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)

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @Alephus
            last edited by

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

            A 1 Reply Last reply Reply Quote 2
            • A Offline
              Alephus @robert-hh
              last edited by

              @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

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @Alephus
                last edited by robert-hh

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

                1 Reply Last reply Reply Quote 2

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom