Page 1 of 1
usb-cdc : how to know when connexion is effective ?
Posted: Tue Aug 28, 2012 7:08 pm
by alex31
I want to know know if serial connection if effective with other side.
I can know that usb is connected via testing like that :
Code: Select all
SDU1.config->usbp->state == USB_ACTIVE
but if a want to know if i have a terminal connected on the serial over usb link, so that the bytes i output will be received on
the other side.
Thanks
Alexandre
Re: usb-cdc : how to know when connexion is effective ?
Posted: Tue Aug 28, 2012 8:04 pm
by Giovanni
Hi,
The host sends CDC messages that you can detect in the messages callback. For example, most communication programs send the message to setup the communication speed after opening the port.
Giovanni
Re: usb-cdc : how to know when connexion is effective ?
Posted: Wed Aug 29, 2012 10:34 am
by alex31
Hi,
unfortunately, le terminal i use don't send anything at startup, but i will try to see if
i can configure it to do so.
well, if i hit keyboard to send a char,
i can detect the first char via
the first char is lost, but then i know i am connected.
i have tested to do
Code: Select all
while (sdGetWouldBlock(&SDU1)) {chThdSleepMilliseconds(100);}
but sdGetWouldBlock always return TRUE, sdGetWouldBlock could not be used with usb_cdc ?
I have also noticed that channel abstraction function for blocking test which was chIOPutWouldBlock()
and chIOGetWouldBlock() in v2.4.x does not exists anymore in 2.5.0, even if there are still referenced
in the commentary section of os/hal/include/serial.h.
If it's possible, the best for user of chibios would be to know the connection status without the need to receive a byte first.
Alexandre
Re: usb-cdc : how to know when connexion is effective ?
Posted: Wed Aug 29, 2012 10:54 am
by Giovanni
The serial driver also generates an even on incoming data and sets the flag CHN_INPUT_AVAILABLE, you can detect the event without having to consume data from the queue.
sdGetWouldBlock() cannot be used, note the prefix is the one of the normal serial driver, it is not part of the standard interface.
Giovanni
Re: usb-cdc : how to know when connexion is effective ?
Posted: Wed Aug 29, 2012 1:14 pm
by alex31
The serial driver also generates an even on incoming data and sets the flag CHN_INPUT_AVAILABLE
Testing the flag works only if somewhere a read is done, until a read is made, flag is not set ... :
Code: Select all
while (!(((SerialUSBDriver *)(USBD2.param))->flags & CHN_INPUT_AVAILABLE)) {
chThdSleepMilliseconds(100);
//chSequentialStreamGet(&SDU1);
}
works if chSequentialStreamGet is uncommented, never go out the loop otherwise. (and, by the way, no event is emitted)
Alexandre
Re: usb-cdc : how to know when connexion is effective ?
Posted: Wed Aug 29, 2012 1:41 pm
by Giovanni
Accessing fields is not correct, functionalities have function or macros, for example flags are accessed using chnGetAndClearFlags().
alex31 wrote:Testing the flag works only if somewhere a read is done, until a read is made, flag is not set
This is probably because an USB read transaction is not started until a read is performed at least once, I will look into this. You are not getting the event for the same reason, the event is generated when the flag is set.
You should be able to workaround this by calling:
Code: Select all
chnGetTimeout(&SDU1, TIME_IMMEDIATE);
this will "trigger" the first transaction and will not wait inside.
Giovanni
Re: usb-cdc : how to know when connexion is effective ?
Posted: Wed Aug 29, 2012 4:21 pm
by alex31
Accessing fields is not correct, functionalities have function or macros, for example flags are accessed using chnGetAndClearFlags().
I had seen that, but since it also clear the flag, i didn't want to modify it, but following your advice i change to use the api and it works.
You should be able to workaround this by calling:
Code: Select all
chnGetTimeout(&SDU1, TIME_IMMEDIATE);
thanks, it works perfectly !
Alexandre