Loading...
Pages: [1]   Go Down
Author Topic: Arduino / SoftwareSerial/replacing ascii string  (Read 196 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

California
Online Online
Edison Member
*
Karma: 37
Posts: 1827
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Edison Member
*
Karma: 41
Posts: 2170
What a host of balls she had seen: gaity, the brass buttons...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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


California
Online Online
Edison Member
*
Karma: 37
Posts: 1827
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your current code?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: [1]   Go Up
Print
 
Jump to: