Output from Serial port

Hello!
I would like to read from serial and and write it to char array:

char wavelength1[]="1000";
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial1.available() > 0 && newData == false) {
    rc = Serial1.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
char buffer[4];
buffer[0] = receivedChars[5];
buffer[1] = receivedChars[6];
buffer[2] = receivedChars[7];
buffer[3] = receivedChars[8];

strcpy(wavelength1,buffer);

}

This is problematic code:
char buffer[4];
buffer[0] = receivedChars[0];
buffer[1] = receivedChars[1];
buffer[2] = receivedChars[2];
buffer[3] = receivedChars[3];
strcpy(wavelength1,buffer);

Because it gives me the whole buffer, for instance I sent 1064, but the output is 1064�
And I would not like this �.
Thank you for your help. :slight_smile:

Can you share how and where you defined all the variables your use?

I think your pb is the strcpy as buffer does not have a 5th byte with a 0 in it to mark the end of the string you need for strcpy

It is almost impossible to check your code because it is just a part.
For example, I don't know what 'numChars' is. Could that cause a overflow of the buffer ? I don't know.
If the buffer is full, you keep writing the received character to the last buffer location. If that is a '\r', it could be in the buffer.
There is no timeout for the characters that are being received. If you receive the first two bytes, and then there is nothing available (because the other characters are being received at that very moment), then the function stops.
If you receive data, and put it into a buffer, it is very dangerous to have that buffer with the exact amount of bytes. The 'wavelength1' could be too small.

J-M-L you were right, its working now. I had to add buffer[4]='\0';
Thank you

I had to add buffer[4]='\0';

I hope you expanded "buffer" to five elements.

AWOL:
I hope you expanded "buffer" to five elements.

hopefully he did indeed!

@Nejcar - great news - have fun