Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: March 07, 2013, 04:41:56 pm » |
char query() { Serial.println("#S|QUERY|[1]#"); //RLFIN Gobetwino if (Serial.available()) { linequery = char(Serial.read()); //Read the file Serial.println(linequery); //Print the file (For checking purpose only) } return (linequery); }
I'll be using the return value of method query for this:
if (linequery == '0') { sms.SendSMS(number, "abc"); } else if (linequery == '1') { sms.SendSMS(number, "def"); } else if (linequery == '2') { sms.SendSMS(number, "ghi"); } else if (linequery == '3') { sms.SendSMS(number, "jkl"); }
But after returning the value of the read file to arduino, it always gets 0. I think, somehow, the arduino misinterpret the data. I also thought it might be the data type that I am using for linequery. Any suggestions/idea/solution? Please help. thanks!
Here is a photo of the Gobetwino serial monitor.
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 4
Posts: 135
|
 |
« Reply #1 on: March 07, 2013, 04:47:39 pm » |
I feel like I'm missing something, like some background info ?
|
|
|
|
|
Logged
|
Gear: Arduino- Uno,Due,Ethernet, OLS, Buspirate, J-Link, TDS1002, Rigol DG1022
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #2 on: March 07, 2013, 05:05:02 pm » |
Oh, I'm sorry. I am using a php file in editing and putting values on the text files. That query.txt may contain 0,1,2 or 3. Here's one part of my code: $openquerycmd = fopen("query.txt","w"); $sendquerycmd = fwrite($openquerycmd, '3'); fclose($openquerycmd); After that, the value of query.txt should be 3. But after execution of RLFIN and returning the value to arduino, it always prints 0. Particularly what data type is it of a text file? char? string? I don't know if that is the background you are asking. Sorry, noob here @.@
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: March 07, 2013, 05:13:28 pm » |
Can we see the whole sketch please? In code tags? How to use this forumOnce you call "query", how do you know if serial was available? Is linequery just some undefined value then?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #4 on: March 07, 2013, 05:16:21 pm » |
Here it is: #include "SIM900.h" #include <SoftwareSerial.h> #include "sms.h" SMSGSM sms;
boolean started=false; char position; char read; char message[160]; char number[20]; char linequery; String errormsg; char error[160]; char ticket; String successtxt; char txtgen[160];
void setup() { Serial.begin(9600); if (gsm.begin(2400)) { Serial.println("\nstatus=READY"); started=true; } else { Serial.println("\nstatus=IDLE"); } }
char query() { Serial.println("#S|QUERY|[1]#"); if (Serial.available()) { linequery = char(Serial.read()); Serial.println(linequery); } return (linequery); }
char* stockerror() { Serial.println("#S|STOCKERR|[1]#"); if (Serial.available()) { errormsg = String(Serial.read()); errormsg.toCharArray(error, 160); Serial.println(error); } return (error); }
char* orderticket() { Serial.println("#S|TXTGEN|[1]#"); if (Serial.available()) { ticket = char(Serial.read()); successtxt = "Order successful! Your ticket order is: "; successtxt += ticket; successtxt.toCharArray(txtgen, 160); Serial.println(txtgen); } return (txtgen); }
void loop() { if(started) { position = sms.IsSMSPresent(SMS_UNREAD); read = sms.IsSMSPresent(SMS_READ); if(position == 1) { sms.GetSMS(position, number, message, 160); Serial.print("#S|VERIFYNUM|["); Serial.print(number); Serial.println("]#"); Serial.print("#S|MSGLOG|["); Serial.print(message); Serial.println("]#"); Serial.println("#S|STARTPHP|[]#"); query(); if (linequery == '0') { sms.SendSMS(number, "This is Piyeshop query and ordering system. For more info, visit thesis.lezworld.com.ph"); } else if (linequery == '1') { sms.SendSMS(number, "Your query is invalid. Please refer to the ordering manual."); } else if (linequery == '2') { sms.SendSMS(number, stockerror()); } else if (linequery == '3') { sms.SendSMS(number, orderticket()); } } else if(position > 1) { for(int i = 1; i <= position; i++) { sms.GetSMS(position, number, message, 160); Serial.print("#S|VERIFYNUM|["); Serial.print(number); Serial.println("]#"); Serial.print("#S|MSGLOG|["); Serial.print(message); Serial.println("]#"); Serial.println("#S|STARTPHP|[]#"); query(); if (linequery == '0') { sms.SendSMS(number, "This is Piyeshop query and ordering system. For more info, visit thesis.lezworld.com.ph"); } else if (linequery == '1') { sms.SendSMS(number, "Your query is invalid. Please refer to the ordering manual."); } else if (linequery == '2') { sms.SendSMS(number, stockerror()); } else if (linequery == '3') { sms.SendSMS(number, orderticket()); } } } } sms.DeleteSMS(read); delay (10000); }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 312
Posts: 35483
Seattle, WA USA
|
 |
« Reply #5 on: March 07, 2013, 05:33:46 pm » |
char query() { Serial.println("#S|QUERY|[1]#"); if (Serial.available()) { linequery = char(Serial.read()); Serial.println(linequery); } return (linequery); }
Why does this function return a global value? Why is the variable in parentheses in the return statement? Why did you feel it necessary to cast the value from Serial.read() to a char? errormsg = String(Serial.read()); How useful. A one character error message. errormsg.toCharArray(error, 160); That is then extracted and stored in a 160 character array. return (error); Another function that returns a global variable. ticket = char(Serial.read()); successtxt = "Order successful! Your ticket order is: "; You are going to run out of ticket numbers after you sell 10 tickets, if the first one is 0. You are pissing memory away right and left. On a platform with 2048 bytes of memory, that is really not a good idea. Get rid of EVERY instance of the String class, and learn to do it like a big boy - using the char arrays without crutches.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: March 07, 2013, 05:47:33 pm » |
To put it a bit more gently ... Please note that, at present, the String library has bugs as discussed here and here. In particular, the dynamic memory allocation used by the String class may fail and cause random crashes. I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example. Alternatively, install the fix described here: Fixing String CrashesAlso you might want to read: http://www.gammon.com.au/serial
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #7 on: March 08, 2013, 01:53:18 am » |
Sorry. >.< I just said I am noob. Well, thanks btw. Still didn't get the answer to what I am asking.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #8 on: March 08, 2013, 03:23:54 am » |
I have a php code which will log 0, 1, 2, or 3 to a text file (Query.txt). After execution, the value is successfully logged on the txt file. I have then an arduino code which prints #S|QUERY|[1]# to command gobetwino to read query.txt. it is successfully read and then returns a value to arduino using variable linequery. I have then a serial.println(linequery) and it prints a value of 0 instead of the right value. More confusing is that the command output window in gobetwino prints the right value. What do you think is the problem? Can someone help me? thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 118
Posts: 10149
|
 |
« Reply #9 on: March 08, 2013, 03:45:30 am » |
Please don't cross-post. Topics merged.
|
|
|
|
« Last Edit: March 08, 2013, 03:48:01 am by Coding Badly »
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6368
-
|
 |
« Reply #10 on: March 08, 2013, 08:47:19 am » |
What do you think is the problem?
The rule you have set up in Gobetwino causes it to send a line of text when you send it the command string. However, your code only reads back the first character of any response. You need to keep reading from the serial port until you have received the complete response line.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #11 on: March 08, 2013, 09:59:12 am » |
Peter, I don't have any idea how to do that. Can you give some example? Is it some sort of an EOF command?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6368
-
|
 |
« Reply #12 on: March 08, 2013, 11:42:33 am » |
You need to keep reading from the serial port until you have received the complete response. Typically that would be a line of text terminated by a carriage return and/or new line character. So you can simply keep reading until you receive one of those. The code could look something like this: const int buflen = 80; // length of the longest line you expect to receive char buf[buflen]; int len = 0;
if(Serial.available()) { char c = Serial.read(); if((c == '\n') || (c == '\r')) { if(len > 0) { handleCommand(buf); len = 0; } } else { if(len < buflen) { buf[len++] = c; buf[len] = 0; // append null terminator } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #13 on: March 09, 2013, 03:30:07 am » |
Peter, I already got it by using while loop until he gets the last byte whenever serial is available. but here's another problem. the serial.read() seems to read the previous logs on the serial. that's why when the serial is initially null, the first process returns null. the second process returns the output for the first process. and consecutively for the next processes. can u help me out?
|
|
|
|
|
Logged
|
|
|
|
|
|