8 years, 9 months ago.

how to avoid linefeed or carriage return during serail read

Hi there, I am working on the serial communication on the NUCLEO-F446RE. I send 8 bytes from PC to the device. In the micro, I use scanf to recive the 8-byte (string)

micro.scanf("%s", data); 

However, I find that if the data contains the linefeed or carriage return, scanf will only read the characters before the linefeed or carriage return so the some data lost. Is it any way to avoid this and receive all data at once?

1 Answer

8 years, 9 months ago.

How do you know when all the data has been received?

You probably want something like:

char data[51];
int count = 0;
do {
  data[count++] = micro.getc();
  if (count==50) // buffer is full exit no matter what
    break;
} while ( <insert end condition here> );
data[count] = 0; // add the c string termination character

all you then need to do is add whatever condition it is you want to use to end the data retrieval.

UPDATE- I just noticed that you said the string was 8 bytes. Which makes the exit condition simple: while (count < 8)

However if you are just counting bytes you will need to ensure that things start in sync

Thanks for the reply. I run the code in SWO debug mode and observe the change of the buffer. That's how I see only the data before \n are received.

From your code, do I have to add micro.readabled() in front of each getc()? Or what will getc behave if the pc end is not ready to send data or not yet run, will it just block there or get something random?

I think I am having a difficult to assure the communication start in sync. Any hint?

posted by kim jone 02 Feb 2016

getc() will block until there is data to read.

Is there any byte or structure you can use to indicate the start or end of the data. How about a pause between data messages?

posted by Andy A 02 Feb 2016

Thanks Andy. I am thinking to send a special character from pc to the micro as signal of communication start. But do you think I need to send something back to pc so to tell it to start passing the data? or it is safe to start collecting data once the special character received by micro?

BTW, is it any way in mbed that I can clear all content in the read or write buffer before any action made?

posted by kim jone 02 Feb 2016

Just sending the signal once in one direction should be ok if you can be sure the other end is ready and there is no risk of data getting lost along the cable. Data loss is not an issue unless you have long cables so it mainly comes down to making sure you start things in the correct order and you'll be fine.

If this is something that will be under your control then you can make sure you start things correctly, if it's for someone else to use once you've got it working then some form of two way communication is probably worth it in case things are started in the wrong order.

The normal way to clear the buffer is:

while (micro.readable())
  micro.getc();

This will read all the data waiting in buffer at a far higher rate than it can arrive and so you'll end up with an empty rx buffer. There's no easy way to clear the transmit buffer but if you know the baud rate and transmit buffer size (16 bytes maximum) then you can work out the time it would take to send a full buffer, if you wait longer than that you know the buffer must be empty.

posted by Andy A 02 Feb 2016