Can not convert char to String?!

Hi,
In wihich situation, char array can not be convert to String?

I get the SIM return message and compare a string by below code:

String gelenSimCevap = String(simCevap);

But becaue of unknown reason I can not compare the below condition?

if (gelenSimCevap.indexOf("1,0,0,0") != -1){

}

I am sure simCevap is "1,0,0,0". But when I convert it to

String gelenSimCevap = String(simCevap);

gelenSimCevap is equal to "", empty?!

What may it be the reason?

Regards,
Mucip:)

Maybe the problem is somewhere in the code you didn't post.

what is simCevap ?

Hi,
I use Adafruit FONA library and get the AT command result by below code:

char simCevap[100];

if (fona.sendCheckReplyText("AT+CLCC", simCevap)) { 
        Serial.print("Sim Cevap: ");
        Serial.println(simCevap);
      }

Normally return is:

Sim Cevap: 1,0,3,0,0,"+905321547374",145,""

And when I hang up the phone then result is:

Sim Cevap: 1,0,0,0,0,"+905321547374",145,""

I want the control if the other party hang up the phone? And send voice file to it.

if (gelenSimCevap.indexOf("1,0,0,0") != -1) {

      fona.sendCheckReply(F("AT+DTAM=2"), ok_reply)
      fona.sendCheckReply(F("AT+CREC=4,1,0,100"), ok_reply)


}

Regards,
Mucip:)

Hi,
Please check my next post.

Regards,
Mucip:)

  char simCevap[100];

  if (fona.sendCheckReplyText("AT+CLCC", simCevap)) {
    Serial.print("Sim Cevap: ");
    Serial.println(simCevap);
  }
  char Req[7]={0};
  for (byte i = 0; i < 7; i++)Req[i] = simCevap[i];
  if (strcmp(Req, "1,0,0,0") == 0) {
    fona.sendCheckReply(F("AT+DTAM=2"), ok_reply)
    fona.sendCheckReply(F("AT+CREC=4,1,0,100"), ok_reply)
  }

Hi,
It shouldn't be const char Req[7]. Should it be?

Regards,
Mucip:)

Hi,
Same problem! It looks like it get some values from remaining buffer and put in Reg value!.
Arduino ram value usage is %87. İs this problem come from this?

Regards,
Mucip:)

On AVR's expecially, C++ Strings SUCK RAM and cycles for what? So talent-free Morons can concatenate with a + and have other More-Like-BASIC/PASCAL syntax while black-boxing text from the coder.
As far as text and text manipulation, plain old C is SUPERIOR to C++ String Objects.
If you dig, that's what you'll find.

2 Likes

ty. changed now.

Hi,
Req variable is just created. Normally in the begining it must be empty/NULL/"".
But after your for loop and println(Req)
Return value is:
Sim Metin Req: 1,0,2,0,0,547374;

Wher did it find ,547374; value, I did not understand?! :thinking:

Regards,
Mucip:)
ArduinoError

const char Def1[]={"1,0,0,0"};

void setup() {
  Serial.begin(115200);
  char simCevap[100] = {
    "0,0,0,0,0,0,0"
  };
  char Req[7] = {0};
  for (byte i = 0; i < 7; i++)Req[i] = simCevap[i];

  Serial.print("Sim Cevap: ");
  Serial.println(simCevap);
  Serial.print("Req: ");
  Serial.println(Req);

  if (strcmp(Req, Def1) == 0)Serial.println("yes\n");
  else Serial.println("no\n");

  simCevap[0] = '1';
  for (byte i = 0; i < 7; i++)Req[i] = simCevap[i];

  Serial.print("Sim Cevap: ");
  Serial.println(simCevap);
  Serial.print("Req: ");
  Serial.println(Req);

  if (strcmp(Req, Def1))Serial.println("no\n");
  else Serial.println("yes\n");
}

void loop() {}

Hi,
I disapointed unfortunatelly?!
I could not convert char to String noway! :frowning:

Where this stupid string come from?! :frowning:

I'm searching in my room if anyone make me hidden camera joke?!... :thinking:

