Skip to main content
Explorer II
October 31, 2022
Solved

Problem with USB in virtual port com

  • October 31, 2022
  • 2 replies
  • 964 views

Hi Everybody,

I've got a few problems with my USB. I have configured the USB port of my STM32L476 in communication device class (virtual port com).

I can send and receive data on my terminal.

What I'm doing is the following :

sprintf(buf,BYTE_TO_BINARY_PATTERN,BYTE_TO_BINARY(micro_switch));
	strcpy(usb_buf,"Etats des microswitchs : ");
	strcat(usb_buf,buf);
	strcat(usb_buf,"\r\n\0");
	CDC_Transmit_FS(usb_buf,strlen(usb_buf));
 
	/* Valeur du 0/10V channel 1 */
	strcpy(usb_buf,"0-10V - channel 1 : ");
	gcvt(calcul_tension(tab_val_ana_moy[0]),4,buf);
	strcat(usb_buf,buf);
	strcat(usb_buf," Volts \r\n\0");
	CDC_Transmit_FS(usb_buf,strlen(usb_buf));
 
	/* Valeur du 0/10V channel 2 */
	strcpy(usb_buf,"0-10V - channel 2 : ");
	gcvt(calcul_tension(tab_val_ana_moy[1]),4,buf);
	strcat(usb_buf,buf);
	strcat(usb_buf," Volts \r\n\0");
	CDC_Transmit_FS(usb_buf,strlen(usb_buf));
 
	/* Nombre d'impulsions du compteur d'énergie */
	sprintf(buf,"%d",comptage_impulsion);
	strcpy(usb_buf,"nombre d'impulsions du compteur d'énergie : ");
	strcat(usb_buf,buf);
	CDC_Transmit_FS(usb_buf,strlen(usb_buf));

So, the terminal is supposed to display four lines of data, but only two (sometimes three) are displayed.

Is there a problem of size of buffer or maybe of timing ?

If you have somes ideas, I'll be very happy.

Thank you.

    This topic has been closed for replies.
    Best answer by Pavel A.

    Hi,

    Before calling CDC_Transmit, you need to check that the previous transmit is gone. This was discussed here many times.

    The CDC driver informs you about TX completion via TransmitCplt callback:

    https://github.com/STMicroelectronics/stm32_mw_usb_device/blob/2022e75b01a499b17acd17d28691b1ed5bbef2dc/Class/CDC/Src/usbd_cdc_if_template.c#L227

    The speed (bytes/sec) of TX is managed by the host. Normally the host is a powerful computer, and it won't cause unexpected delays, but things happen. Device cannot "push" data to host unless the host is ready to receive.

    Device should be prepared to situation when the host does not receive, and behave reasonably.

    2 replies

    fab04Author
    Explorer II
    October 31, 2022

    I've found a solution by adding HAL_Delay(10) after each CDC_Transmit_FS.

    Pavel A.Answer
    Super User
    October 31, 2022

    Hi,

    Before calling CDC_Transmit, you need to check that the previous transmit is gone. This was discussed here many times.

    The CDC driver informs you about TX completion via TransmitCplt callback:

    https://github.com/STMicroelectronics/stm32_mw_usb_device/blob/2022e75b01a499b17acd17d28691b1ed5bbef2dc/Class/CDC/Src/usbd_cdc_if_template.c#L227

    The speed (bytes/sec) of TX is managed by the host. Normally the host is a powerful computer, and it won't cause unexpected delays, but things happen. Device cannot "push" data to host unless the host is ready to receive.

    Device should be prepared to situation when the host does not receive, and behave reasonably.

    fab04Author
    Explorer II
    October 31, 2022

    Hi,

    Thank you for your explanation, I've modified my code to take care of this point.