Skip to main content
Visitor II
November 16, 2021
Solved

USB CDC receving more than one char

  • November 16, 2021
  • 2 replies
  • 2374 views

Hello,

I wonder, is it possible to receive more than one char (1 byte) at the time? For now I can only receive one in one message, i can't find the solution to receive more.

Here is my code:

/**usbd_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);
 
	extern uint8_t received_buffor[64];
	extern uint8_t received_flag;
 
	for(int i = 0; i < 64; i++){
		received_buffor[i] = 0;
	}
 
	strlcpy(received_buffor, Buf, (*Len) + 1); 
 
	received_flag = 1;
 
	return (USBD_OK);
 /* USER CODE END 6 */
}
 
/**main.c**/
uint8_t received_buffor[64];
uint8_t send_buffor[64];
uint8_t received_flag = 0;
uint8_t len;
 
int main(void)
{
while (1)
 {
	 if(received_flag == 1){
		 received_flag = 0;
 
		 len = sprintf(send_buffor, "Received: %s\n\r", received_buffor);
		 CDC_Transmit_FS(send_buffor, len);
 }
}

Thanks in advance :)

    This topic has been closed for replies.
    Best answer by bmak

    Yep, it was terminals problem....

    I used Terminal, Putty and Atollic's debugger terminal - all of them is sending 1 byte.

    I have written terminal on pythons pyqt5 and it works!

    2 replies

    Graduate II
    November 16, 2021

    Rx part:

    /**usbd_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);
     
    	extern uint8_t received_buffor[64];
    	extern uint8_t received_flag;
     
    	for(int i = 0; i < 64; i++){
    		received_buffor[i] = 0;
    	}
     //this copies Len bytes received_buffor into Buf
    	memcpy(Buf,&received_buffor,*Len);
     
    	received_flag = 1;
     
    	return (USBD_OK);
     /* USER CODE END 6 */
    }

    TX part:

    Just write the total bytelength you want to send

    CDC_Transmit_FS(send_buffor, sizeof(send_buffor));

    Warning!: CDC_Transmit_FS will give the order to transmitt your buffer but is not going to block your process.

    So if you dont place a sufficient delay after CDC_Transmit_FS you will have overlapping buffers issues.

    CDC_Transmit_FS(send_buffor, sizeof(send_buffor));
    HAL_Delay(sufficient_time)

    its ugly but i didnt fing a better solution

    its spelled buffer not buffor. (Sassy Hermione voice)

    bmakAuthor
    Visitor II
    November 16, 2021

    Hi, thanks for your answer! Whats sad about it, is that it doesn't work for me :\ Tried different delays (from 10, to 1000 ms). On terminal it looks like maybe its working, but i can't see received data.

    0693W00000GXjcoQAD.png

    bmakAuthor
    Visitor II
    November 16, 2021

    Also, received_buffor doesn't contain the data I've sent, because these conditions don't work (they worked before):

    0693W00000GXjdwQAD.png

    Super User
    November 16, 2021

    How do you know more than 1 byte at a time is being sent? CDC_Receive_FS will receive the entire packet, up to 64 bytes. It's up to the sending program to put more than one byte per packet.

    Variables used in both ISRs and the main loop should be marked volatile.

    bmakAuthorAnswer
    Visitor II
    November 16, 2021

    Yep, it was terminals problem....

    I used Terminal, Putty and Atollic's debugger terminal - all of them is sending 1 byte.

    I have written terminal on pythons pyqt5 and it works!

    Super User
    November 16, 2021

    Try to send a file (xmodem, ymodem, etc) from Teraterm or other such app. Then you will see multi-byte receives.