Offline
Full Member
Karma: 0
Posts: 218
|
 |
« on: January 04, 2013, 02:04:27 pm » |
Dear All, I am very new with Arduino and I am trying hard to understand a code wich read the serial port. Could you help me on it? My goal is read this: if(cell.available() > 0){ incoming_char=cell.read(); //Get the character from the cellular serial port. Serial.println(incoming_char); //Print the incoming character to the terminal. if (incoming_char == '\n') { Serial.println("[n]"); } if (incoming_char == '\r') { Serial.println("[r]"); } if (incoming_char == '\O') { Serial.println("[00]"); }
} and to collect all data, in a array or varibale, such +SIND: 1 +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1 +SIND: 11 +SIND: 3 +SIND: 4 (and other)
I also would like to Serial.print() the message matching to those code 0 SIM card removed 1 SIM card inserted 2 Ring melody 3 AT module is partially ready 4 AT module is totally ready 5 ID of released calls 6 Released call whose ID=<idx> 7 The network service is available for an emergency call 8 The network is lost 9 Audio ON 10 Show the status of each phonebook after init phrase 11 Registered to network
I also, I would like to run the GSM/GPS communication when +SIND: 4. For now I found that code that I can not understand, or, let's say translate and explain to a human: static void readATString(boolean watchTimeout = false) {
char buffidx = 0; int time = millis(); while (1) { int newTime = millis();
if (watchTimeout) { // Time out if we never receive a response if (newTime - time > 30000) error(ERROR_GSM_FAIL); } if (cell.available() > 0) { incoming_char = cell.read(); if (incoming_char == -1) { at_buffer[buffidx] = '\0'; return; } if (incoming_char == '\n') { continue; } if ((buffidx == BUFFSIZE - 1) || incoming_char == '\r') { at_buffer[buffidx] = '\0'; return; } at_buffer[buffidx++] = incoming_char; } } }
How is printed and stored the value of at_buffer[buffidx]? In other word, I would like to have somethink like this. if (something == "+SIND:0") Serial.print("SIM card remove"); if (something =="+SIND:1") Serial.print("SIM card inserted"); if (something == "+SIND:3") Serial.print("AT module is partially ready"); if (something == "+SIND:4") Serial.print("AT module is totaly ready"); if (something == "+SIND:10") Serial.print("message"); if (something == "+SIND:11") Serial.print("Registered to network");
Someone can explain me or provide me an exemple? Many thank for your great help, I loat all my afternoon on this.... :o| Cheers and happy new year!!
|
|
|
|
« Last Edit: January 04, 2013, 02:47:11 pm by pierrot10 »
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 144
Posts: 19382
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: January 04, 2013, 02:11:26 pm » |
if ("+SIND:1") Serial.print("SIM card inserted"); As you've probably already found, "+SIND:1" is always true. You need to look at "strcmp"
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36485
Seattle, WA USA
|
 |
« Reply #2 on: January 04, 2013, 02:16:55 pm » |
} // Here you need an else statement with a block that stores the character // in the next element in an array }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 218
|
 |
« Reply #3 on: January 04, 2013, 03:11:10 pm » |
Thank for help, I understand a litle bit more. I would really appreciate if you can help me to understand how work this and how the data are store in at_buffer. I ma confused particularely with the \r (Line new) and \n (return) and \0. How the loop run and at with step data are store in at_buffer.... static void readATString(boolean watchTimeout = false) { char c; char buffidx = 0; int time = millis(); while (1) { int newTime = millis(); // after 30sec it retun a error (not usefull for now) if (watchTimeout) { // Time out if we never receive a response if (newTime - time > 30000) error(ERROR_GSM_FAIL); } if (cell.available() > 0) { c = cell.read(); // If not available ....... if (c == -1) { at_buffer[buffidx] = '\0'; //? return; } if (c == '\n') { continue; } if ((buffidx == BUFFSIZE - 1) || c == '\r') { at_buffer[buffidx] = '\0'; return; } at_buffer[buffidx++] = c; } } }
Is there not a print_r() to print at_buffer, like in PHP? Many thank for your help
|
|
|
|
« Last Edit: January 04, 2013, 03:15:13 pm by pierrot10 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36485
Seattle, WA USA
|
 |
« Reply #4 on: January 04, 2013, 04:10:06 pm » |
if (cell.available() > 0) { c = cell.read(); // If not available ....... if (c == -1) {
If there is data to read, read the first character. How can you not have read something? Is there not a print_r() to print at_buffer, like in PHP? No, and why should there be? C++ allows for function overloading. The print() method can print a lot of different things, like ints, floats, bytes, chars, and, you guessed it, arrays of chars.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 218
|
 |
« Reply #5 on: January 05, 2013, 07:02:17 am » |
Hello I have some big trouble to understand how work SoftwareSerial. I understood that I have to add those line: SoftwareSerial cell(GPRS_TX, GPRS_RX); //int GPRS_TX = 2; int GPRS_RX = 3; Then I can run taht condition: if (cell.available() > 0) { incoming_char = cell.read(); }
But now I have big difficulties ti understand how to store and work specialy with those value +SIND: 1 +SIND: 10,"SM",1,"FD",1,"LD",1,"MC",1,"RC",1,"ME",1 +SIND: 3 +SIND: 4 +SIND: 7 +SIND: 11 I am trying to do something like this: if (incoming_char == "+SIND:0") Serial.print("SIM card remove"); if (incoming_char =="+SIND:1") Serial.print("SIM card inserted"); if (incoming_char == "+SIND:3") Serial.print("AT module is partially ready"); if (incoming_char == "+SIND:4") Serial.print("AT module is totaly ready"); if (incoming_char == "+SIND:10") Serial.print("message"); if (incoming_char == "+SIND:11") Serial.print("Registered to network"); My goal is on myLoop() function. Each 10 sec, it start the loop, check the GPRS connetion and display the error (or a red led), if there are errors. I would really really appreciate if you can provide me some help. I am totaly lost. I hope my post was clear, but feel you free to ask me question for more detail Many thank
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 125
Arduino rocks
|
 |
« Reply #6 on: January 05, 2013, 07:29:32 am » |
But now I have big difficulties ti understand how to store and work specialy with those value
Your post definitely needs some clarity added to it (more of the code), and perhaps some of what you have tried? The first thing you need to test is the result of 'incoming_char'- so Serial.print it. I bet it's not the string you are testing for, I'd suggest it's just one of the characters of the string you are expecting.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 144
Posts: 19382
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: January 05, 2013, 07:35:08 am » |
Near-identical topics merged.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 218
|
 |
« Reply #8 on: January 05, 2013, 07:59:33 am » |
Here are more code void setup() { pinMode(LED_STATUS, OUTPUT); //int LED_STATUS = 13; pinMode(LED_ERROR, OUTPUT); // int LED_ERROR = 12; pinMode(GPS_RELAY, OUTPUT); // int GPS_RELAY = 9; ////Initialize serial ports for communication. Serial.begin(4800); cell.begin(9600); //Let's get started! Serial.println("Power on"); // Check LEDS blinkLed(LED_ERROR, 5, 25); blinkLed(LED_STATUS, 5, 25); // Pin, flashtime, delay }
// Main Loop void loop() { /* if (firstTimeInLoop) { firstTimeInLoop = false; } */ readATString();
Serial.println("Delay of 10 sec"); delay(10000); }
readATString is called and ... static void readATString(boolean watchTimeout = false) { const int BUFFSIZE = 180; char at_buffer[BUFFSIZE]; //char buffidx; char buffer[BUFFSIZE];
char buffidx = 0; int time = millis(); //while (strstr(at_buffer, "+SIND: 4") == 0 && strstr(at_buffer, "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1") == 0) { int i = 0; while (1) { i++; Serial.print(i); if (cell.available() > 0) { incoming_char = cell.read(); if (incoming_char == -1) { at_buffer[buffidx] = '\0'; Serial.print("--------------------------1"); return; } if (incoming_char == '\n') { Serial.println("continue"); continue; } if ((buffidx == BUFFSIZE - 1) || incoming_char == '\r') { at_buffer[buffidx] = '\0'; Serial.println("return"); return; } at_buffer[buffidx++] = incoming_char;
}else{ Serial.println("toto"); return; } } }
... and I would like that at_buffer collect the code as +SIND+ 0, >SUND+ 1 etc in order to display the marching message, as for exemple if (at_buffer == "+SIND:0") Serial.print("SIM card remove"); if (at_buffer =="+SIND:1") Serial.print("SIM card inserted"); if (at_buffer == "+SIND:3") Serial.print("AT module is partially ready"); if (at_buffer == "+SIND:4") Serial.print("AT module is totaly ready"); if (at_buffer == "+SIND:10") Serial.print("message"); if (at_buffer == "+SIND:11") Serial.print("Registered to network");
Is it no array in sketch? I do not really understanf the readATString function, how the caracter are "compiled" in at_buffer. I am very block at this point. Many thankf for your help
|
|
|
|
« Last Edit: January 05, 2013, 08:01:53 am by pierrot10 »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 125
Arduino rocks
|
 |
« Reply #9 on: January 05, 2013, 08:07:35 am » |
Is it no array in sketch?
I do not really understanf the readATString function, how the caracter are "compiled" in at_buffer.
I am very block at this point.
Many thankf for your help
You aren't using an array with this line of code: if (at_buffer == "+SIND:0") Serial.print("SIM card remove");
You were given a valid answer earlier: You need to look at "strcmp"
What did you find when you looked into strcmp?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 218
|
 |
« Reply #10 on: January 05, 2013, 08:35:13 am » |
Yes I use it. if(strcmp(at_buffer, "+SIND: 1") == 0 ) Serial.print("No SIM card"). But it does not help because I do not undertsand how work at_buffer. May be yoiu can correct my comment, here const int BUFFSIZE = 180; static void readATString(boolean watchTimeout = false) {
char buffidx = 0; int time = millis(); //while (strstr(at_buffer, "+SIND: 4") == 0 && strstr(at_buffer, "+SIND: 10,\"SM\",1,\"FD\",1,\"LD\",1,\"MC\",1,\"RC\",1,\"ME\",1") == 0) { int i = 0; while (1) { i++; Serial.print(i); // If available continue if (cell.available() > 0) { // add the first caracter to incoming_char incoming_char = cell.read(); // If there is no caracter, leave the function if (incoming_char == -1) { // This I do not understand at_buffer[buffidx] = '\0'; Serial.print("--------------------------1"); // Leave the function return; } // If caracter is a new line, continue if (incoming_char == '\n') { Serial.println("continue"); // Continue to to top of the loop continue; } // If bufidx is egal to BUFFSIZE-1 or caracter is return chariot if ((buffidx == BUFFSIZE - 1) || incoming_char == '\r') { //This i do not understand at_buffer[buffidx] = '\0'; Serial.println("return"); // Leave the loop return; } // Add the caracter to at_buffer, at possition egal to buffidx and increment buffidx at_buffer[buffidx++] = incoming_char; /* but if at_buffer is an array, I should read it like this at_buffer = { 1 => +, 2 => S, 3 => I, 4 => N, 5 => D, 6 => :, 7 => , 8 => 1 } And I thin it would be easier to have this at_buffer = { 1 => +SIND: 1, 2 => +SIND: 4, 3 => +SIND: 10 //etc. } */
// I think I need to clarefied it about at_buffer } } }
Thank a lor for your help
|
|
|
|
« Last Edit: January 05, 2013, 08:37:45 am by pierrot10 »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 218
|
 |
« Reply #11 on: January 05, 2013, 08:45:26 am » |
I am agree, I am confused... 
|
|
|
|
« Last Edit: January 05, 2013, 08:55:46 am by pierrot10 »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 125
Arduino rocks
|
 |
« Reply #12 on: January 05, 2013, 06:11:56 pm » |
You should add this to the code to get an idea of what is being seen: Serial.print(i);
Get rid of that, it's just giving you needless numbers (telling you it's looping).
incoming_char = cell.read(); Add Serial.println(incoming_char)
That'll give you a new line with the received character in Serial monitor.
The \0 at the end of at_buffer is a string null termination character.
|
|
|
|
|
Logged
|
|
|
|
|
|