usb_cdc

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gjk_baker
 
Posts: 55
Joined: Mon Feb 01, 2021 5:19 pm

usb_cdc

Post by gjk_baker »

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

Code: Select all

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

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: usb_cdc

Post by adafruit_support_carter »

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?

User avatar
gjk_baker
 
Posts: 55
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

Post by gjk_baker »

Thanks for getting back to me.

Maybe it is my ignorance of Python but

Code: Select all

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.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: usb_cdc

Post by adafruit_support_carter »

The syntax would be something like this:

Code: Select all

>>> import usb_cdc
>>> usb_cdc.console.timeout = 1
>>> usb_cdc.console.timeout
1.0
>>>

User avatar
gjk_baker
 
Posts: 55
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

Post by gjk_baker »

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

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

User avatar
tannewt
 
Posts: 3298
Joined: Thu Oct 06, 2016 8:48 pm

Re: usb_cdc

Post by tannewt »

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

User avatar
gjk_baker
 
Posts: 55
Joined: Mon Feb 01, 2021 5:19 pm

Re: usb_cdc

Post by gjk_baker »

Okay, now I understand the documentation. Thanks

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: usb_cdc

Post by adafruit_support_carter »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”