1

I am trying to extract a substring out of another string, but it doesn't seem to be working. I am new to programming in arduino and c++ in general. Here is my code:

void setup()
{
    Serial.begin(9600);
}

char rfid;

void loop()
{
  while (Serial.available() > 0)
  {
    rfid = Serial.read();
    String cardnum(rfid);
    cardnum.replace("", "");
    cardnum.replace("", "");
    String id = cardnum.substring(0,2);
    Serial.print(id);
  }
}  

The first argument in cardnum.replace("", ""); and cardnum.replace("", ""); replaces the start and end bit with nothing, but it somehow doesn't display it correctly here.
Now on to the problem at hand. The output for cardnum is 1F006B471F2C which it should be, but the ouput for id is the same, when i want it to be only the first two characters of cardnum. How would i go about achieving that.
Thanks in advance for any answers you can provide.

3
  • First off you're not reading the serial properly. You should read and digest this before you go any further: hackingmajenkoblog.wordpress.com/2016/02/01/…
    – Majenko
    Commented Apr 5, 2016 at 17:38
  • The entire while{} loop executes once for each character you read - is that what you intend? I suspect your intent was to read the the whole string and then modify it. If so, perhaps you'd find the Serial.readBytes() function useful.
    – JRobert
    Commented Apr 5, 2016 at 17:41
  • Thanks for your answers. Yes my intent was to read the whole string. This helps me out a lot.
    – y4my4m
    Commented Apr 5, 2016 at 17:59

1 Answer 1

2

Let's dissect your program a moment and see why it's not doing what you want:

while (Serial.available() > 0)
{

So while there are characters available to read in the serial buffer

  rfid = Serial.read();

read one character into the rfid variable.

  String cardnum(rfid);

Convert that character into a string.

  cardnum.replace("", "");

Replace nothing ("") with nothing ("")

  cardnum.replace("", "");

Do the same again,

  String id = cardnum.substring(0,2);

Take the first two characters of the string that contains the single character that you have read.

  Serial.print(id);

Print it.

So basically you have read one character, made it into a string, done nothing to it, copied it to another string, then printed it.

Instead of this method, which does nothing useful, you should read the whole ID into a string (I'd use a C string [i.e., char array] rather than an Arduino String object) then perform operations on that string once you have read the whole ID code.

There's many methods of doing that, but how you do it is heavily influenced by exactly how the data is presented to you. Does it have a terminating CR or LF to indicate the end of the string? If not you will have to use some form of timeout to determine when the string has been completer.

1
  • Thanks for your answer. cardnum.replace("", ""); replaces the bit that signals the start and the end of the id with nothing. It looks like there is nothing because Stackexchange can't display the symbol. When i print cardnum it displays 1F006B471F2C. So it should have read more than one character i assume.
    – y4my4m
    Commented Apr 5, 2016 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.