STM32F4 serial.Available()?

All about boards manufactured by ST
Post Reply
tcv
Posts: 4
Joined: Sat Apr 04, 2020 5:50 pm
Location: VALENCIA - SPAIN

STM32F4 serial.Available()?

Post 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
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: STM32F4 serial.Available()?

Post by mrburnette »

User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F4 serial.Available()?

Post by fpiSTM »

Don't know which core you used but if it is the STM32 core, then Serial.available() works the same as Arduino.
tcv
Posts: 4
Joined: Sat Apr 04, 2020 5:50 pm
Location: VALENCIA - SPAIN

Re: STM32F4 serial.Available()?

Post 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
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F4 serial.Available()?

Post 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;
tcv
Posts: 4
Joined: Sat Apr 04, 2020 5:50 pm
Location: VALENCIA - SPAIN

Re: STM32F4 serial.Available()?

Post 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
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F4 serial.Available()?

Post 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.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: STM32F4 serial.Available()?

Post 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.
Post Reply

Return to “STM boards (Discovery, Eval, Nucleo, ...)”