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

    Send data serially from LoPy to PC

    Scheduled Pinned Locked Moved LoPy
    8 Posts 3 Posters 554 Views 3 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.
    • S Offline
      sarthak04
      last edited by

      Hello everyone!
      I'm using a LoPy with an expansion board that acts as a central node, receiving data from other LoPys over LoRa. The central LoPy is connected to a PC serially using a MicroUSB. I wanted some guidance on how to save the received data in a csv file on the PC (Not on LoPy flash, or on any SD card).
      Your help would be highly appreciated.
      Thank you!
      Sarthak

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

        @sarthak04 By using the print() or sys.stdout.write() statement, you can send data from the LoPy by the serial interface of the expansion board. The data format will be defined by the script sending it. The default baud rate is 115200 baud. You can change it with uart.init(), if needed. You will need a small program on the PC which receives the data and stores it to a file. Attached is a sample program which I made once for a similar purpose. It stops when receiving an empty line, but that can be changed.

        import sys
        import serial
        from time import sleep
        
        
        def main():
            if len(sys.argv) > 1:
                port = sys.argv[1]
                if len(sys.argv) > 2:
                    filename = sys.argv[2]
                    try:
                        f = open(filename, "w")
                    except:
                        print("Could not open file:", filename)
                        return
                else:
                    f = None
        # open file
                try:
                    ser = serial.Serial(port, 115200)
                    sleep(3)
                    ser.reset_input_buffer()
                except Exception as err:
                    print("Could not connect: {!r}".format(err))
                    return
                print("Data connection at:", port)
                while True:
                    try:
                        line = ser.readline().decode().rstrip("\r\n")
                        if line != '': # Still alive?
                            print(line)
                            if f is not None:
                                f.write(line + "\r\n")
                        else: # EOF
                            break
                    except Exception as err:
                        print("{!r}".format(err))
                        break
                    except KeyboardInterrupt:
                        print("Interrupted!")
                        break
                if f is not None:
                    f.close()
            else:
                print("Usage: usbrecv port [filename]")
        
        if __name__ == "__main__":
            main()
        
        1 Reply Last reply Reply Quote 1
        • S Offline
          sarthak04
          last edited by sarthak04

          Thanks a lot for the quick reply. For testing the above code on my PC, I have to send the data from lopy through serial interface. I looked up on the forum but couldn't find anything to do so. Sorry to bother but could you please help me out with how to do that too because I was wondering if I could do it with uart.write() function.
          Thanks in advance!

          M robert-hhR 2 Replies Last reply Reply Quote 0
          • M Offline
            Martinnn @sarthak04
            last edited by

            @sarthak04 See the quick usage example in the docs: https://docs.pycom.io/firmwareapi/pycom/machine/uart.html

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

              @sarthak04 You can do that with the uart.write() call. But since UART0 is also used by REPL, it is more consistent to use print() or sys.stdout.write() in that case. The use of uarte.write() is not possible in other ports.

              1 Reply Last reply Reply Quote 1
              • S Offline
                sarthak04
                last edited by

                Oh I didn't know that. So if I have to go with the print() funciton or sys.stdout.write, do we just add any one of this function in the code? And give the data I want to store as parameter?

                Thanks for the help!

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

                  @sarthak04 Sorry, I do not understand your discomfort.
                  For the task you described you must have written some non-trivial code. At a certain point of that you receive a message from a LoRa device and now you want so send that content to the PC. Just to add a statement like print(msg) is the easy part. But you might want to format that in a specific way, and that is to be done by your code. If you do not know yet how to do that, you have to learn it. Learning is fun!

                  S 1 Reply Last reply Reply Quote 0
                  • S Offline
                    sarthak04 @robert-hh
                    last edited by

                    @robert-hh yes the data that I receive is a combination of multiple sensor data, and I am sending it as a struct. So I guess the same would work here too.
                    Thanks

                    1 Reply Last reply Reply Quote 0

                    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