Hello ,
I want to know how do I add to a string I get ,1 char-comma ,
.
so if the string I have is
String1=[1,2,3,4]
I want the new string will be :
String2=[,1,2,3,4]
Thanks ,
Hello ,
I want to know how do I add to a string I get ,1 char-comma ,
.
so if the string I have is
String1=[1,2,3,4]
I want the new string will be :
String2=[,1,2,3,4]
Thanks ,
There is a difference between a String and a string. How are String1 and String2 declared and initialised in your program ?
Please post your code so that the context of your question can be more easily understood.
sure.
and I think(well I know) I made mistake -first I need to extract data from pointer and then add to it 1 char.
this is the code
#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);
Serial.println("Ready to work!");
mySerial.begin(19200);
delay(1500);
Serial.println("Modem connection is on!");
}
void loop()
{//start of loop
if (digitalRead(test)==HIGH)
{//start of push
char full [44];
Serial.println("sending AT+SBDI");
mySerial.println("AT+sbdi");
delay(16000);
if (mySerial.available()!=0)
{//start of answer
for (int num=0;num<43;num++)
{//start reading
full[num]=mySerial.read();
Serial.print(num);
Serial.print(" ");
Serial.println(full[num]);
}//end of reading
// Serial.println(full);
delay (1000);
Serial.println(".....................");
char * pch;
int i=0;
char *david[6];
pch = strtok( full, ",");
while (pch !=NULL)
{
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
Serial.println("*****end of sbdi******");
}//end of reading
}//end of push
}//end of loop
this is the full answer I'm getting
Ready to work!
Modem connection is on!
sending AT+SBDI
0 A
1 T
2 +
3 s
4 b
5 d
6 i
7 89 10
11 +
12 S
13 B
14 D
15 I
16 :
17
18 2
19 ,
20
21 7
22 1
23 ,
24
25 2
26 ,
27
28 0
29 ,
30
31 0
32 ,
33
34 0
35
3637
3839 O
40 K
41
42.....................
0- 71
1- 2
2- 0
3- 0
4- 0OK
end of sbdi*
As you can see I don't get the first number - so I thought that if I will save the answer in another string and then add to him ',' - I will be able to see all 6 answers - and from there I will be able to do what I want according to the number.
Thanks ,
You have done the classic error:-
if (mySerial.available()!=0)
{//start of answer
for (int num=0;num<43;num++)
{//start reading
full[num]=mySerial.read();
You see if there is one or more byte to read and then you go and read 43 bytes if they are in the buffer or not.
I know that the answer will be 44 chars - for sure.
david1234:
I know that the answer will be 44 chars - for sure.
... but you don't know if all those 44 chars are in the serial buffer or not. You only know that there is at least one (!=0), and then you go to read all of them whether they've arrived or not.
I understand what you are saying -
I have change it to -
if (mySerial.available()>43)
{//start of answer
for (int num=0;num<43;num++)
{//start reading
full[num]=mySerial.read();
work the same , but I understand the different.
now about my question - after I have read all 44 chars how do I take from the pointer the data and move it into a string , and them add the string ',' so i will be able to work with it?
Thank ,
(mySerial.available() > 0)
The reason you dont see the first number is because your discarding it, then go onto the next one.
char david[6];
pch = strtok( full, ","); //first number
/ ??? / //Where do you save the first number?
while (pch !=NULL)
{
pch=strtok(NULL,",");//second number
david=pch; //save it*
- i++;*
[/quote]
It should be
```- char david[6];
pch = strtok( full, ","); //first number
david[i] = pch; // save it, make sure i = 0
while (pch !=NULL)
{
pch=strtok(NULL,","); //second number
david[i + 1] = pch; // increment space, and save
i++;
```
did it -
but I still get only 5 (0-4) and the first one is garbage or empty .
david[ i ] = pch; // save it, i = 0
while (pch !=NULL)
{
i++; // moved, i = 1
pch=strtok(NULL,","); //second number
david[ i ] = pch; // increment space, and saved to david[1.....6]
}
You may need to increase the size of char "*david[6];" to maybe 8
Also your only printing out 0 - 4 from,
for (int t=0;t<5;t++) //0 - 4
{//start of printing
Serial.print(t);
Serial.print ("- ");
now I'm lost - it's start to give me garbage answer
no order in the answer.
but now I have try to do something else
I have rip the answer after ":"
and I get this:
2, 93, 2, 0, 0, 0
OK
how do I rip from here ?
this is the code
#include <SoftwareSerial.h>
#include <stdio.h>
SoftwareSerial mySerial(10, 11); // RX, TX
int test=7;
int time=0;
void setup()
{
pinMode(test,INPUT);
pinMode(13,OUTPUT);
Serial.begin(19200); // Open serial communications and wait for port to open:
delay(1500);
Serial.println("Ready to work!");
mySerial.begin(19200);
delay(1500);
Serial.println("Modem connection is on!");
}
void loop()
{//start of loop
char full [44];
Serial.println("sending AT+SBDI");
mySerial.println("AT+sbdi");
delay(16000);
if (mySerial.available()>43)
{//start of answer
for (int num=0;num<43;num++)
{//start reading
full[num]=mySerial.read();
Serial.print(num);
Serial.print(" ");
Serial.println(full[num]);
}//end of reading
// Serial.println(full);
delay (1000);
Serial.println(".....................");
char * pch;
int i=0;
char *david[6];
pch = strtok( full, ":");
//david[i]=pch;
while (pch !=NULL)
{
pch=strtok(NULL,":");
david[i]=pch;
i++;
}// end of cutting the answer
Serial.println(david[0]);
Serial.println("*****end of sbdi******");
}//end of reading
Serial.print("how many times - ");
Serial.println(time);
Serial.println("*************");
time++;
delay(30000);
}//end of loop
I have it on david[0] - so how do I move it from the pointer into a char ? and rip the answer after
Thanks ,
full isn't explicitly null terminated.
strtok() expects a null terminated char array.
pch = strtok( full, ":");
So, pch now points to the stuff after the ':'.
while (pch !=NULL)
{
pch=strtok(NULL,":");
david[i]=pch;
There are no more :'s are there?
You seem to be missing the fact that changing what the pointer points to is not the same as changing where the pointer is located. You end up with david[] being an array of pointers - all the same, all pointing to whatever pch last pointed to. If that is not what you want, and I doubt that it is, you need to make a copy of the pointed to data, using strdup(), not a copy of the pointer.
Assuming that you get the delimiters squared away, that is.
I think it's time to step back and look at just what you're doing.
You have a string
+SBDI: 2,71, 2, 0, 0, 0
And you want to get the numbers from it into, ultimately, integers, yes?
Am I the only one to have noticed that this is a fixed format string? The single digits are prefixed with a space, the double digits not?
Do you really need to go through so much hastle playing with strtok when you can just look at specific locations within the string, and set commas to NULL?
int numbers[6];
char *incoming = "+SBDI: 2,71, 2, 0, 0, 0";
unsigned char i;
for (i=0; i<6; i++) {
incoming[8+(i*3)] = 0;
numbers[i] = atoi(incoming+6+(i*3));
}
for (i=0; i<6; i++) {
Serial.print(i);
Serial.print(": ");
Serial.println(numbers[i]);
}
(untested)
" PaulS "
let me see if I got this :
strdup - take the data the pointer point at and save it into a string\char?
if so - how do I use this function? and what do i need to config at first?
about "majenko" post
the answer is changing - this is the form but the numbers are changing all the time , I don't know what the numbers will be.
Thanks,
Your chars "+SBDI: 2,71, 2, 0, 0, 0"
If you want everything after : then us an if statement to look at the chars, and if it sees ' : ', start storing the chars after it.
let me see if I got this :
strdup - take the data the pointer point at and save it into a string\char?
if so - how do I use this function?
It makes a copy of the data, and returns a pointer to the copy.
david[i] = strdup(pch);
You will need to free() all the copies later, to avoid leaking memory.
The numbers will change, but will they ever be more than 2 digits? Why have the space padding in there if you sometimes get 3 digit? My guess (somewhat educated) is the numbers will always be between 0 and 99, and because of the space padding on the numbers 0 to 9 the string will always be in the same format.
PaulS:
let me see if I got this :
strdup - take the data the pointer point at and save it into a string\char?
if so - how do I use this function?It makes a copy of the data, and returns a pointer to the copy.
david[i] = strdup(pch);
You will need to free() all the copies later, to avoid leaking memory.
If you use strcpy( buffer1, buffer2 ) to copy the string in buffer2 to buffer1 then you won't need to use free() and can avoid blindly shotgunning your heap. That may not mean much on a PC but on an ATMega328 with 2k RAM it's a pretty good idea.
Here's one way to capture data incoming over time.
#define BUFFLEN 64 // big enough chunk of 2k ram?
char buff[BUFFLEN];
char *cp = buff;
byte flag = 0;
void clearbuff( void )
{
memset( buff, 0, 64 );
}
void setup( void )
{
Serial.begin( 9600 );
clearbuff();
}
void loop( void )
{
if ( Serial.available())
{
*(cp++) = Serial.read(); // cp is address to fill, ++ increments cp after it's used.
// cp is the pointer, *cp is the memory that cp points to
if (( cp - buff >= BUFFLEN ) || ( *(cp - 1) == '\n' )) // bounds check or newline
{
flag = 1;
}
}
if ( flag ) // same as if ( flag != 0 ) or if ( flag > 0 ) but less typing
{
// you might be using strstr() in your processing here
Serial.println( buff ); // print out what you got
clearbuff(); // re-initialize for more
cp = buff;
flag = 0;
}
}
Note how simple it would be to add other tasks?