Serial over USB hangs if nobody is listening at the PC side

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Serial over USB hangs if nobody is listening at the PC side

Post by noether »

Probably this is a common issue and not only related with the USB CDC.

I have one Thread which sends every second two chars (one integer and one blank) with chprintf

Code: Select all

    while (TRUE) {
        time += MS2ST(1000);
        u++;
        chprintf(stream_usb, "%d ", u);
        palTogglePad(GPIOB, GPIOB_LED_BLUE);
        chThdSleepUntil(time);
    }


If I have running some program monitoring the ttyACM0 at the pc side, then the firmware runs nice.
If nobody is listening to the ttyACM0, then after some transmissions, the firmware stops at chprintf.

If after this, I monitor the serial port at the pc side. Then I read all the bytes sent by the micro at once,

Code: Select all

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 


and one extra iteration occurs (the LED toggles), and it stops again.

Probably is related with some buffer which is full? Is this an expected behavior?

I have read this in the documentation about employing a streamer.
"The function writes data from a buffer to a channel. If the channel is not ready to accept data then the calling thread is suspended."

But after receiving the data at the PC side, it should keep sending right? and not hanging after one extra iteration.

Thanks in advance.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Serial over USB hangs if nobody is listening at the PC s

Post by Giovanni »

Hi,

This is the expected behavior and like you mentioned it is related to the transmission buffer not being emptied by the host.

You may try to write using a timeout, that way the timeout would tell you the data is being not pulled by the host, you may also specify TIME_IMMEDIATE and don't wait at all, it writes as much data the buffer can take (even zero) and return immediately.

Giovanni
Post Reply