Hi Thomas,
What you are doing looks spot on. Here is a test program I just wrote:
// serial scanf test
// wire p9 - p10
#include "mbed.h"
Serial s(p9, p10);
int main() {
s.printf("R125\n");
int r;
int n = s.scanf("R%3d", &r);
printf("n = %d, r = %d\n", n, r);
}
and the result i get is:
n = 1, r = 125
In your code, i'd have a go at reading the return value of scanf to see how many elements it thinks it has read. If it is not what you expect (i'd suspect it is 0), the result is unlikely to be valid. Maybe your serial connection is sending other characters too, so the first thing it reads is not "R" and hence it exits scanf at that point?
For more information on scanf, a good reference is:
When you find out what is up, it'd be great if you could share it. You won't be the last to be fighting scanf :)
Simon
hello,
I'm sure it's a simple matter but it doesn't seem to be working the way I expect it to. I have a device that transmits a string like "R125\r". I need to store 125 as an integer, and from research it looks like I'm supposed to do something like serialobj.scanf("R%3d", &result); and then int result should be 125. This is not working and result is some incorrect number constantly.
Are there any more docs for Serial::scanf that I could look at, or could someone explain this function better? Thanks to everyone.