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 8368155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:19:16+00:00 2026-06-09T13:19:16+00:00

My background is not in C (it’s in Real Studio – similar to VB)

  • 0

My background is not in C (it’s in Real Studio – similar to VB) and I’m really struggling to split a comma-delimited string since I’m not used to low-level string handling.

I’m sending strings to an Arduino over serial. These strings are commands in a certain format. For instance:

@20,2000,5!
@10,423,0!

‘@’ is the header indicating a new command and ‘!’ is the terminating footer marking the end of a command. The first integer after ‘@’ is the command id and the remaining integers are data (the number of integers passed as data may be anywhere from 0 – 10 integers).

I’ve written a sketch that gets the command (stripped of the ‘@’ and ‘!’) and calls a function called handleCommand() when there is a command to handle. The problem is, I really don’t know how to split this command up to handle it!

Here’s the sketch code:

String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // main loop
  handleCommand();
}

void serialEvent(){
  while (Serial.available()) {
  // all we do is construct the incoming command to be handled in the main loop

    // get the incoming byte from the serial stream
    char incomingByte = (char)Serial.read();

    if (incomingByte == '!')
    {
       // marks the end of a command
       commandReceived = true;
       return;
    }
    else if (incomingByte == '@')
    {
       // marks the start of a new command
       command = "";
       commandReceived = false;
       return;
    }
    else
    {
      command += incomingByte;
      return;
    }

  }
}

void handleCommand() {

  if (!commandReceived) return; // no command to handle

  // variables to hold the command id and the command data
  int id;
  int data[9];

  // NOT SURE WHAT TO DO HERE!!

  // flag that we've handled the command 
  commandReceived = false;
}

Say my PC sends the Arduino the string “@20,2000,5!”. My sketch ends up with a String variable (called command) that contains “20,2000,5” and the commandRecieved boolean variable is set to True so the handleCommand() function is called.

What I would like to do in the (currently useless) handleCommand() function is assign 20 to a variable called id and 2000 and 5 to an array of integers called data, i.e: data[0] = 2000, data[1] = 5, etc.

I’ve read about strtok() and atoi() but frankly I just can’t get my head around them and the concept of pointers. I’m sure my Arduino sketch could be optimised too.

  • 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-06-09T13:19:18+00:00Added an answer on June 9, 2026 at 1:19 pm

    Since you’re using the Arduino core String type, strtok and other string.h functions aren’t appropriate. Note that you can change your code to use standard C null-terminated strings instead, but using Arduino String will let you do this without using pointers.

    The String type gives you indexOf and substring.

    Assuming a String with the @ and ! stripped off, finding your command and arguments would look something like this:

    // given: String command
    int data[MAX_ARGS];
    int numArgs = 0;
    
    int beginIdx = 0;
    int idx = command.indexOf(",");
    
    String arg;
    char charBuffer[16];
    
    while (idx != -1)
    {
        arg = command.substring(beginIdx, idx);
        arg.toCharArray(charBuffer, 16);
    
        // add error handling for atoi:
        data[numArgs++] = atoi(charBuffer);
        beginIdx = idx + 1;
        idx = command.indexOf(",", beginIdx);
    }
    
    data[numArgs++] = command.substring(beginIdx);
    

    This will give you your entire command in the data array, including the command number at data[0], while you’ve specified that only the args should be in data. But the necessary changes are minor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple challenge with a background not animating. I have used this
Background: I'm coming from Java background so not too familiar with Javascript. We are
The body background is not displaying in firefox. here is my CSS body{ font-family:Trebuchet
For some reason the background does not appear. This is the CSS div.sidebar_beige {
Calling the WriteObject Method from a background thread is not possible! Is there a
I've inherited code where BeginInvoke is called from the main thread (not a background
AS the subject outlines, the background div will not show, I thought you needed
<style type=text/css> #[':0:adId'] { /* this does not work */ background:red; } </style> <div
Background The Apache Action class is not thread-safe. However, this was only realized after
Background: I hope my question is not too vague. I will try and explain

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.