I am reading data from an RFID reader using software serial. The data is being received Byte by Byte and placed into a char array. The sketch below works great and displays the data perfectly, but I would like to find a way in which I can convert the char rfidData[Buffsize] into a string. I want a string so that I can call this string variable elsewhere in my code and it can be processed. The other part of the code is sending data with a GSM shield, so it must need the data in a string format. Does this make sense?
The whole thing has me a little confused.
Any help would be great.
#include <SoftwareSerial.h>
SoftwareSerial RFID(A8, A9); //pin2 Rx, pin3 Tx
#define BUFSIZE 15 // Size of receive buffer (in bytes) (10-byte unique ID + null character)
const int rfidon = A5;
void setup()
{
pinMode(rfidon, OUTPUT);
Serial.begin(9600);
RFID.begin(9600); // set the data rate for the SoftwareSerial port
digitalWrite(rfidon, HIGH);
}
void loop()
{
char rfidData[BUFSIZE]; // Buffer for incoming data
int G = Read_RFID_Tag(rfidData); //**
if (G == 0){
Serial.println(rfidData);
}
}
int Read_RFID_Tag(char *const rfidData)
{
long setime = millis();
char offset = 0;
byte C =0;
RFID.write(0x53);//This command sets the reader active (Command SRA)
RFID.write(0x52);//**
RFID.write(0x41);//**
RFID.write(0x0d);//**
while(millis() - setime < 2000){
if (C == 13 && offset == 17){
return 0;
//break;
}
rfidData[BUFSIZE]; // Buffer for incoming data
rfidData[0] = 0; // Clear the buffer
offset = 0; // Offset into buffer
while (RFID.available()) {
C = RFID.read();
rfidData[offset] = C;
offset++;
delay(1);
//Serial.print(C);
if(C == 13 && offset == 17){
//rSerial.println("");
//Serial.println(rfidData);
return 0;
//break;
}
}
}
RFID.write(0x53);//This command sets the reader deactive (Command SRD)
RFID.write(0x52);//**
RFID.write(0x44);//**
RFID.write(0x0d);//**
}
The difference between a character array and a string (small s*) is the null terminating character ('\0'). An array has no null terminator and a string does. Make your receive buffer, at least, large enough to hold the character array plus 1 extra element for the null. After you read the last character append a null to the array and you have a string.
*string-null terminated character array as opposed to a String-an object of the String class.
Yes, I understand. Although, I have assigned a buffer of 15, my offset position seems to 17 after I receive all of the data from the RFID correctly which has me confused. Why would that be? Should I increase the size of the buffer to allow for the null character?
I have made the following changes:
#define BUFSIZE 18
Is this the line of code I need to make the conversion?
Should I increase the size of the buffer to allow for the null character?
You MUST do that.
If you are reading 17 bytes from the RFID reader, you MUST size your array to hold 17 (or more) bytes.
That you haven't been burned yet means that you are trashing memory that is not otherwise being used. It's far better to be good than to be lucky, though.
Is this the line of code I need to make the conversion?
Code: [Select]
String myNewString(rfidData);
You do not need to convert the null terminated character array rfidData to anything else.
I want a string so that I can call this string variable elsewhere in my code and it can be processed. The other part of the code is sending data with a GSM shield, so it must need the data in a string format.
rfidData when null terminated is a "string" (small 's') and can be processed in any way you like using a powerful tool kit of standard string functions.
Commonly used functions are:
strcat - concatenate two strings
strchr - string scanning operation
strcmp - compare two strings
strcpy - copy a string
strlen - get string length
strncat - concatenate one string with part of another
strncmp - compare parts of two strings
strncpy - copy part of a string
strchr - string scanning operation
strtok- divide a string into pieces
You do not need and should avoid "String" (capital 'S') which can lead to memory problems.