Go Down

Topic: Arduino / SoftwareSerial/replacing ascii string (Read 480 times) previous topic - next topic

dscott

Hi - I'm new to programming and attempting to replace part of an input string with another string. I'm using a Uno with a CuteDigi Protosheild with a MAX233. I'm inputting/outputting a 9600 baud serial string ($OHPR,xxx.x,xxx.x,xx.x,xx.x*xx) thru the 233 into the Uno using SoftwareSerial.h thru D2 and out thru the USB serial port. I want to replace the beginning '$OPHR' with '$GPXDR'. I can print the sring and strip out the '*xx' but cant seem to figure out how to replace those beginning characters. Any input would be helpful.

Thank you

Arrch

Assuming those strings are exact and don't vary, you can create a new string that is the length of the original + 1, and use strncpy() twice.

dscott

Hi - Thanks for the reply. The serial string data does change - the original string is heading, roll, pitch and temp. The $OHPR, does not change and this is what I'd like to replace with $GPXDR.

majenko

Do you need to replace it, or just print $GPXDR instead of $OHPR ?

Code: [Select]

char *in = "$OHPR,xxx.x,xxx.x,xx.x,xx.x*xx";

Serial.print("$GPXDR");
Serial.println(in+5); // Print from the 6th character (,)

Arrch


Hi - Thanks for the reply. The serial string data does change - the original string is heading, roll, pitch and temp. The $OHPR, does not change and this is what I'd like to replace with $GPXDR.


That's what I meant, if it's always going to start with "$OHPR", then you can use strncpy() to a new string. If you just wante to print it, use majenko's code.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up