Ok, I just didn't want to give you the answer when you'd done so much good work narrowing it down!
So, close your eyes and imagine you are scanf. And i've just told you to read a decimal number (%d) from the serial port. What is your logic?
You wait eagarly for a character to come in....
Ah, here's one! A '2' just arrived. So the answer is 2!
Or is it? What if the next character to arrive is a 4? Would the answer still be 2? Or 24?
So the underlying problem you have in your design is you have devised a communication protocol with variable length encoding (a plain decimal number) with no delimeter (e.g. \n) so it is impossible to know when something ends and the next starts. You are seeing a "hang" because the scanf has no choice but to wait for the next character to see if it is a number. Either no character will ever come if you only sent it once, or it will come but despite being the start of the next number, it doesn't know that is what you intended so has to assume it is still part of the first (either way; hang!).
So the standard solutions to solve this may be:
- Use a delimiter (e.g. comma, new line, etc)
- Use a fixed length code (e.g. print out the number to always contain a certain number of zero padded digits; printf("%06d", value))
Hope this makes it clear on why it works like this, and some possible solutions.
Simon
btw, I think a good solution would be:
xbee1.printf("%d\n",id);
xbee2.scanf("%d\n",&id);
as a new line is a natural way to seperate elements as used in csv. Then for example, you could start sending more data per packet:
xbee1.printf("%d,%d\n",x, y);
xbee2.scanf("%d,%d\n",&x, &y);
and also be able to read it on a terminal, store it in a file, or open in excel easily.
Hello,
I have mbed n°1 with an RFID reader and XBee n°1. When reading an RFID, I send it thru XBee to mbed n°2. It works perfectly but only once. It seems that the receiving code I wrote needs some reset to clean the Xbee status because even if I send the RFID value once, the Xbee n°2 is saying the ID is still coming thru indefinitely... the xbee.readable() is allways true.
I am sure this is very basic but I do not find any clue in the Serial lib. Can some one bring me some light ?
Here is the sending code :
while (1) {
int id = rfid.read();
if (id) {
xbee1.printf("%d/n",id);
id = 0;
}
}
Receiving code :
while (1) {
if (xbee2.readable()) {
xbee2.scanf("%d", &id);
Nokialcd.printf("RFID is %d",id);
} else {
Nokialcd.printf("Nothing");
}
}
Thaks in advance !