Page 1 of 1

STM32F4 serial.Available()?

Posted: Wed Nov 04, 2020 8:30 pm
by tcv
Hello everyone



I have started very recently with ARMs, specifically with the STM32F407 VG DISCO

I am experimenting with the USART port and I have some doubts.

In the same way as in the arduino IDE for avr, I have always used serial.Available () to know the number of bytes that are going to arrive, I need to do the same for the STM32F4, but I have not found anything about it. All they suggest to me is to set the number of bytes to receive.

I am doing some tests with a machine to which I send from the STM32F4 discovery, a command of two bytes, for example (0x01, 0x11) and it responds with a frame of bytes of variable length. For some commands I know the length of bytes that are received, in those cases the reception is OK, but if I don't know how many bytes are going to arrive I can't get the answer.

I have read about using circular DMA for these cases, but this machine only responds once for every command sent, it is not a constant frame.

I appreciate any example, suggestion or advice to achieve my goal


Regards

Re: STM32F4 serial.Available()?

Posted: Wed Nov 04, 2020 11:53 pm
by mrburnette

Re: STM32F4 serial.Available()?

Posted: Thu Nov 05, 2020 10:05 am
by fpiSTM
Don't know which core you used but if it is the STM32 core, then Serial.available() works the same as Arduino.

Re: STM32F4 serial.Available()?

Posted: Thu Nov 05, 2020 3:12 pm
by tcv
I am working with CUBE IDE and HAL libraries

What I need is an example or idea to emulate the serial.Available () function that is used in the Arduino IDE

Re: STM32F4 serial.Available()?

Posted: Thu Nov 05, 2020 5:16 pm
by fpiSTM
The available is simply the check of how many bytes are available in the buffer:

Code: Select all

  return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _serial.rx_head - _serial.rx_tail)) % SERIAL_RX_BUFFER_SIZE;

Re: STM32F4 serial.Available()?

Posted: Fri Nov 06, 2020 2:09 pm
by tcv
This that you propose is what the function does in the Arduino library, the topic is how to translate this for handling with HAL libraries

Re: STM32F4 serial.Available()?

Posted: Fri Nov 06, 2020 2:48 pm
by fpiSTM
tcv wrote: Fri Nov 06, 2020 2:09 pm This that you propose is what the function does in the Arduino library, the topic is how to translate this for handling with HAL libraries
Yes, HardwareSerial used the HAL... Ok not with DMA.
Anyway for your code I guess you could implement something around the buffer also... but only a guess maybe I'm wrong.

Re: STM32F4 serial.Available()?

Posted: Fri Nov 06, 2020 3:09 pm
by mlundin
If you dont know the number of bytes in the response message, then I assume there is a specific code or byte sequence to signal the end of the message. Perhaps there is also a known maximum length of the message.

The Serial.available() will only tell you the current number of bytes that has arrived at that precise moment, not necssarily the same as the message length, serial transmission takes time. So you should read one byte at a time as long as Serial.available() > 0 and either save them in a buffer for later processing, or interpret them one by one, until the end code/codes arrive.