Pages: [1]   Go Down
Author Topic: RLFIN Command always returns 0 value to ardunio  (Read 881 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* gobetwino.jpg (126.86 KB, 700x457 - viewed 24 times.)
Logged

Belgium
Offline Offline
Full Member
***
Karma: 6
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can we see the whole sketch please? In code tags? How to use this forum

Once you call "query", how do you know if serial was available? Is linequery just some undefined value then?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here it is:

Code:
#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
Online Online
Brattain Member
*****
Karma: 601
Posts: 48566
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?

Code:
      errormsg = String(Serial.read());
How useful. A one character error message.

Code:
      errormsg.toCharArray(error, 160);
That is then extracted and stored in a 160 character array.

Code:
return (error);
Another function that returns a global variable.

Code:
      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
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Crashes


Also you might want to read: http://www.gammon.com.au/serial
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry. >.< I just said I am noob. Well, thanks btw. Still didn't get the answer to what I am asking.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12782
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Please don't cross-post.  Topics merged.
« Last Edit: March 08, 2013, 03:48:01 am by Coding Badly » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: