# Understanding serial communication with another software

**URL:** <https://forum.arduino.cc/t/understanding-serial-communication-with-another-software/1405317>\
**Category:** Programming\
**Created:** [September 4, 2025, 6:09am UTC](https://forum.arduino.cc/t/understanding-serial-communication-with-another-software/1405317 "2025-09-04T06:09:54Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 4, 2025, 10:24am UTC](https://forum.arduino.cc/t/understanding-serial-communication-with-another-software/1405317/7 "2025-09-04T10:24:13Z")

</div>

look this code over, seems like where you're headed

- it better to read a complete line (up to a \n) and then extract fields from that line
- rather than have unique IDs for each LED, consider an "led" cmd with 2 arguments: an led idx and value

```arduino
// simple cmd processing

const int NUM_LEDS = 2;
const int LED_pins[] = { 13, 12 };

// -----------------------------------------------------------------------------
void cmdProcessor ()
{
    if (Serial.available ()) {
        char buf [90];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        char cmd [10];
        int id;
        int arg;
        sscanf (buf, "%s %d %d", cmd, &id, &arg);

        if (! strcmp (cmd, "led"))
            if (NUM_LEDS > id)
                digitalWrite (LED_pins [id], arg);

        // other cmds
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    cmdProcessor ();
}

void setup ()
{
  Serial.begin (115200);

  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode (LED_pins[i], OUTPUT);
  }
}

```

---

_[View the full topic](https://forum.arduino.cc/t/understanding-serial-communication-with-another-software/1405317)._
