Arduino simple serial echo

Part of my process of going from a large and complicated A, to a large and complicated B is to do small things along the way that gradually inch me closer to what I want to do.

What I really wanted was this example of an arduino script from which I could test and get confidence on my next step in the process.

without too much explanation, this short arduino code tells your peripheral device to receive a string on the serial communication port and send it back out to the same serial communication port.


/* Arduino serial echo

The circuit:
Any serial device attached to Serial port
Serial monitor open on Serial port:

This example code is in the public domain.*/

void setup() {
// initialize serial port:
Serial.begin(115200);
}

void loop() {
// read from port, send to port:
if (Serial.available()) {
char inStr = Serial.read();
// delay(100) //optional delay of 1ms
Serial.write(inStr);
}
}


Geez.  That wasn’t so hard was it?  Now hopefully if anyone searches google for an arduino serial echo they will get here and be able to make handy use of this script along the way to bigger things.


Meta: This took 10mins to write

Edit: oh hey look here it is https://www.arduino.cc/en/Serial/Available same as my script only I didn’t know the command Available prior to hacking it into a script

This entry was posted in electronics and tagged , , , , , , . Bookmark the permalink.

Leave a Reply