Serial Read, Character Arrays and Pointers

Hello,

I am trying to capture serial data framed by unique start and end bytes into a character array using pointers.

I realize i could do it without pointers, but i am using this as a tool to try an understand how pointers work.

The function code is:-

void GetSerialPacket(char pkt[], int pkt_len, char start_char){  
  
  char *ptr = &pkt[0];
  Serial.println((int)*ptr);
    
  while(Serial.available()){                       
    if(Serial.read() == start_char){
      for(int i=0; i<(pkt_len-1); i++){
        *ptr = Serial.read();
        *ptr++;
      }
    }
  }
}

The call to the function is:-

void loop(){
     printchar(msg);    
     delay(1000);
     
     GetSerialPacket(msg, sizeof(msg),  'q');
     printchar(msg);
     delay(1000);
}

The character array 'msg' is my placeholder in memory for packets from the serial port.

What i am finding when i call the function is that the address of &pkt[0] changes every time the function is called. The contents of msg reflect the data written from the serial port but it looks like the memory address of msg changes with each call to the function in line with &pkt[0].

The output from the serial monitor when sent the following input from the serial port is given below, note how the number provided by (int)*ptr changes with each call.

qHello
qWorld
qGood
qBye

Operating Mode: Waiting

Contents of char array: 1234567

49

Contents of char array: 1234567

Contents of char array: 1234567

49

Contents of char array: Helloÿÿ

Contents of char array: Helloÿÿ

72

Contents of char array: Worldÿÿ

Contents of char array: Worldÿÿ

87

Contents of char array: Goodÿÿÿ

Contents of char array: Goodÿÿÿ

71

Contents of char array: Byeÿÿÿÿ

Contents of char array: Byeÿÿÿÿ

66

Help appreciated.

The snippet you posted doesn't show how your msg variable is declared. Posting partial code means you're only going to get partial answers, you need to post a complete sketch that demonstrates the problem.

Your code doesn't seem to detect the start and end of messages based on start/end characters as your description implied.

You implementation of GetSerialPacket() doesn't wait for a complete packet to be received, and it's not clear what you expect to happen if only a partial packet is available.

You aren't null-terminating the received character array so it is wrong to treat it as a string subsequently.

The use of the ptr variable is both unnecessary, and wrong. You could achieve identical behaviour by uysing pkt directly (and it would be clearer if you declared that as a pointer rather than an array, although it doesn't affect the behaviour) and you are incrementing the value pointed to, not the pointer.

*ptr++;

should be

ptr++;

I do not have a solution, but I would be very interested in what you do to solve it. I have a very similar problem. I am looking for the start string "\nE,". Here is what I have:

#define DATABUFFERSIZE 50*3
char databuffer[DATABUFFERSIZE];
...
while(Serial.findUntil("\nE,", "\r")) {
	while(Serial.available()==0);
	Serial.readBytes(databuffer,DATABUFFERSIZE);
	//databuffer[charread] = Serial.read();
	//charread++;
	count++;
	// write to file
	logfile.print(now.hour()   , DEC);
	logfile.print(now.minute() , DEC);
	logfile.print(now.second() , DEC);
	logfile.println(databuffer);
	Serial.println(databuffer);
	Serial.println("=====================");
}

However, I still get garbled text. For example, input is as follows (from an other arduino simulating an instrument):

  Serial3.println("E,00,00,00,005,046,00,00,000,00,244,9999,00,082");
  Serial3.println("1,08,00,00,001,119,00,02,000,00,244,008,000,041");
  Serial3.println("D,00,00,00,004,045,00,00,000,00,244,9999,00,079");
  delay(1000);
  Serial3.println("E,00,00,00,005,046,00,00,000,00,244,9999,00,082");
  Serial3.println("1,08,00,00,001,119,00,02,000,00,244,008,000,041");
  Serial3.println("D,00,00,00,004,045,00,00,000,00,244,9999,00,079");
  delay(1000);
  Serial3.println("E,00,00,00,005,046,00,00,000,00,244,9999,00,082");
  Serial3.println("1,08,00,00,001,119,00,02,000,00,244,008,000,041");
  Serial3.println("D,00,00,00,004,045,00,00,000,00,245,9999,00,080");
  delay(1000);

 ...
...

and the output I see from the receiving arduino is as follows:

00,00,00,0000,000,00,244,9999,00,079
E,00,00,00,005,046,00,99,00,083
1,07,00,00,001,119,00,02,000,00,244,008,000,040
D,00,00,00,004,045,00,00,000,01
=====================
00,00,00,005,046,00,00,000,00,245,9999,00,083
1,07,00,00,001,119,00,02,000,00,244,008,000,040
D,00,00,00,004,045,00,00,000,00,244,9999,00,079
E,00,2
=====================
00,00,00,005,046,00,00,001,08,00,00,001,119,00,02,000,00,244E,00,00,00,005,046,00,00,000,00,245,9999,00,083
1,08,00,00,001,119,00,02,000,00,244,008,02