toCharArray Giving Weird Results

I have a String that I want to convert to a char Array.
The following code WORKS FINE

char buff[100] = "";

strcpy(buff, "MyString5522");

However THE FOLLOWING gives me a weird block as the beginning character.

char buff[100];
String myString = "MyString5522";

myString.toCharArray(buff, myString.length()+1);

How can I fix this?

How and where are you seeing this weird character ? Please post a small but complete sketch that illustrates your problem

#include<SPI.h>

char buff[100] = "";

String B1SN = "MySerialTest4455";
String B2SN = "";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1)  // starting new sequence?
  {
     B1SN.toCharArray(buff, B1SN.length()+1);
     active = true;
     pos = 0;
     SPDR = buff [pos++];   // send first byte
     return;
  }
  if (c == 2)  // starting new sequence?
  {
     strcpy(buff, "ENERGIZER");
     active = true;
     pos = 0;
     SPDR = buff [pos++];   // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

This is a slave code for a SPI project

The obvious question is why are you using a String in the first place ?

I suspect that the problem is not in the toCharArray() function but elsewhere in your code

Try

void setup()
{
  Serial.begin(115200);
  char buff[100];
  String myString = "A";
  myString.toCharArray(buff, myString.length() + 1);
  Serial.println(strlen(buff));
  Serial.println(buff);
}

void loop()
{
}

Would it be better to just store everything initially as a char array?
Does this help with memory for my Arduino?

Yes to both questions

Ok I just converted to the char array but still the same problem, if I use strcpy(buff, SN) in the setup, everything works fine but as soon as the strcpy is in the interrupt it gives me this weird block.
I will send a quick screenshot

#include<SPI.h>

char buff[100] = "";

char B1SN[100] = "MySerialTest5524558Red";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1)  // starting new sequence?
  {
     strcpy(buff, B1SN);
     active = true;
     pos = 0;
     SPDR = buff [pos++];   // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

This gives This...

However if i place the strcpy in the setup it does what I expect, but I can't use that method as I need to update the buffer with different info

#include<SPI.h>

char buff[100] = "";

char B1SN[100] = "MySerialTest5524558Red";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
  strcpy(buff, B1SN);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1)  // starting new sequence?
  {
     active = true;
     pos = 0;
     SPDR = buff [pos++];   // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

Bad idea. The second argument to the toCharArray method is the size of the buffer, not the length of the string.

I found the problem.
On my master i have a delayMicroseconds, increasing this time removes the weird characters and increases the length of sendable data

I have not read in details but The indentation in this snippet seems to indicate you wanted to include the second line in the if expression. Are you missing {}?

And to empty a cString you don’t need strcpy() just use buff[0] = '\0'; or *buff = 0; and the likes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.