Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:46:06+00:00 2026-05-28T04:46:06+00:00

I have the following code on my Arduino that constantly checks for a serial

  • 0

I have the following code on my Arduino that constantly checks for a serial command that’s sent over TCP using a Wifly library.

What the following code does is split a string like the following when sent over serial:

{power,tv}

It sets these properties accordingly:

char command[32];
char value[32];

It then executes certain methods using sendCommand(command, value); based on the properties set in the loop below.

Keep in mind this works just fine using the Wifly library.

void loop() {
  Client client = server.available();

  if (client) {

    boolean start_data = false;
    boolean next = false;

    char command[32];
    char value[32];
    int index = 0;

    while (client.connected()) {

      if (client.available()) {
        char c = client.read();
        Serial.print(c);

        if (c == '}') {
          break;
        }

        if(start_data == true) {

          if(c != ',') {

            if(next)
              value[index] = c;
            else
              command[index] = c;

            index++;
          } else {
            next = true;
            command[index] = '\0';
            index = 0;
          }

        }

        if (c == '{') {
          start_data = true;
        }

      }

    }

    value[index] = '\0';

    client.flush();
    client.stop();

    sendCommand(command,value);
  }

}

Instead of using WiFi I’ve purchased some Xbee modules. They basically allow you to send serial bytes as well. The only problem is that I’m not quite sure how to handle the looping considering there’s no while(client.connected()) anymore. Instead of that I’ve used while(Serial.available()) thinking that will work, but it doesn’t set the value property for some reason.

I get command but I don’t get value.

Also I’m not sure whether the loop above is the best way of doing what I’m after, all I know is that it works just fine the way it is. 🙂

Here is my new loop, which only returns command and not value for some reason:

void loop() {

  // if there are bytes waiting on the serial port
  if (Serial.available()) { 
    boolean start_data = false;
    boolean next = false;

    char command[32];
    char value[32];
    int index = 0;

    while (Serial.available()) {
      char c = Serial.read();
      Serial.print(c);

      if (c == '}') {
        break;
      }

      if(start_data == true) {
        if(c != ',') {

          if(next)
            value[index] = c;
          else
            command[index] = c;

          index++;
        } else {
          next = true;
          command[index] = '\0';
          index = 0;
        }

      }

      if (c == '{') {
        start_data = true;
      }

    }

    value[index] = '\0';

    sendCommand(command,value);

  }

}

If the following works with the new loop, I’ll be very happy!

void sendCommand(char *command, char *value) {
 // do something wonderful with command and value!
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:46:06+00:00Added an answer on May 28, 2026 at 4:46 am

    Got it working by using the following code:

    #define SOP '{'
    #define EOP '}'
    
    bool started = false;
    bool ended = false;
    
    char inData[80];
    byte index;
    
    void setup()
    {
       Serial.begin(9600);
       // Other stuff...
    }
    
    void loop()
    {
      // Read all serial data available, as fast as possible
      while(Serial.available() > 0)
      {
        char inChar = Serial.read();
        if(inChar == SOP)
        {
           index = 0;
           inData[index] = '\0';
           started = true;
           ended = false;
        }
        else if(inChar == EOP)
        {
           ended = true;
           break;
        }
        else
        {
          if(index < 79)
          {
            inData[index] = inChar;
            index++;
            inData[index] = '\0';
          }
        }
      }
    
      // We are here either because all pending serial
      // data has been read OR because an end of
      // packet marker arrived. Which is it?
      if(started && ended)
      {
        // The end of packet marker arrived. Process the packet
        char *cmd = strtok(inData, ",");
        if(cmd)
        {
           char *val = strtok(NULL, ",");
           if(val)
           {
              sendCommand(cmd, val);
           }
        } 
    
        // Reset for the next packet
        started = false;
        ended = false;
        index = 0;
        inData[index] = '\0';
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet that i use to compile class at the run
I have following code that displays an Image with letters, public class MainActivity extends
I have a constructor for my Arduino-code which is something like the following: class
I have following code that creates Linq query. I've never used Linq until today
I am fetching data from the following database: I have arduino-box that send that
In order to ensure that some initialization code runs before main (using Arduino/avr-gcc) I
I have following code that I creating a grid var store = Ext.create('Ext.data.ArrayStore', {
I have following code that is executed when doing a mouseenter on a div
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.