cancel
Showing results for 
Search instead for 
Did you mean: 

USB receive when pressed enter

GunkutA
Senior

Hello, I am trying to establish a USB communication between PC and a STM32 MCU. I want to receive an integer from a PC. (range of the integer is between 0 and 20.000). I added those lines:

To usb_cdc_if.h :

__weak void CDC_ReceiveCallBack(uint8_t *buf, uint32_t len);

To the usb_cdc_if.c

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  CDC_ReceiveCallBack(Buf, Len);
  return (USBD_OK);
  /* USER CODE END 6 */
}
.
.
.
__weak void CDC_ReceiveCallBack(uint8_t *buf, uint32_t len)
{}

And in the main class:

void CDC_ReceiveCallBack(uint8_t *buf, uint32_t len)
{
 
fan_speed=atoi(buf);
	memset(statusString,0x00,255);
statusStringLength = sprintf(statusString,"Fan Speed: %d ", fan_speed);
	CDC_Transmit_FS(statusString,statusStringLength);
}

So I can only receive 1 digit numbers. Should I add an if statement to the beginning of function and when enter is pressed, then I should receive it from buffer?

1 REPLY 1
Vivek yadav1
Associate III

1> i shall suggest you to look into the file "usbd_cdc_if.c"

>> line no : 104

/* Create buffer for reception and transmission      */

/* It's up to user to redefine and/or remove those define */

/* Received Data over USB are stored in this buffer    */

uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];

/* Send Data over USB CDC are stored in this buffer    */

uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

These two buffers are by default is used for Data Receiving and Transmitting.

So no need to write your own code/Function.

2:>

As far as Concerning about ,hit "Enter key" and Get the data what you want.

"Enter Key" is part of Keyboard and Keyboard comes under HID class of USB. First Thing you should identify that

how will your micro controller know that you have pressed Enter key.

@Regards

@Vivek yadav​