Go Down

Topic: Serial Input Basics Tutorial from Robin2 - Example Modification (Read 103 times) previous topic - next topic

Hilarius1

Hello everyone!

I would like to modify example #5 in above mentioned tutorial (programming questions/useful links/general design/communications/serial input basics - Apr. 25/2016) in that way, that it receives only 2 integers instead of each one text, integer and floting point variable.

In the function  void parseData()  I removed the 2 lines concerning the float vriables and I added 2 lines for a 2nd integer variable; with this I´m able to receive 1 text and 2 integers as expected.

As soon as I remove the 2 lines concerning the text message, the received data (from the 2 input integers) becomes 0 0 .

I suspect the fault around  the expression  char * strtokIndx , but my problem is, that I´m not all familiar with the real C-Language. Can somebody show me the right way?

Hilarius1

PaulS

Quote
Can somebody show me the right way?
Sure. You show us the wrong way, and we'll help you make it right.

Robin2

What @PaulS means is that you should post your version of the program so we can see exactly what changes you have made.

And please use the code button </>
Code: [Select]
so your code looks like thisand is easy to copy to a text editor

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Hilarius1

Here is the code.
Code: [Select]
[code]

// receive data with start- and end-markers combined with parsing

const byte numChars=32;
char receivedChars[numChars];
char tempChars[numChars];   //temporary array for use with parsing

//variables to hold the parsed data
char messageFromPC[numChars]={0};
int integerFromPC=0;
int bintegerFromPC=0;
//float floatFromPC=0.0;

boolean newData=false;

//*****************************************

void setup()
{
  Serial.begin(9600);
  Serial.println("This demo expects input of 2 integers");
  Serial.println("Enter data in this style <243, 547>");
}
//*****************************************

void loop()
{
  recvWithStartEndMarkers();
  if (newData==true)
  {
    strcpy(tempChars, receivedChars);//this temporary copy is necessary to protect the
    //original data, because strtok() in parseData() replaces the commas with  \0

    parseData();
    showParsedData();
    newData=false;
  }
}
//*****************************************
void recvWithStartEndMarkers()
{
  static boolean recvInProgress=false;
  static byte ndx=0;
  char startMarker='<';
  char endMarker='>';
  char rc;

  while (Serial.available()>0 && newData==false)
  {
    rc=Serial.read();

    if(recvInProgress==true)
    {
      if (rc !=endMarker)
      {
        receivedChars[ndx]=rc;
        ndx++;
        if(ndx>=numChars)
        {
          ndx=numChars-1;
        }
      }
      else
      {
        receivedChars[ndx]='\0';//terminate the string
        recvInProgress=false;
        ndx=0;
        newData=true;
      }
    }
    else if (rc==startMarker)
    {
      recvInProgress=true;
    }
  }
}
//**************************************

void parseData()//split data into its parts (text, integer...)
{
  char * strtokIndx;//this is used by strtok() as an index

 // strtokIndx=strtok(tempChars,",");//get the first part - the string
 // strcpy(messageFromPC, strtokIndx);//copy it to messageFromPC

  strtokIndx=strtok(NULL,",");//this continues, where the previous call left off
  integerFromPC=atoi(strtokIndx);//convert this part to an integer

  strtokIndx=strtok(NULL,",");//my 2nd integer;
  bintegerFromPC=atoi(strtokIndx);

  //strtokIndx=strtok(NULL,",");//float eliminated
  //floatFromPC=atof(strtokIndx);//convert this part to a float
}
//***************************************

void showParsedData()
{
 //Serial.print("Message ");
 //Serial.println(messageFromPC);
 
  Serial.print("Integer ");
  Serial.println(integerFromPC);
  Serial.print("bInteger ");
  Serial.println(bintegerFromPC);

  //Serial.print("Float ");
  //Serial.println(floatFromPC);
}


[/code]

PaulS

Code: [Select]
  Serial.println("This demo expects input of 2 integers");
Is that what you get?

Code: [Select]
  if (newData==true)
  {
    // Find out what you got
    Serial.print("Received: [");
    Serial.print(receivedChars);
    Serial.println("]");

    strcpy(tempChars, receivedChars);//this temporary copy is necessary to protect the


Code: [Select]
// strtokIndx=strtok(tempChars,",");//get the first part - the string
 // strcpy(messageFromPC, strtokIndx);//copy it to messageFromPC

You commented out the code that tells strtok() to start parsing some array of data.

Code: [Select]
  strtokIndx=strtok(NULL,",");//this continues, where the previous call left off
Then you tell it to keep parsing the same data. But, you haven't told it what to parse...

PaulS

In the parseData() function, Robin2 was parsing the string to get three tokens. You want to get two tokens. How YOU handle the two tokens will be different from how Robin2 was handling the three tokens. But, the process of getting the tokes remains the same.

Robin2

I think you commented out the wrong line. Try this
Code: [Select]
void parseData()//split data into its parts (text, integer...)
{
  char * strtokIndx;//this is used by strtok() as an index

 strtokIndx=strtok(tempChars,",");//get the first part - the string
 // strcpy(messageFromPC, strtokIndx);//copy it to messageFromPC

  // strtokIndx=strtok(NULL,",");//this continues, where the previous call left off
  integerFromPC=atoi(strtokIndx);//convert this part to an integer

  strtokIndx=strtok(NULL,",");//my 2nd integer;
  bintegerFromPC=atoi(strtokIndx);

  //strtokIndx=strtok(NULL,",");//float eliminated
  //floatFromPC=atof(strtokIndx);//convert this part to a float
}


Note the the first call to strtok() is different from the others. You commented out the first one.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Hilarius1

Now I got it working the right way and can continue with my project, - great!

Thank you both for your great help, an additional appreciation to Robin2 for the nice tutorial.

Best regards

Hilarius1












Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy