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.