1 year, 2 months ago.

Serial.getc() & CoolTerm (MACOS) - 1 ascii character is ok more doesn't work

Hello,

I'm trying to convert some code from Arduino to STM32767Zi.

Code

bool receiveData(uint8_t * data)
{

if (Serial.readable()) data[0] = Serial.getc();
else return false;

wait(5);

if (Serial.readable()) data[1] = Serial.getc(); else return false;
if (Serial.readable()) data[2] = Serial.getc(); else return false;

}

return true;

Some points: - CoolTerm (MACOS) for the communication; - Send String (Hex) option to send 3 bytes;

Problem: The function reads the first byte but can't read the others two. Can you help me to understand what is happening here?

ex.: sending -> 0x01 0x02 0x03 (press send) data[0] = 0x01 -> return false

If I send character by character I get 2 bytes.

ex.: sending -> 0x01 (press send) 0x02 (press send) 0x03 (press send) data[0] = 0x01 -> data[1] = 0x02 -> return false

Thank you, Hugo Freitas

Comment on this question

1 Answer

1 year, 2 months ago.

The STM32F767 probably doesnt have a hardware fifo (check datasheet) so you need to read the received byte from the serial port before the next one arrives or that next byte will be lost. So when you send three bytes in one burst the first one will be stored in the serial port buffer and you read it OK, then you wait 5 sec and try to read the next two, but they have not been saved because the serial buffer was still filled with 0x01 when they arrived. In the second case you send one byte (0x01), it is stored in the serial port buffer, you read it and store it in data[0] before you send the next one (0x02), your code waits 5 secs before reading that second value and storing it, it then immediately attempts to read the third byte (no wait (5) in between) and fails because you either have not yet pressed the send button again and the serial port is still empty or the third byte was lost because the serial port buffer was still occupied with 0x02 when 0x03 arrived. The solution is to immediately read a byte as soon as it arrives in serial port buffer and store it in data[] array before doing anything else. Check out the mbed serial interrupt and setup your code to respond to the interrupt, handle the new byte and set a signal for the main loop which does the display stuff when something has arrived.

Thank you for the suggestion. It worked, but I should say that interrupts work better with class RawSerial than Serial and more information can be found here: https://os.mbed.com/cookbook/Serial-Interrupts.

posted by Hugo Freitas 05 Sep 2018