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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:25:13+00:00 2026-05-28T17:25:13+00:00

The program listen to messages from serial port in the form or where first

  • 0

The program listen to messages from serial port in the form or where first character (A or D) means analog or digital, the 2nd character – pin, the 3rd character – 1/0 or from 0 to 255. The markers < and > show the beginning and the end of packet.

For example, if packet is received, the light is turned on by digitalWrite(13,1)
But nothing happens. When I send via serial monitor, for instance: light is supposed to blink but it does not. The same with analogue outputs.

bool started = false;
bool ended = false;
char inData[5];
byte index;


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();


    if (inChar == '<')
    {
      index = 0;
      started = true;
      ended = false; 
    }

    else if (inChar == '>')
    {
      ended = true;
      break;
    }

    else
    {
      if (index <= 4)
      {
        inData[index] = inChar;
        index++;
      }
    }



     if (started && ended)
    {
      if (inData[0] == 'A')
      {
        pinMode(inData[2],OUTPUT);
        analogWrite(inData[2],inData[4]);
      }


      else if (inData[0] == 'D')
      {
        if (inData[4] == 1)
        {
          pinMode(inData[2],OUTPUT);
          digitalWrite(inData[2],HIGH);
        }

        else if (inData[4] == 0)
        {
           pinMode(inData[2],OUTPUT);
           digitalWrite(inData[2],LOW);
        }
      }
      started = false;
      ended = false;
      index = 0; 
    }

  }


Serial.println("Sending");  



}
  • 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-28T17:25:14+00:00Added an answer on May 28, 2026 at 5:25 pm

    The following code will allow you to execute a method with an example serial string:

    <power,led>
    

    Once it processes this string, it’ll execute the following method:

    sendCommand(cmd, val);
    

    See below for an example of how to turn on an LED on PIN 11.

    #include <avr/pgmspace.h>
    
    int IRledPin = 11;
    
    #define SOP '<'
    #define EOP '>'
    
    bool started = false;
    bool ended = false;
    
    char inData[80];
    byte index;
    
    void setup() {
      pinMode(IRledPin, OUTPUT);
      Serial.begin(9600);
    }
    
    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';
      }
    }
    
    void sendCommand(char *command, char *value) {
      if (strcmp(command,"power") == 0) {
        power(value);
      }
    }
    
    void power(char* value) {
      if (strcmp(value, "led") == 0) {
        digitalWrite(IRledPin, HIGH);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program needs to listen incoming socket connections (lets agree on port 8765), but
I have an ipod touch program that should receive messages from a server program
I'm writing a program in C++ to listen to a stream of tcp messages
We have a .NET program that uses WCF to listen for communication from another
I am writing a program to sit in the background on osx 10.6, listen
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
A program receives a list of Messages (base type). Each message in the list
I've got an event-driven network server program. This program accepts connections from other processes
I've got a chat program which pushes JSON data from Apache/PHP to Node.js, via
Am using java to develop program that receives and processes SMS messages. I have

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.