how to parse a string?

Hi… I’m trying to create a string of characfters to extrat each value but its not working… Im doing something wrong…

	Serial.begin(115200);
	float vteste = 23.45;
	int vteste2 = 1;
	int vteste3 = 2;
	int vteste4 = 3;

	char valores_juntos[4] = { vteste, vteste2, vteste3, vteste4 };
	char delimitador[] = ",";
	char *resultado = NULL;
	resultado = strtok(valores_juntos, delimitador);

	while (resultado != NULL) {
		printf("result is \"%s\"\n", resultado);
		Serial.println(resultado);
		resultado = strtok(NULL, delimitador);
	}

Its comma delimited …this code returns some weird characters.

I’m new into arrays etc… please Help…

:slight_smile:

maybe because these aren’t strings

{ vteste, vteste2, vteste3, vteste4 };

convert them to strings first.
use ‘iota’ for ints

I’m not sure about float to string function.

Hi... First of tll thank you.

Well..Actually I'm new into this functions and maybe that woulb be better If I change my question...

Well.. How Could I convert the below values into comma delimited string for using un a char array?
float vteste = 23.45;
int vteste2 = 1;
int vteste3 = 2;
int vteste4 = 3;

string all_values_together ="??????"
Could anybody help me?

Tks a lot!

cabecinhas:
Hi… I’m trying to create a string of characfters to extrat each value but its not working… Im doing something wrong…

Here is a piece of code from a project I did a few years ago. It accepts commands via the serial port and executes the appropriate command.

Optionally, parameters can be specified. For example, you can type:

command

or

command, parameter

The code isn’t complete for your use obviously, you’ll have to edit it, but it should get you going in the right direction.

Hope this helps.

#define buffer_size 32

void setup (void)   /* this runs once at Arduino reset */
{
	Serial.begin (115200); /* setup serial port baud rate */
}

void loop (void)   /* this runs continuously */
{
	get_Command(); /* get command and run it */
}

/* table of commands we recognize - upper and lower case INPUT both match this table
   so be sure to enter all commands to be found in UPPERCASE  */
const char *cmd_table[] = {
	"FAN", /* 0 */
	"RPM", /* 1 */
	"FANSW", /* 2 */
	"LIGHTSW", /* 3 */
	"ECHO", /* 4 */
	"REPLY", /* 5 */
	"HELP", /* 6 */
	/* go ahead, add more commands! */
};

/* sprintf buffer and line input */
char buffer [buffer_size];

/* search command table and find a match with the line input buffer */
void get_Command (void)
{
	int n, x, err;
	int line_len;
	int cmd_len;
	int cmd_count;

	if (! (cmd_len = line_len = read_line (buffer, buffer_size))) {
		return;
	}

	/* how many commands are in the command table */
	cmd_count = (sizeof (cmd_table) / sizeof (cmd_table[0]));

	/* find a comma (command / parameter separator) if present */
	for (n = 0; n < line_len; n++) {
		if (buffer[n] == ',') {
			cmd_len = n; /* when done, cmd_len is the length of the command ONLY */
			break;
		}
	}

	for (n = 0; n < cmd_count; n++) {
		err = 0;

		for (x = 0; x < cmd_len; x++) {
			if (cmd_table[n][x] != buffer[x]) {
				err++;
			}
		}

		/* if command matched the table and is the same length, then it's valid */
		if ( (!err) && (cmd_len == strlen (cmd_table[n]))) {
			parse_cmd (n, buffer);
			break;
		}
	}
}

/* extract the numeric part of the command (if any) */
void parse_cmd (int cmd, char *param)
{
	char *ptr = param;
	int n;
	long int value;
	int param_len;

	param_len = strlen (param);

	for (n = 0; n < param_len; n++) {

		ptr++;

		if (param[n] == ',') {
			break; /* found a comma, we're done */
		}
	}

	/* here, ptr points to the parameter (if it exists) ONLY */
	if (strlen (ptr) == 0) {
		value = -1; /* -1 means "no parameter found" */

	} else {
		value = atoi (ptr); /* parameter is numeric */
	}

	exec_cmd (cmd, value); /* call main code (if param == -1 then no param was specified) */
}

/* call the parsed command and pass it the param (if there was one) */
void exec_cmd (int cmd, long int param)
{
	/* call the appropriate function for each command */
	switch (cmd) {
		case 0:
			cmd_0 (cmd, param);
			break;
		case 1:
			cmd_1 (cmd, param);
			break;
		case 2:
			cmd_2 (cmd, param);
			break;
		case 3:
			cmd_3 (cmd, param);
			break;
		case 4:
			cmd_4 (cmd, param);
			break;
		case 5:
			cmd_5 (cmd, param);
			break;
		case 6:
			cmd_6 (cmd, param);
			break;
		default:
			break;
	}
}

/* read a line from user into buffer, return char count */
int read_line (char *buf, int limit)
{
	int x;
	int ptr = 0;

	buf[0] = 0; /* clear first byte of buffer */

	if (!Serial.available()) {
		return 0; /* no data, exit */
	}

	while (1) {

		if (Serial.available()) {

			x = toupper (Serial.read()); /* uppercase everything */

			if (x == 0x0D) { /* carriage return means done typing */
				if (echo) {
					Serial.print ("\r\n"); /* echo a newline */
				}

				buf[ptr] = 0; /* mark end of string */
				return ptr; /* ptr equals character count */
			}

			if (x == 0x08) { /* backspace */

				if (ptr > 0) { /* if not already at the start of the line... */

					ptr--; /* back up pointer */

					buf[ptr] = 0; /* erase bs'd character */

					if (echo) {
						Serial.print ( (char) 0x08); /* erase bs'd char on the terminal */
						Serial.print ( (char) 0x20);
						Serial.print ( (char) 0x08);
					}
				}

			} else {
				if ( (ptr < (limit - 1)) && (x != ' ')) {

					if (echo) {
						Serial.print ( (char) x); /* echo received char */
					}

					buf[ptr] = x;
					ptr++;
				}
			}
		}
	}
}

What's the point of creating a comma delimited string from some values, and then parsing that string?

PaulS:
What’s the point of creating a comma delimited string from some values, and then parsing that string?

The “string” data comes from user input. In my original program, you could do this for example:

FAN (Reported the RPM of a fan)

FAN 2000 (Set the fan to 2000 RPM)

Since the OP wants to parse data, I figured he could use parts or all of my code snippet.

The "string" data comes from user input.

Not in OP's case.

cabecinhas:
Hi... First of tll thank you.

Well..Actually I'm new into this functions and maybe that woulb be better If I change my question...

Well.. How Could I convert the below values into comma delimited string for using un a char array?
float vteste = 23.45;
int vteste2 = 1;
int vteste3 = 2;
int vteste4 = 3;

string all_values_together ="??????"
Could anybody help me?

Tks a lot!

Try something like this...

  char fbuffer[10];
  char buffer[40];

  sprintf(buffer,"%d,%d,%d,%s",vteste2,vteste3,vteste4,dtostrf(vteste,5,2,fbuffer));

PaulS:

The "string" data comes from user input.

Not in OP's case.

Still looking for YOUR "helpful contribution".............

Regardless of where the data comes from, parsing code to iterate across a string, locate delimiters and parse numeric parameters has (IMHO) some of if not most of the pieces that the OP needs.

If not, then maybe someone else can use it.

Point is, I'm trying to HELP, not CRITICIZE.....

nuff said.

DavidOConnor:

cabecinhas:
Hi... First of tll thank you.

Well..Actually I'm new into this functions and maybe that woulb be better If I change my question...

Well.. How Could I convert the below values into comma delimited string for using un a char array?
float vteste = 23.45;
int vteste2 = 1;
int vteste3 = 2;
int vteste4 = 3;

string all_values_together ="??????"
Could anybody help me?

Tks a lot!

Try something like this...

  char fbuffer[10];

char buffer[40];

sprintf(buffer,"%d,%d,%d,%s",vteste2,vteste3,vteste4,dtostrf(vteste,5,2,fbuffer));

Hi!!

THANK YOU SO MUCH!

Thats all I was looking for!

just one more question... where Could I get more information about the "parameters".. like "%d" "%s" etc,, I supose d is "digit", s "string" etc, right? Where could I get more information about it

Tks a lot!

Take a look…

http://www.cplusplus.com/reference/cstdio/printf/

Perfect David!

Thanks for yor fast and objective asnwer!