how can i do split string function?
example string "NS:212:Open:S1233"
array[0] = "NS";
array[1] = "212";
array[2] = "Open";
array[3] = "S1233";
i don't know how to write code.
how can i do split string function?
example string "NS:212:Open:S1233"
array[0] = "NS";
array[1] = "212";
array[2] = "Open";
array[3] = "S1233";
i don't know how to write code.
Look up the strtok() function. There are many examples.
The easiest way is to store it as a char array, and look for a terminatind character to tell the arduino to stop storing and start to split it.
Look in strtok().
Working Example:
char data[30];
char temp;
int count = 0;
//inside loop()
temp = Serial.read(); //read in string char by char "NS:212:Open:S1233." better to add a period at the end
if(temp != '.') { //keep storing data until a period is found
data[count] = temp; // store chars in array
count++; //update array
}
else {
array[0] = strtok(data, ":. "); // split by ":" or "." or blank space " "
array[1] = strtok(NULL, ":. ");
array[2] = strtok(NULL, ":. ");
array[3] = strtok(NULL, ":. ");
count = 0; // reset count
}
A little old post but im having problem with this. Im getting a string between [] chars. That works good (if i receive [123:ABC:321]) inString prints 123:ABC:321, then i want to split with ":" and trying to use this code.
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
if (inChar == '[') { // '<' Inicio de la cadena de caracteres de interes
startRead = true;
} else if(startRead){
if(inChar !=']'){ // '>' Fin de la cadena de interes
inString[stringPos]=inChar;
stringPos++;
}
else {
rx1 = strtok(inString, ":");
rx2 = strtok(NULL, ":");
rx3 = strtok(NULL, ":");}
}
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
The error its
error: incompatible types in assignment of 'char*' to 'char [4]'
I have the variables declared in this way
char rx1[4],rx2[4],rx3[4];
Post your full code.
I have the variables declared in this way
strtok() returns a pointer. You can strcat() the data pointed to by the pointer to those arrays. You can not assign it using the assignment operator the way you are trying to do it.
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];
stringPos=0;
memset( &inString, 0, 32 ); //clear inString memory
Serial.print(F("conectando\n"));
if(client.connect(ipBuf,thisPort))
{
Serial.println(F("conectado"));
sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("fallo"));
return 0;
}
// connectLoop controls the hardware fail timeout
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
if (inChar == '[') { // '<' Inicio de la cadena de caracteres de interes
startRead = true;
} else if(startRead){
if(inChar !=']'){ // '>' Fin de la cadena de interes
inString[stringPos]=inChar;
stringPos++;
}
else {
rx1 = strtok(inString, ":");
rx2 = strtok(NULL, ":");
rx3 = strtok(NULL, ":");
}
}
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("timeout"));
client.stop();
startRead = false;
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println(F("desconectado"));
// close client end
client.stop();
startRead = false;
//Serial.write(inString);
Serial.write(rx1);
Serial.write(rx2);
Serial.write(rx3);
return 1;
}
This is the full function, it reads from a txt on web this: [123:ABC:321]
have solved the error declaring rx1 rx2 rx3 in this way
char * rx1;
char * rx2;
char * rx3;
Now the question its how to convert this char* to int
Now the question its how to convert this char* to int
Gee, I wonder what atoi() would do...
PaulS:
Now the question its how to convert this char* to int
Gee, I wonder what atoi() would do...
Im not sure if this will be in a new thread but, having problem using function atoi:
Trying this:
The entry is: [20:344:321]
The output its:
20
344
321
(Blank space here where its supossed to be the output of Serial.write(intrx1)) sometimes it looks like a '{' character
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];
stringPos=0;
memset( &inString, 0, 32 ); //clear inString memorySerial.print(F("conectando\n"));
if(client.connect(ipBuf,thisPort))
{
Serial.println(F("conectado"));sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("fallo"));
return 0;
}// connectLoop controls the hardware fail timeout
int connectLoop = 0;while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
if (inChar == '[') { // '<' Inicio de la cadena de caracteres de interes
startRead = true;
} else if(startRead){
if(inChar !=']'){ // '>' Fin de la cadena de interes
inString[stringPos]=inChar;
stringPos++;
}
else {
** rx1 = strtok(inString, ":");**
** rx2 = strtok(NULL, ":");**
** rx3 = strtok(NULL, ":");**}
}
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("timeout"));
client.stop();
startRead = false;
}
// this is a delay for the connectLoop timing
delay(1);
}Serial.println();
Serial.println(F("desconectado"));
// close client end
client.stop();
startRead = false;
//Serial.write(inString);
Serial.println("\n");
Serial.write(rx1);
Serial.println("\n");
Serial.write(rx2);
Serial.println("\n");
Serial.write(rx3);
Serial.println("\n");
intrx1=atoi(rx1);
Serial.write(intrx1);
return 1;
}
Mixing Serial.write() and Serial.print() statements to print all ASCII data seems strange.
Printing values without identifying what is being printed seems useless.
Instead of three pointers, you only need one. When the pointer is returned by strtok(), call atoi() immediately. Then, reuse the pointer.
Serial.write() of an int does not output a string representation of the value. It sends the value as binary data. The receiver (Serial Monitor?) is probably what is having problems displaying the binary data.
PaulS:
Mixing Serial.write() and Serial.print() statements to print all ASCII data seems strange.Printing values without identifying what is being printed seems useless.
Instead of three pointers, you only need one. When the pointer is returned by strtok(), call atoi() immediately. Then, reuse the pointer.
Serial.write() of an int does not output a string representation of the value. It sends the value as binary data. The receiver (Serial Monitor?) is probably what is having problems displaying the binary data.
Changes:
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];
stringPos=0;
memset( &inString, 0, 32 ); //clear inString memory
Serial.print(F("conectando\n"));
if(client.connect(ipBuf,thisPort))
{
Serial.println(F("conectado"));
sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("fallo"));
return 0;
}
// connectLoop controls the hardware fail timeout
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
if (inChar == '[') { // '<' Inicio de la cadena de caracteres de interes
startRead = true;
} else if(startRead){
if(inChar !=']'){ // '>' Fin de la cadena de interes
inString[stringPos]=inChar;
stringPos++;
}
else {
[b] rx = strtok(inString, ":");
rx1=atoi(rx);
rx = strtok(NULL, ":");
rx2=atoi(rx);
rx = strtok(NULL, ":");
rx3=atoi(rx);[/b]
}
}
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("timeout"));
client.stop();
startRead = false;
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println(F("desconectado"));
// close client end
client.stop();
startRead = false;
//Serial.write(inString);
[b] Serial.println("\n");
Serial.println(rx1,DEC);
Serial.println("\n");
Serial.println(rx2,DEC);
Serial.println("\n");
Serial.println(rx3,DEC);[/b]
return 1;
}
Output now:
20
344
321
correct!. Thanks for the tips i will be more cautious with the use of serial.write and serial.println