Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usb_cdc.serials[1].connected always return False for Raspberry Pi Pico #4345

Closed
ismael-miguel opened this issue Mar 6, 2021 · 7 comments · Fixed by #4359
Closed

usb_cdc.serials[1].connected always return False for Raspberry Pi Pico #4345

ismael-miguel opened this issue Mar 6, 2021 · 7 comments · Fixed by #4359
Milestone

Comments

@ismael-miguel
Copy link

Firmware

Adafruit CircuitPython 6.2.0-beta.3 on 2021-03-04; Raspberry Pi Pico with rp2040

Code/REPL

import usb_cdc
usb_cdc.serials[1].connected

Behavior

Running the following code ALWAYS returns False, even when reading data to/from the Raspberry Pi Pico, on that USB CDC channel.

>>> import usb_cdc
>>> usb_cdc.serials[1].connected
False

Additional Info

Despite returning False, the link is working properly.

I can send and receive data, without any issues.


According to this: https://circuitpython.readthedocs.io/en/latest/shared-bindings/usb_cdc/index.html

True if this Serial is connected to a host. (read-only)

Since it is connected to a host, should return True.

And no, the Raspberry Pi Pico is the only device connected to serial, and both ports belong to it:
image

@dhalbert
Copy link
Collaborator

dhalbert commented Mar 6, 2021

I do not see this problem with beta.3, either on Windows or Linux. I do have to be connected to serials[1] before it turns to True. In the screenshot below, I at first did not have a connection to COM18, but then when I did connect via Tera Term, you can see it changed from False to True.

.connected means that the COM port has been opened, not just that is is present.

image

@ismael-miguel
Copy link
Author

Interesting ...

When I use C# to connect to it, it returns False, but I can read and send data without issues:
image

But when I use PuTTY, it return True:
image

@dhalbert
Copy link
Collaborator

dhalbert commented Mar 6, 2021

I wonder if the C# code you are using is closing the connection after each write or something like that. If you do a read with a timeout in C# so it hangs while you check, does it show as connected during that time?

@ismael-miguel
Copy link
Author

The C# code uses the SerialPort.DataReceived event, and waits for a text line.
(https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.datareceived?view=netframework-4.7.2)

It opens the connection and waits.
There's nothing written to close the connection.


I've tried to stop on the serial.ReadLine(); method, in C#:
image

But it still returns False:
image

@hathach
Copy link
Member

hathach commented Mar 7, 2021

You need to set line state DTR to 1, it is used by tinyusb to detect terminal connection

@dhalbert
Copy link
Collaborator

dhalbert commented Mar 7, 2021

Thanks @hathach. Some websearching shows this is a convention, and that terminal programs typically assert it when opening the conenction. So does pyserial, by default. This does not mean that DTR/DSR-style flow control is enabled. There is no other easy way to determine that something is connected on the host end. I can add more docmentation to .connected to make this clear.

Some more websearching shows that other people have had this problem with C#, which defaults to DTR not being set.

@ismael-miguel
Copy link
Author

Thank you @hathach and @dhalbert.

I can confirm that setting serial.DtrEnable = true; in C# solved the issue:
image

Would be amazing if this was documented, to avoid possible duplicated issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants