Get data through USB_VCP

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
bittware
Posts: 45
Joined: Mon Aug 18, 2014 3:27 am

Get data through USB_VCP

Post by bittware » Fri Jan 02, 2015 3:06 am

Hello,
My software on PC sends out switch status to pyboard from time to time based on button click operation on PC GUI.
The status data is with fixed length, say 8 bytes, reflecting all switches status.
I am hoping to use USB_VCP to get these data by polling in 100ms interval.
One thing I'm not sure of is what if the PC just sends out 4 bytes and at the same time pyboard using usb_vcp.recv(8) wants a complete set of 8 bytes? Would the data misalignment occur?
By the way, what's the difference between usb_vcp.recv(8) and usb_vcp.read(8)?

Thanks in advance.

User avatar
kfricke
Posts: 342
Joined: Mon May 05, 2014 9:13 am
Location: Germany

Re: Get data through USB_VCP

Post by kfricke » Fri Jan 02, 2015 10:53 am

If i read the code correctly the read() method is non-blocking (dunno if this really differs from the recv() method!?). It does not allocate any memory on the heap and should be usable within an interrupt handling mechanism. The return value always is the number of bytes read. This is a different behavior from the documentation of the recv() method.

The correct argument list for the read() method should be:
  1. buffer which is filled "in place"
  2. maximum number of bytes to be read during the call
  3. error code may indicate EAGAIN in case nothing got read and you should try to read again later
The documentation has open bits and pieces and may be best read from the C code directly...

C code for the read() method:
https://github.com/micropython/micropyt ... usb.c#L278

And for the recv() method:
https://github.com/micropython/micropyt ... usb.c#L230

aerro
Posts: 2
Joined: Fri Jan 02, 2015 11:46 am

Re: Get data through USB_VCP

Post by aerro » Fri Jan 02, 2015 12:03 pm

Taking advantage of the topic I'm trying to get data through USB_VCP using this code...

from pyb import USB_VCP
serial = USB_VCP()

while True:
data = serial.recv(1)
ch = data[0]
print("Rcvd: 0x%x '%c'" % (data[0], ch))

Reading the serial the pyboard give this error:
ImportError: cannot import name USB_VCP

Somebody else found this error using USB_VCP class?
How can I fix this?

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

Re: Get data through USB_VCP

Post by dhylands » Fri Jan 02, 2015 11:40 pm

I tried your code and it worked for me.

So I'm going to guess that your firmware is perhaps too old? When you power up the board (ore press Control-D) it should display some version info like:

Code: Select all

Micro Python v1.3.8-9-g8a2cc1c on 2014-12-29; F4DISC with STM32F407
What does yours show?

aerro
Posts: 2
Joined: Fri Jan 02, 2015 11:46 am

Re: Get data through USB_VCP

Post by aerro » Sat Jan 03, 2015 3:03 pm

You were rigth Davi,

I was using one old firmware

Code: Select all

Micro Python v1.0 on 2014-05-03;
After I upgrade to the latest version the USB_VCP class it's working properly

Code: Select all

Micro Python v1.3.8-41-g6fd4b36 on 2015-01-03;
Thanks for your help

Post Reply