ArduinoError

Really strange!

Regards,
Mucip:)i

Hi,
Maybe there is unwanted charecters in SimCevap which is return from SIM800.

How can I check if all the charecters in SimCevap are printable or correct?

I am sure it must be some unexpected chars in SimCevap?!

Regards,
Mucip:)

  char simCevap[100];
  fona.sendCheckReplyText("AT+CLCC", simCevap);
  for (i = 0; i < 100; i++) {
    Serial.print(i);
    Serial.print('\t');
    Serial.println(simCevap[i],DEC);
  }

Hi,
Everything looks OK. Very strange?! :thinking:

Well, why I can not convert this char to string than?! :frowning:

Tekrar sayısı: 6
---> AT+CLCC
<--- +CLCC: 1,0,0,0,0,"+905321547374",145,""
Sim Cevap: 1,0,0,0,0,"+905321547374",145,""
1,0,0,0,0,"+905321547374",145,""
0 49
1 44
2 48
3 44
4 48
5 44
6 48
7 44
8 48
9 44
10 34
11 43
12 57
13 48
14 53
15 51
16 50
17 49
18 53
19 52
20 55
21 51
22 55
23 52
24 34
25 44
26 49
27 52
28 53
29 44
30 34
31 34
32 0
33 0
34 0
35 0
36 0
37 0
38 0
39 0
40 0
41 0
42 0
43 0
44 0
45 0
46 0
47 0
48 0
49 0
Sim Metin Cevap:
Sim Metin Req: 1,0,0,0,0,547374;
Def1: 1,0,0,0
Req Metin: 1,0,0,0,0,547374;
Def1 Metin:
Cevap bekleniyor!

Regards,
Mucip:)

  char simCevap[50];
  const char Line1[7] = {49, 44, 48, 44, 48, 44, 48};
  if (fona.sendCheckReplyText("AT+CLCC", simCevap)) {
    Serial.print("SimCevap:");
    Serial.println(simCevap);
  }

  char Req[7] = {0};
  for (byte i = 0; i < 7; i++)Req[i] = simCevap[i];
  Serial.println(Req);
  Serial.println(Line1);
  if (strcmp(Req, Line1) == 0) {
    Serial.println("OK");
  }

Hi,
Yes, we can get the result with this code maybe. But we still don't know why we can not convert this char into String?! :thinking:

This is the main question unfortunatelly and still alive... :frowning:

Regards,
Mucip:)

yes we can.

Serial.begin(115200);
String LettersDef = "1234567890";
char Symbols[10] = {"1234567890"};
String Letters = Symbols;
Serial.println(Letters);
if ( LettersDef == Letters)  Serial.println("OK");
else Serial.println("not OK");
Letters = Letters.substring(0, 10);
Serial.println(Letters);
Serial.println(LettersDef);
if ( LettersDef == Letters)  Serial.println("OK");

check this one and compare

  Serial.begin(115200);
  String LettersDef = "1234567890";
  delay(1000);
  char Symbols[10] = {49,50,51,52,53,54,55,56,57,48};
  String Letters = Symbols;
  Serial.println(Letters);
  if ( LettersDef == Letters)  Serial.println("OK");
  else Serial.println("not OK");

Hi,
But all these codes can not explain why we can not get success below code?

fona.sendCheckReplyText("AT+CLCC", simCevap)  //In here,  send AT Command to SIM and get result in simCevap

String gelenSimCevap = String(simCevap); //We want to convert simCevap (Char value) into  gelenSimCevap (This is String)

String comparedString = "1,0,0,0" ; //This is what we need to check value

if(gelenSimCevap.indexOf(comparedString ) != -1){

makeAnotherJob();

}else{

//Still wait for hang up phone?

}

 

I can get the char no problem:

Serial.println(simCevap); //Result is 1,0,3,0,0,"+905321547374",145,""

but later,

String gelenSimCevap = String(simCevap);  
Serial.println(gelenSimCevap ); //Result is nothing... :frowning: 

Very strange?!

Regards,
Mucip:)