Send data serially from LoPy to PC



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



  • @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!



  • 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!



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





  • 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!



  • @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()
    

Log in to reply
 

Pycom on Twitter