cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC receving more than one char

bmak
Senior

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 :)

1 ACCEPTED SOLUTION

Accepted Solutions

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!

View solution in original post

12 REPLIES 12
Javier1
Principal

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)

we dont need to firmware by ourselves, lets talk

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

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

0693W00000GXjdwQAD.png

This could be a pointer's problem. (youre mixing and twisting * with & with nothing at all)

Are you using the debugger?

what happens if you do this

len = sprintf(&send_buffor, "Received: %s\n\r", received_buffor);

we dont need to firmware by ourselves, lets talk

Where I'm mixing it? Right now I have the same code as yours.

When I do "this" nothing changes, it's the same.

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

If a function like sprintf /memcpy /strlcpy requires as an argument a pointer to a buffer (uint8_t*) you should use it with a pointer to a buffer.

 

sprintf(send_buffor,...

sprintf(&send_buffor,...

 

strlcpy(received_buffor.....

strlcpy(&received_buffor.....

 

 

we dont need to firmware by ourselves, lets talk

Thanks, didn't know that.

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!