2021-11-15 09:28 PM
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 :)
Solved! Go to Solution.
2021-11-16 06:05 AM
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!
2021-11-16 12:27 AM
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)
2021-11-16 04:05 AM
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.
2021-11-16 04:07 AM
Also, received_buffor doesn't contain the data I've sent, because these conditions don't work (they worked before):
2021-11-16 04:36 AM
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);
2021-11-16 05:02 AM
Where I'm mixing it? Right now I have the same code as yours.
When I do "this" nothing changes, it's the same.
2021-11-16 05:33 AM
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.
2021-11-16 05:58 AM
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.....
2021-11-16 06:04 AM
Thanks, didn't know that.
2021-11-16 06:05 AM
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!