PARSE AN INPUT STRING INTO CHARS AND INTEGERS

HI,

MY old ISA 486 COmpaq shortcircuited in the psu and it killed my EPROM-burner (Sunshine 4C) as well.

So I plan to build a new EPROM-reader using a 2650 MEGA and do the strapping depending on the EPROM-type (2716 - 27512) on a breadboard. (port A data , port B and C = addresses , port D = control).

I want to control the 2560 by sending external Commands to it.

EG:

A 0000 --> Set READ address

C 0,1 --> Set /OE and /RE = 0 and 1 or C 0,0

R --> READ DATA

Reading the Command line from the EXTERNAL PC is not a proplem :

char inChar;
String inString = "";

(* part of LOOP *)

while (Serial.available() > 0)
{
inChar = Serial.read();
if (inChar != '\n')
{
inString += inString;
}
else
{..... }

inString = "";
}

From here I am stuck:

I want to check if inString[0] is a valid command (= A,C,R) and afterwards I want to check if
instring[1] is a valid delimiter [" " or ","] / (space or comma ) and then check if the next input(s) are valid hex-digits and in the C command the /OE and /RD are separated by a valid delimiter .

Tried to copy several of the examples given in these pages, but the results a not good (for me anyway!)

So how Do I do that ?

Kristian aka Snestrup2016

I wouldn't bother using String.
I would bother with posting the code I'm having problems with, in code tags.
YMMV

I would keep ALL of my questions about this topic in the first thread you created.

HI,

#AWOL:

I don't know how to say this, so I say it directly:

In one way you made me start thinking (I don't know how you did it, but you did !)

So I began rewriting and testing (and some searching on the I-net) and a lot of trial and (even more) errors, but finally I did this:

String str_data = "";
char inChar;

void setup()
{
Serial.begin(9600);
// put your setup code here, to run once:
// CharPtr = 0;
}

void loop()
{
while (Serial.available() > 0)
{
inChar = Serial.read();
if (inChar != '\n')
{
str_data = str_data + char(inChar);
}
else
{
str_data = str_data + char(0);

Serial.print("11 " + str_data);
Serial.println();

int firstCommaIndex = str_data.indexOf(',');
int secondCommaIndex = str_data.indexOf(',', firstCommaIndex+1);
String cmd = str_data.substring(0, firstCommaIndex);
String param1 = str_data.substring(firstCommaIndex+1, secondCommaIndex);
String param2 = str_data.substring(secondCommaIndex+1);

Serial.print(cmd);
Serial.println();
Serial.print(param1);
Serial.println();
Serial.print(param2);
Serial.println();
Serial.println();

delay(5000);

str_data = "A,0000,FFFF";
firstCommaIndex = str_data.indexOf(',');
secondCommaIndex = str_data.indexOf(',', firstCommaIndex+1);
cmd = str_data.substring(0, firstCommaIndex);
param1 = str_data.substring(firstCommaIndex+1, secondCommaIndex);
param2 = str_data.substring(secondCommaIndex+1);

Serial.print(cmd);
Serial.println();
Serial.print(param1);
Serial.println();
Serial.print(param2);
Serial.println();
Serial.println();
str_data = "";
}
CharPtr = 0;
}

}

The second part of the code (line: delay (5000) ++++) is for testing only and will be removed later ! ..

My second problem is to convert a HEX-input (string) (0x0000 --> 0XFFFF) to Integer.

How do I do That ? Is there in C++ a predefined function ?

snestrup2016

In one way you made me start thinking (I don't know how you did it, but you did !)

Well,I tried to get you to address your use of String, and your lack of use of code tags

Looks like I failed on both counts.

#Awol;

To be honest:

I am not that good to C++ (yet) --

snestrup2016

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

HI,

#AWOL:

You wrote:

"Well,I tried to get you to address your use of String, and your lack of use of code tags

Looks like I failed on both counts. "

Sorry for asking, but what do you mean by that ? As I stated earlier. I'm not very good to C++ (at present) but I am willing to learn, and if I can "kill" a bad habit in the start a lot of people (I think) will be happier.... (me included).

I have carefully studied the "Serial Input Basics " that Robin2 suggests.

snestrup2016

what he means is please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

the other hint is about the class String versus c-string (null terminated char array). The String class tends to use more memory (lots of code) and can possibly poke holes into the stack depending on how well or bad you use it... better stick to c-string on small micro-controllers with limited RAM

HI,

Thanks a lot, I'll remember that next time.

Saving bytes is always a good idea, I did not know that the STRING class could do that . So Thanks again.

Anyway I am rewriting my code after reading "Serial Input Basics " (#Robin2 suggestion) hoping saving bytes and execution time.

Thank a lot for your answer(s). ALL OF YOU...

snestrup2016 (very) newbie...