Arduino Read Serial Input

Here I am again, talking about another thing I really struggled with, reading an input from the serial on the Arduino.  I am using a 2560 hooked to my Raspberry Pi on Serial1, you might have an Uno or something, the directions are probably similar, but you only get the single shared serial port.

Back story, So I have the Arduino Ethernet shield, and I am using the 2560 with SoftwareSerial turned on, but wouldn’t you know it SoftwareSerial and The ethernet shield don’t play nice, so I needed another plan of attack, the original idea was to have the arduino report back to the raspberry pi with some data, so basically I turned it around and am connecting to the Arduino via the UART connection on the Raspberry Pi, running the commands I need and getting the output I need to store, so far it seems to work well.  One really big issues I had was reading in on the Serial then doing a compare with the “command” I wanted to execute.  Here is what I ended up doing:

#include <SoftwareSerial.h>
char str[10];
char* c = str;
 // RX, TX, true sets the inverter
void setup() {
void loop() {
  char* c = str;
  while (Serial1.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial1.available() >0) {
      *c++ =;  //gets one byte from serial buffer
      //makes the string readString
     //do something if we get a match!
     Serial1.println("You sly dog, you got a command");

That is it! For each of the strings I want to compare I do another strcmp(). I think one of the most important things is *c=0, this nulls out str on the next go around of the loop, otherwise you may get some unexpected results.


Leave a Reply

Your email address will not be published. Required fields are marked *