Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 3 months ago.
RFID network with code
Hello, I'm doing an RFID project which use two LPC1768 chip and two Mbed application board. One can receive the RFID tag number and then transmit it to another Mbed. Thy are connected by the cable. Now, I have some problem with the code, could anyone helps me check it and solve the problem.
1 Answer
10 years, 3 months ago.
The problem is exactly what the error is telling you is wrong, rfid.read() is an integer. socket.send() expects a char * or char[]. You either need to convert the number to a string if you want to send it as text or cast it to a char* if you want to send it as binary data.
Also you do 3 rfid reads, they could give you different values or lock things up while waiting for a new read. It's far safer to read once and store the result.
If you want to send it as binary then it should be
if (rfid.readable()) {
// only read once so that we can't display one value and then send a different one over the network if two cards are close
int value = rfid.read();
lcd.printf("RFID tag number: %d\n",value);
socket.send_all( (char *)&value, sizeof(value));
}
To send text you would do
if (rfid.readable()) {
// only read once so that we can't display one value and then send a different one over the network if two cards are close
int value = rfid.read();
char valueString[10]; // a single 16 or 32 bit number should never go over 9 characters so this size should always be big enough.
int stringLen = snprintf(valueString, 10, "%d", value); // convert number to string
lcd.printf("RFID tag number: %s\n",valueString); // use the string version for the lcd, saves converting it twice.
socket.send_all( valueString, stringLen);
}
If you are using scanf() or something similar to read the data at the other end of the network link then it may make sense to include a \n at the end of the network data by adding it to line 5 above and then removing it from line 6.
In the future rather than using a screen shot to include code put <<code>> on a new line before the code and <</code>> after it.