byte array print issues... Solved #6

This prints fine :

byte arrayToPrint[21] = { “THIS IS LINE ONE …!”};

Serial.write( arrayToPrint, 20);

This does not print at all :

struct dataStruct {
byte arrayToPrint[21];
byte cursorRow;
} txDataStream;

txDataStream.arrayToPrint[21] = { “THIS IS LINE ONE …!”};

Serial.write(txDataStream.arrayToPrint, 20);

Why ??

I'm fuzzy on the details, but I think C++ handles initializers in a structure differently than "regular" C. Try this (untested):

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

struct dataStruct {                                            
  byte arrayToPrint[21];
  byte cursorRow;
} txDataStream = {"THIS IS LINE ONE ..!", 0};  // Initialize after struct is defined

Serial.write(txDataStream.arrayToPrint, 20);

}

void loop() {
}

econjack:
I'm fuzzy on the details, but I think C++ handles initializers in a structure differently than "regular" C. Try this (untested):

void setup() {

// put your setup code here, to run once:
  Serial.begin(9600);

struct dataStruct {                                           
  byte arrayToPrint[21];
  byte cursorRow;
} txDataStream = {"THIS IS LINE ONE ..!", 0};  // Initialize after struct is defined

Serial.write(txDataStream.arrayToPrint, 20);

}

void loop() {
}

OK that works. But thats fine if i have only message to print .. in reality I need to be able to change the message that I pass to that byte array from inside the loop. When I tried to do that, the compiler does not emit any error but printing also does not happen. Tried both ways ...( one at a time of course !)

txDataStream.lcdMessage[21] = {" This is line No 1. "};

txDataStream.lcdMessage[21] = " This is line No 1. ";

Serial.write(txDataStream.arrayToPrint, 20);

I guess I don't see the problem. Just use strcpy() to change it:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  struct dataStruct {
    byte arrayToPrint[21];
    byte cursorRow;
  } txDataStream = {"THIS IS LINE ONE ..!", 0};  // Initialize after struct is defined

  Serial.write(txDataStream.arrayToPrint, strlen(txDataStream.arrayToPrint));

  Serial.write('\n');

  strcpy(txDataStream.arrayToPrint, "New message");

  Serial.write(txDataStream.arrayToPrint, strlen(txDataStream.arrayToPrint));

}

void loop() {
}

Why are you using the array size outside of the structure definition? txDataStream is a struct object with two members and my code initializes it at the time of definition. If you want to change it after it has been defined and initialized, just use strcpy() on that member. You do not need to use the array size again outside of the structure definition.

struct dataStruct
{
  byte arrayToPrint[21];
  byte cursorRow;
};

dataStruct txDataStream = 
{
  "THIS IS LINE ONE ..!", // the text to print
  0 // the row
};

void setup()
{
  Serial.begin(9600);
  Serial.write(txDataStream.arrayToPrint, 20);
}

void loop()
{
}

Not tested but I suspect it to work :wink:

Please post complete code and please use code tags (embed your code between
** **[code]** **
and
** **[/code]** **
).

txDataStream.arrayToPrint[21] = { "THIS IS LINE ONE ..!"};

Your array has 21 elements in it, numbered 0 to 20. There is NO element at position 21 in which to put anything.

If there were, the type of element is byte, so how can you expect to store an array in a byte?

econjack:
I guess I don't see the problem. Just use strcpy() to change it
.....

strcpy(txDataStream.arrayToPrint, "New message");
.....

strcpy() did what I wanted!! Now I can continuously keep changing the message from within loop

Thanks econjack.