-solved- extra 1 added from nowhere?

I am currently working on a c# project that includes a Boarduino. The concept is to receive a string via serial, store it, and prints the value sent back into serial. my char string is throwing out result +1… A strange observation is that the result is perfectly normal if I upsize the char array to 7 (6 values) could someone explain why if possible? thanks
sorry about that last post,and thanks for the fast response. straight from a freshly compiled file.

  • ben
char c[2];  //c[7] works?// where to keep the recieved data
int i; // my favorite variable
int output;  // flag on data stored

void setup() 
{               
  pinMode(13, OUTPUT);
  Serial.begin(9600); 
  Serial.flush();
}


void loop() {
 
  if (Serial.available() >= 3)  {   //6   works?
 
      for (i=0; i<3; i++) {    //i<6 works?
       c[i] = Serial.read();   
       }
      output = 1; // data stored flag
  }
  if (output > 0)
    {
    Serial.flush(); // clear serial buffer
    delay(10); // make sure things dont collide
    Serial.print(c);// print stored value   
    output = 0;  // reset flag
    }
  }

Three things...

  1. Welcome.

  2. Please click Modify ( or ) on your post, select the source code, click the button above the edit window, then click .

  3. I don't think the code you posted compiles. It's a bit difficult to offer advice without seeing the actual code you are using.

One problem is that you are using three array items to store data, but your array is only two items long:

char c[2]; 
...
for (i=0; i<3; i++) {
  c[i] = Serial.read();

an array of 3 would give me 4 char's even though i'm only using 3 though, right? tnx

Wrong.
An array of three elements (indices zero to two) would give you three "char"s

if I run with my array set to 3 I get a 4th garbage char on my response. if I run with 2 I get 3 char's and it has the extra 1 added to the value. (also does this with letters too eee results in eef) why would changing the values to reflect an array 7 (0 - 6) to not add the extra 1?

ok, I see what you mean now.. if I upsize the array to 4 the problem goes away....

I apologize for my silly noob question.. I thought so hard about this I was missing the obvious... thank you for your patience and timely responses!

thanks again - ben

    Serial.flush(); // clear serial buffer

Dump any data that you haven't read yet. Why?

    Serial.print(c);// print stored value

The overload of Serial.print() that accepts an array of characters expects that array to be NULL terminated. Your array is not. Unexpected results are quite likely when you do not play by the rules.

yeah, my problem revolved around my brain going backwards, when I initialized with a 7 char array this morning I noticed that I was properly sizing it, but I had gotten myself confused and thought I should have been counting the 0 (due to another language, that I was confusing in this) as for the flush.. it was put in for debug reasons, I intend on only sending the bytes I need, but when communicating via the test console I had written in C# I was manually typing in the strings, and would accidentally input more than what I was buffering into the receive. so basically it was just to keep my input regular regardless of me getting frustrated and sending too much... does that make sense?

I was manually typing in the strings, and would accidentally input more than what I was buffering into the receive.

Thereby proving that you need to be sending, and looking for, an end-of-packet marker.

And, of course, checking that there is room in the buffer before writing to it.

box_of_rain_s4: an array of 3 would give me 4 char's even though i'm only using 3 though, right?

You might be thinking of the reverse: a typical string uses one MORE character than expected (not less), for the terminating zero byte. But if you use 3 bytes, your buffer needs to be 3 characters. For buffers, it rarely hurts to add a byte or two (or five or ten) for safety - even on the Arduino. Of course, a lot of strings add up, but in your case, it's one buffer.

very true, my intent was to use a null or something. I had actually removed the buffer size check I was doing to simplify the code down and make life a bit easier when I approached the forum… my former experience is with with plc’s they do things a bit differently in this area(at least with the brand I learned on)… it definitely confuses me from time to time… I love everyone’s input on this and appreciate it. I never have anyone to talk to about stuff like this in my local world… its a nice refreshment.

-ben