10 years, 3 months ago.

Reading line from serial pc

I need to read a string sent by my pc in the general form of "2,3,5;800,34,123;899,422,0;", appended by a newline character. The string could be of any size. The fastest way to do this would be a readline() type of function like the arduino has, but to my knowledge that doesn't exist on mBed. I have tried the following code within my main function:

string dimensions = "";

delay(2000);

while(pc.readable()>0) { char p = pc.getc(); dimensions = dimensions + p;

if(p == '\n') { pc.printf(p+"\n"); break; }

delay(20);

}

pc.printf("Read in: "); pc.printf(dimensions.c_str()); pc.printf("\n");

However, for some odd reason it is only appending the first 15 characters to the string, and nothing more. (If I send in "2,4,7,1;4,66,43,200;" , it only prints back "2,4,7,1;4,66,43")

Can anyone provide a simpler solution to my problem, or find why my code is not working? Any help would be much appreciated.

Thanks

1 Answer

10 years, 3 months ago.

Instead of using while(pc.readable), I would use while (p != '\n'). Then it should just keep reading until it received everything. Now it is possible it stops reading, since there is no char in the receive buffer, because that is still being transmitted -> Reading some values and adding them to a string is alot faster than the actually transmissions via Serial.

Tbh your board has plenty of power to use strings instead of char arrays, but char arrays are alot more efficient on your memory. Although then you do need to know a maximum length (or make your own code which can deal with changing sizes of what you receive, similar to what string does internally). For Arduino it is the same, you cannot use strings there.

changed it to p != '\n', but it again only reads the first 15 characters. Before reading the string, I put more than enough delay to allow the string to be send from pc to mBed, so that is not the issue. Is it possible the mBed can only hold 15 characters in its Serial buffer? I don't think that should be the case but im a bit stumped on this.

I have also tried sending in the length of the incoming string first, generating a char array with the appropriate size, and then filling that array with the list of numbers, but i'm still getting the same 15 character limit thing

posted by Arnav Vaid 24 Aug 2015

Oh wait, you are not reading it directly. Then yeah you have the issue, your board has a 16 character receive buffer (don't know how it then ends up at 15 characters, but that will be the cause). This is actually a quite large receive buffer. But not enough to hold your entire string.

posted by Erik - 25 Aug 2015

I see, what do you suggest I do to solve this? When pc.getc() the a spot for a character gets freed up in the buffer right?

posted by Arnav Vaid 26 Aug 2015

Depending on your program it might be better to use buffers, or the lazy route: use BufferedSerial or MODSERIAL (that one is for the LPC1768, your device has the same UART, but it might require some coding on your part to make tit compile). These automatically place new data in a software buffer which can be alot larger than the hardware buffer.

Alternatively remove the delay, and just immediatly start reading until you receive the \n. (Note that it depends on your terminal settings if a \n is received ever, it can also transmit \r as last character).

You are correct thet getc frees up a space in the receive buffer.

posted by Erik - 26 Aug 2015