How to handle data transfer from pyboard to PC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to handle data transfer from pyboard to PC

Post by pythoncoder » Fri Nov 13, 2015 11:06 am

It turns out that my boot.py had all lines commented out. With the following it works over 1K iterations.

Code: Select all

import pyb
pyb.usb_mode('CDC')
The distro on this laptop is Mint, which auto-mounts drives. So the SD card was mounted, but the mountpoint was unused.

So many problems seem be down to this issue.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to handle data transfer from pyboard to PC

Post by pythoncoder » Fri Nov 13, 2015 11:41 am

@Chemist You might be interested in a generalisation of this approach, which allows the program running on the Pyboard to return an arbitrary Python object which is reconstituted on the PC. This uses code from the pickle module in the Micropython library.
pb_get_object.py:

Code: Select all

def get_data():
    data= [i for i in range(10)]
    data.append({1:'rats', 2:'aardvark'}) # just to show we can
    return data
print(repr(get_data()))
print_object.py:

Code: Select all

import pyboard

def execfile(filename, device):
    pyb = pyboard.Pyboard(device)
    pyb.enter_raw_repl()
    output = pyb.execfile(filename)
    pyb.exit_raw_repl()
    pyb.close()
    return output

def loads(s):
    d = {}
    exec("v=" + s, d)
    return d["v"]

def getobject(filename, device='/dev/ttyACM0'):
    b = execfile(filename, device)
    s = b.decode('utf-8')
    return loads(s)

print(getobject('pb_get_object.py'))
Outcome:

Code: Select all

[adminpete@axolotl]: ~
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import print_object
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, {1: 'rats', 2: 'aardvark'}]
>>> 
The ease of object serialisation in Python is one of its more endearing features :)
Peter Hinch
Index to my micropython libraries.

Chemist
Posts: 20
Joined: Wed Mar 04, 2015 5:25 pm

Re: How to handle data transfer from pyboard to PC

Post by Chemist » Sun Nov 15, 2015 8:03 pm

I also sometimes get this error:

Code: Select all

OSError: [Errno 16] Device or resource busy: '/dev/pyboard'
I don't know why I get it, maybe as mentioned dhylands, this happens because I access the pyboard from two terminals for plotting data via matplotlib and bokeh(my custom web interface). In most cases it works well - ran more that 600 times with my sensor without any problems. I also uncommented this line: pyb.usb_mode('CDC') in the boot.py and will monitor its behaviour.
@Pythoncoder, I really like your generalization approach, it makes the data transfer much easier to handle.

Thanks,
Vitali

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to handle data transfer from pyboard to PC

Post by pythoncoder » Mon Nov 16, 2015 10:38 am

The "Device or resource busy" message is a nuisance: in my experience it goes away if you wait a while. The device seems to start up in a busy state after the Pyboard is plugged in. The time for it to go away seems dependant on the USB port on the host machine. On my desktop boxes the device is usable after about 2 seconds. On my (fast) laptop it takes ten seconds before I can get a REPL.

It does seem an inordinately long time given that the Pyboard boots in a fraction of a second.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: How to handle data transfer from pyboard to PC

Post by dhylands » Mon Nov 16, 2015 5:48 pm

Opening the same serial port in 2 different programs at the same time is basically asking for trouble.

Both processes will have reads pending, but a given byte of input will only go to one process or the other, and never both. Which process gets the data is "essentially random".

There is a locking protocol which is used by all of the serial terminal programs, and its on my todo list to add that to pyboard.py (at least for linux).

tcnaks5
Posts: 6
Joined: Tue Aug 18, 2020 2:18 pm

Re: How to handle data transfer from pyboard to PC

Post by tcnaks5 » Tue Aug 25, 2020 1:30 pm

dhylands wrote:
Wed Nov 11, 2015 4:01 pm

Code: Select all

import pyboard

def execfile(filename, device='/dev/ttyACM0'):
    pyb = pyboard.Pyboard(device)
    pyb.enter_raw_repl()
    output = pyb.execfile(filename)
    pyb.exit_raw_repl()
    pyb.close()
    return output
I was able to get this working but I was wondering if there was a way to give data to the file youre running in the REPL? I have a list I want to give to the script that I want to run on the device, but it doesnt seem that I can just add it as another paramter.

Thanks for the help

Post Reply