Skip to main content
Added C++ language formatting tag.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126
#include <stdlib.h>
#include <string.h>
HardwareSerial *port;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}
#include <stdlib.h>
#include <string.h>
HardwareSerial *port;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}
#include <stdlib.h>
#include <string.h>
HardwareSerial *port;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}
#include <stdlib.h>
#include <string.h>
HardwareSerial *port;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}
Simplified the code further
Source Link
sscirrus
  • 177
  • 1
  • 10

I have a Yun that accepts commands via the bridge (note the below code is part of a very simple virtual keyboard that accepts commands then runs Keyboard.println on them:

#include <stdlib.h>
#include <string.h>
 
HardwareSerial *port;
String cmd = "";
int arg_comma_index;
String instruction;
String func;
String args;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.

I have a Yun that accepts commands via the bridge (note the below code is part of a very simple virtual keyboard that accepts commands then runs Keyboard.println on them:

#include <stdlib.h>
#include <string.h>
 
HardwareSerial *port;
String cmd = "";
int arg_comma_index;
String instruction;
String func;
String args;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      instruction = cmd;
      arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      func = instruction.substring(0, arg_comma_index);
      args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.

I have a Yun that accepts commands via the bridge (note the below code is part of a very simple virtual keyboard that accepts commands then runs Keyboard.println on them:

#include <stdlib.h>
#include <string.h>
HardwareSerial *port;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      String instruction = cmd;
      int arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      String func = instruction.substring(0, arg_comma_index);
      String args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.
added 4 characters in body
Source Link
sscirrus
  • 177
  • 1
  • 10

I have a Yun that accepts commands via the bridge (note the below code is part of a very simple virtual keyboard that accepts commands then runs Keyboard.println on them:

#include <stdlib.h>
#include <string.h>

HardwareSerial *port;
String cmd = "";
int arg_comma_index;
String instruction;
String func;
String args;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
    port->flush();
 
    if(cmd.length() > 1){
      port->println("signeddebug\t" + String(cmd));

      char cmd_chars[ cmd.length()+1 ];
      cmd.toCharArray(cmd_chars, cmd.length() + 1);

      instruction = cmd;
      arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      func = instruction.substring(0, arg_comma_index);
      args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // ThrowTry to throw away whatever's left to prevent looping (which is still happening).
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.

I have a Yun that accepts commands via the bridge:

#include <stdlib.h>
#include <string.h>

HardwareSerial *port;
String cmd = "";
int arg_comma_index;
String instruction;
String func;
String args;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
    port->flush();
 
    if(cmd.length() > 1){
      port->println("signeddebug\t" + String(cmd));

      char cmd_chars[ cmd.length()+1 ];
      cmd.toCharArray(cmd_chars, cmd.length() + 1);

      instruction = cmd;
      arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      func = instruction.substring(0, arg_comma_index);
      args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Throw away whatever's left.
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.

I have a Yun that accepts commands via the bridge (note the below code is part of a very simple virtual keyboard that accepts commands then runs Keyboard.println on them:

#include <stdlib.h>
#include <string.h>

HardwareSerial *port;
String cmd = "";
int arg_comma_index;
String instruction;
String func;
String args;

void setup() {
  port = &Serial1;
  port->begin(9600);
}

void loop() {
  if (port->available()) {
    String cmd = port->readString();
 
    if(cmd.length() > 1){
      instruction = cmd;
      arg_comma_index = 0;
      arg_comma_index = instruction.indexOf(",");
      func = instruction.substring(0, arg_comma_index);
      args = instruction.substring(arg_comma_index + 1, instruction.length() + 1 );

      Keyboard.begin();
      Keyboard.println(args);
      Keyboard.end();
    }
    port->read(); // Try to throw away whatever's left to prevent looping (which is still happening).
  }
}

If I send it a short string:

  • The first time, immediately after starting the sketch, it works.
  • The second time, it works but loops anywhere from 1-5 times.
  • The third time, it loops more times still.
  • If I leave the device running (even if not operating it), the number of times it loops goes up when I finally do send it an instruction. I'm seeing 20-75 loops after less than 15 minutes of uptime.

Clearly the loops where no instruction is being received are still building up something here. I've tried:

  • Restarting the port each void loop().
  • Flushing the port at the top and/or bottom of each void loop().
  • Setting cmd = "" at the end of each void loop().
  • Checking peek(), available(), and readString() - after I send it one instruction, they return the same values every single time they loop.
Added actual code
Source Link
sscirrus
  • 177
  • 1
  • 10
Loading
Source Link
sscirrus
  • 177
  • 1
  • 10
Loading