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!
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.
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!
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.
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
}
}
}
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?