2016-01-05 11:55 AM
I am using the STM32F072B-Discovery board and used the CDC_Standalone example to communicate with the PC using a Virtual Serial Port. I am using the VCP driver from the website.
I can send data from the microcontroller to the hyperterminal/putty running on the PC using these commands. I can see the data on the console and it works perfectly.
memcpy(UserBuffer, ''123'', sizeof(char) * 3); USBD_CDC_SetTxBuffer(&USBD_Device, UserBuffer, 3); USBD_CDC_TransmitPacket(&USBD_Device)Unfortunately, I am still not clear on how to read data send from hyperterminal/putty to the microcontroller.
I know that I am receiving the data.In the function CDC_Itf_Receive(), I added toggles to the LEDs and I can see the LED's toggling as I type in the putty console.
static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len)
{ if (Buf[0] == '1') BSP_LED_Toggle(LED_GREEN);if (*Len == 1)
BSP_LED_Toggle(LED_ORANGE);HAL_UART_Transmit_DMA(&UartHandle, Buf, *Len);
return (USBD_OK); }However, I am still unclear on how to read the data in the main() subroutine.
I know that Buf and *Len contain the information that I need in CDC_Itf_Receive() function. Should I copy those values to a global buffer and then use those values in main()What I basically want is to monitor for any data from putty in main()
main()
...
if (LengthOfDataReceived != 0)
{ if (!strcmp(BufferReceived, ''ABCD'')) DoABCD(); else DoEFGH(); } } #vcp-read-receive2016-01-05 12:20 PM
I think you'll have to create your own FIFO/RING buffer, and extract and align that data in the main() loop.
2016-01-06 08:55 AM
Thanks.
Is there any documentation on when CDC_Itf_Receive() is called. When isHAL_UART_TxCpltCallback() called?
The one section in a README file that I found is: '' - 1 x Bulk OUT endpoint for transmitting data from PC host to STM32 device: When data are received through this endpoint they are saved in the buffer ''UserRxBuffer'' then they are transmitted over UART using interrupt mode and in meanwhile the OUT endpoint is NAKed. Once the transmission is over, the OUT endpoint is prepared to receive next packet in HAL_UART_TxCpltCallback().'' But this is still unclear to me.2016-01-06 11:12 AM
I found documentation here:
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00108129.pdf
I solved the issue for my particular needs by adding the following code. I didn't need to do extensive serial communication, so this code works for me so far.static
int8_t
CDC_Itf_Receive(
uint8_t
* Buf,
uint32_t
*Len){
uint32_t
i;for
(i = 0; i < *Len; i++) { TempUserBuffer[BuffLength] = Buf[i]; BuffLength++; }HAL_UART_Transmit_DMA(&UartHandle, Buf, *Len);
return
(
USBD_OK
); }void
HAL_UART_TxCpltCallback(
UART_HandleTypeDef
*huart){ // wait until a valid command is received by checking if the UserBuffer contains '\r'
if
(TempUserBuffer[BuffLength-1] ==
'\r'
) { BSP_LED_Toggle(LED_ORANGE
);memcpy
(UserBuffer, TempUserBuffer, BuffLength); RxBuffLength = BuffLength; BuffLength = 0; }/* Initiate next USB packet transfer once UART completes transfer (transmitting data over
Tx
line) */ USBD_CDC_ReceivePacket(&USBD_Device); }2016-01-06 12:10 PM
Remember that string functions expect NUL terminated data