0

usb_cdc
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

usb_cdc

by gjk_baker on Sat Jul 24, 2021 6:51 pm

Just trying to put a time out on the USB input.
Don't understand why I am getting these errors.

Code: Select all | TOGGLE FULL SIZE
import usb_cdc
if (usb_cdc.Serial.connected):       # This works okay
    print ("True")
print ("Bytes - ", usb_cdc.Serial.in_waiting)
#Bytes -  <property>                        Don't understand why it is a property when it should be an <int>???
usb_cdc.Serial.timeout:3              # I want the function to move on after 3 seconds if no input

#A = usb_cdc.Serial.read()
#TypeError: function missing 1 required positional arguments

#A = usb_cdc.Serial.read(1)
#TypeError: argument has wrong type

gjk_baker
 
Posts: 40
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

by adafruit_support_carter on Mon Jul 26, 2021 1:45 pm

You're calling class level function vs. instance. See here from some general info:
https://learn.adafruit.com/customizing- ... 3096590-12
Are you wanting to change the timeout for the console or data usb_cdc.Serial instance?

adafruit_support_carter
 
Posts: 21122
Joined: Tue Nov 29, 2016 2:45 pm

Re: usb_cdc

by gjk_baker on Mon Jul 26, 2021 2:38 pm

Thanks for getting back to me.

Maybe it is my ignorance of Python but

Code: Select all | TOGGLE FULL SIZE
import usb_cdc
serial = usb_cdc.serials

  File "code.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'serials'


In conjunction with usb_cdc.read and usb_cdc.timeout, I am trying to limit the time to waiting for input on the console.
I haven't been able to find any sample code using these functions.

gjk_baker
 
Posts: 40
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

by adafruit_support_carter on Mon Jul 26, 2021 5:26 pm

The syntax would be something like this:
Code: Select all | TOGGLE FULL SIZE
>>> import usb_cdc
>>> usb_cdc.console.timeout = 1
>>> usb_cdc.console.timeout
1.0
>>>

adafruit_support_carter
 
Posts: 21122
Joined: Tue Nov 29, 2016 2:45 pm

Re: usb_cdc

by gjk_baker on Tue Jul 27, 2021 3:04 pm

Thanks, but that is not where I am getting the error
Once I set the timeout, I need to read from the usb.
So, "usb_cdc.Serial.read(1)"
generates the following error

Code: Select all | TOGGLE FULL SIZE
import usb_cdc

usb_cdc.console.timeout = 1
A = usb_cdc.Serial.read(1)

print (A)


File "code.py", line 5, in <module>
TypeError: argument has wrong type

gjk_baker
 
Posts: 40
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

by tannewt on Tue Jul 27, 2021 3:13 pm

`usb_cdc.Serial` is a class, not an instance. The only thing you would normally do with it is `usb_cdc.Serial()`.

Instead, we have two instances already created for you `usb_cdc.console` and `usb_cdc.data`. Use those two instead of `usb_cdc.Serial`

tannewt
 
Posts: 2581
Joined: Thu Oct 06, 2016 8:48 pm

Re: usb_cdc

by gjk_baker on Tue Jul 27, 2021 4:32 pm

Okay, now I understand the documentation. Thanks

gjk_baker
 
Posts: 40
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

by adafruit_support_carter on Tue Jul 27, 2021 4:57 pm

Also keep in mind the data one is not enabled by default.
You can enable and disable the console device and the data device independently. The console device is enabled by default, but the data device is not.

adafruit_support_carter
 
Posts: 21122
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.