Arm Mbed OS support forum

USB CDC receive example

Hellow. I am learning cpp and mbed recently. please help to write 6 uint numbers from usb cdc. I read the documentation but found in it just an example of sending. I apologize for not showing the code. I don’t even know where to start

Hello Nikita,

You can try the following:

#include "mbed.h"
#include "USBCDC.h"

USBCDC          cdc;
unsigned int    data[6] = { };              // data array
uint8_t*        p_data = (uint8_t*)data;    // write pointer
uint32_t        size_read;                  // the number of bytes actually received

int main(void)
    while (1) {
        cdc.receive_nb(p_data, 1, &size_read);          // write received byte to data array
        p_data += size_read;                            // advance the write pointer by the number of bytes actually received
        if (p_data == (uint8_t*)data + sizeof(data)) {  // when array is full
            printf("Data received:\r\n");
            for (uint8_t i = 0; i < 6; i++) {
                printf("data[%d] = %d\r\n", i, data[i]);
            p_data = (uint8_t*)data;                    // reset the write pointer