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.
Get data through USB_VCP
Re: Get data through USB_VCP
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:
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
The correct argument list for the read() method should be:
- buffer which is filled "in place"
- maximum number of bytes to be read during the call
- error code may indicate EAGAIN in case nothing got read and you should try to read again later
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
Re: Get data through USB_VCP
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?
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?
Re: Get data through USB_VCP
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:
What does yours show?
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
Re: Get data through USB_VCP
You were rigth Davi,
I was using one old firmware
After I upgrade to the latest version the USB_VCP class it's working properly
Thanks for your help
I was using one old firmware
Code: Select all
Micro Python v1.0 on 2014-05-03;
Code: Select all
Micro Python v1.3.8-41-g6fd4b36 on 2015-01-03;