Pages: [1]   Go Down
Author Topic: Arduino / SoftwareSerial/replacing ascii string  (Read 375 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
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
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
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 170
Posts: 12451
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 -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: