# serial.read() and device it to multi string

Hi,
result: 1, 23, 6, 0, 444, 5 (the numbers can change between 0-512)
my question is :
I know that the pattern is number and there is 6 of them.
how can I show only the results like this:

asnwer2=23
asnwer4=0
asnwer6=5

I have try to do this but I’m stuck

if (in_sbdi==' ')
{
soa=true;
eoa=false;
}
else if (in_sbdi==',')
{
soa=false;
eoa==true;
}


I’m know how to recognize start\end of answer - but how do I continue from here?

Thanks ,

The strtok() function bears investigation.

Nor did you take time to google strtok(), did you?

If the data being sent is properly comma delimited, code like below might be a start.

//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,

if (Serial.available())  {
char c = Serial.read();  //gets one byte from serial buffer
if (c == ',') {
Serial.println(readString); //prints string to serial port out
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
}
}
}


I have search - but it must be the time of the day - and I miss spelling it - my mistake …sorry

but now I know I have problems with my definition of the function

this is an example that I have found :

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}


and this is what I have try to do :

 Serial.print(full);//this is my full answer from the device
char * pch;
pch = strtok(full,' ', ',');
while (pch!='\n')

{
Serial.println ("/%s\n",pch);
here I got lost - and I don't know if until here it's O.K

}


this is an example that I have found

Which uses two arguments - the string to tokenize and an array of delimiters.

and this is what I have try to do

Which tries to call strtok with three arguments. That won't work.

You only have one delimiter - the comma. So, the code looks like:

  char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str, ",");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, ",");
}


Easiest way I think:

It’s said that -

call of overloades ‘println(const char [34],char [44])’ is ambiguous

• what does it mean?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;

void setup()
{
pinMode(test,INPUT);
pinMode(13,OUTPUT);
Serial.begin(19200); // Open serial communications and wait for port to open:
delay(1500);
mySerial.begin(19200);
delay(1500);
Serial.println("Modem connection is on!");
}

void loop()
{
{
char in_csq;
char full[44];
mySerial.println("AT+sbdi");
delay(16000);
if  (mySerial.available()> 7)
for (int csq_num=0;csq_num<43;csq_num++)

char * pch;
Serial.println("spliting string \"%\" into token: \n" ,full);
pch = strtok(full,",");
while (pch !=NULL)
{
Serial.println("%s\n",pch);
pch=strtok(NULL,",");
}

}

}


Also I have try the second code that was offer by

guix

and I think the problem is that this is not the full answer - this is my full answer:

AT+sbdi

+SBDI: 2,41,2,1,3,8

OK

but when I write it like it is I still don’t get nothing…
this is how I wrote it:

int16_t a1,a2,a3,a4,a5,a6;
//      sscanf(full,"at+sbdi  +sbdi: %hu,%hu,%hu,%hu,%hu,%hu"  OK,&a1, &a2, &a3, &a4, &a5, &a6);
//      printf("answer1= %hu\n 2 = %hu\n  3 = %hu\n 4= %hu\n 5=%hu\n 6=%hu\n " , a1,a2,a3,a4,a5,a6);


Try with:

if ( sscanf( str, "at+sbdi\r\n+sbdi:%hu,%hu,%hu,%hu,%hu,%hu", &a1, &a2, &a3, &a4, &a5, &a6 ) == 6 )
{
char str2[90];
sprintf( str2,
a1, a2, a3, a4, a5, a6
);
Serial.println( str2 );
}
else
{
Serial.println( "fail" );
}


no,sorry - still no good I get fail

wait wait wait wait…

I have return to the first code and I understand my mistake
now it’s almost working
It’s printing 5 out of the 6 numbers - just the first one I can’t get. this is the code
I have enter the numbers into a variable because I need to do things with them latter.

#include <SoftwareSerial.h>
#include <stdio.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;

void setup()
{
pinMode(test,INPUT);
pinMode(13,OUTPUT);
Serial.begin(19200); // Open serial communications and wait for port to open:
delay(1500);
mySerial.begin(19200);
delay(1500);
Serial.println("Modem connection is on!");
}

void loop()
{
{
char  full [42];
Serial.println("sending AT+SBDI");
mySerial.println("AT+sbdi");
delay(16000);
if  (mySerial.available()> 41)
for (int csq_num=0;csq_num<41;csq_num++)

Serial.println(full);
delay (1000);

Serial.println("test2..........");
char * pch;
int i=0;
char *david[6];
printf("spliting string \"%\" into token: \n" ,full);
pch = strtok( full, ",");
while (pch !=NULL)
{
printf("%s\n",pch);
pch=strtok(NULL,",");
david[i]=pch;
i++;
}
for (int t=0;t<5;t++)

{
Serial.print(t);
Serial.print ("-   ");
Serial.println(david[t]);
}

}
}


the IMG is what I get

      printf("spliting string \"%\" into token: \n" ,full);


What is this supposed to print? What, exactly, is the %nothing format specifier supposed to do for you?

        printf("%s\n",pch);


Is is too much to expect you to print something before and after the string to identify and bound it?

I don't see either of these statements having any impact on what is in your screenshot.

I'm learning the language from post to post.... (the new commands at least ) I didn't know what I need to put after the % can you explain me what I need to put there ? the full string? the part I need? what?

Thanks ,(for the patience)

The % introduces a format specifier. Presumably you put it there because you want the variable formatted somehow. I suspect that %s is the correct format specifier, but that is only a(n educated) guess.

I’m sorry - but maybe my English is not as good as I think ,so let me see if I understand:
the %s stand for what? - the string “full”? right?
that mean that printf is saying :
printf("this is what you have in the string full “%s” ,full)
if this is true - I still don’t see any print in the serial.- but this is not a problem.
I want to see all 6 numbers and I can only see the last 5.

this is the setup part:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;
int sendbutton=5;
boolean cansend=false;

void setup()
{
pinMode(test,INPUT);
pinMode(sendbutton,INPUT);
pinMode(13,OUTPUT);
Serial.begin(19200); // Open serial communications and wait for port to open:
delay(1500);
mySerial.begin(19200);
delay(1500);
Serial.println("Modem connection is on!");
}


this is the code:

void sendsbdi()
{//start of sendsbdi
char  full [42];
Serial.println("sending AT+SBDI");
mySerial.println("AT+sbdi");
delay(16000);
if  (mySerial.available()> 41)
for (int csq_num=0;csq_num<41;csq_num++)

Serial.println(full);
delay (1000);
Serial.println("test2..........");
char * pch;
int i=0;
char *david[6];

printf("spliting string \"%s\" into token: \n" ,full); //the part of cutting the answer
pch = strtok( full, ",");
while (pch !=NULL)
{
printf("%s\n",pch);
pch=strtok(NULL,",");
david[i]=pch;
i++;

}// end of cutting the answer
for (int t=0;t<5;t++)
{//start of printing
Serial.print(t);
Serial.print ("-   ");
Serial.println(david[t]);
}//end of printing

}//end of sendsbdi


so what is wrong now?

if this is true - I still don't see any print in the serial.- but this is not a problem.

Yes, it is.

Although, I don't see how you have created a relationship between stdout (that printf writes to) and the serial port.

Use sprintf() to write to a buffer, and Serial.print() to print the buffer, or just use Serial.print().

O.K. (thanks ) how do I created a relationship between them? if I try only Serial.println("spliting string \"%s\" into token: \n" ,full); //the part of cutting the answer I get error this error call of overloaded 'println(const char [34],char [44])' is ambiguous

The Serial print methods are not like the usual C printf. You can't use format strings like that.

The Serial print methods are not like the usual C printf.
You can’t use format strings like that.

True, but OP could:

char buff[160];
sprintf(buff, "spliting string \"%s\" into token: \n" ,full);
Serial.print(buff);


which WOULD support formatted printing.

Or, of course, the simpler:

Serial.print("spliting string \"");
Serial.print(full);
Serial.println("\" into tokens");