-
-
Save dyadica/50ac3d7d4daaca84b8c81e0dd7bc7bc0 to your computer and use it in GitHub Desktop.
A collection of Serial port bounce methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A collection of Serial Port bounce methods | |
Each example is based on the public domain code provided by by Tom Igoe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialise the program | |
void setup() | |
{ | |
// Initialise serial port 0 | |
Serial.begin(9600); | |
// Initialise serial port 1 | |
Serial1.begin(9600); | |
} | |
// Run the program loop | |
void loop() | |
{ | |
// read from port 1 | |
if (Serial1.available()) | |
{ | |
// If there is data then send to | |
// Serial port 1 | |
int inByte = Serial1.read(); | |
Serial.write(inByte); | |
} | |
// read from port 0 | |
if (Serial.available()) | |
{ | |
// If there is data then send to | |
// Serial port 0 | |
int inByte = Serial.read(); | |
Serial1.write(inByte); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Placeholder string to hold incoming data | |
String inputString = ""; | |
// A flag to show if the string is complete | |
boolean stringComplete = false; | |
// Initialise the program | |
void setup() | |
{ | |
// Initialise the serial port | |
Serial.begin(9600); | |
// reserve 200 bytes for the | |
// inputString (buffer) | |
inputString.reserve(200); | |
} | |
// Run the program loop | |
void loop() | |
{ | |
// print the string when a newline delimiter | |
// is detected: | |
if (stringComplete) | |
{ | |
Serial.println(inputString); | |
// clear the string and reset flag | |
inputString = ""; | |
stringComplete = false; | |
} | |
} | |
// A SerialEvent occurs whenever a new data comes in the | |
// hardware serial RX. This routine is run between each | |
// time loop() runs, so using delay inside loop can delay | |
// response. Multiple bytes of data may be available. | |
void serialEvent() | |
{ | |
while (Serial.available()) | |
{ | |
// get the new byte | |
char inChar = (char)Serial.read(); | |
// add it to the inputString | |
inputString += inChar; | |
// if the incoming character is a newline, | |
// update the flag to inform that a full | |
// string has been sent. | |
if (inChar == '\n') | |
{ | |
stringComplete = true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the software serial lib | |
#include <SoftwareSerial.h> | |
// Register the software port | |
SoftwareSerial mySerial(10, 11); // RX, TX | |
// Initialise the program | |
void setup() | |
{ | |
// Initialise serial port 0 | |
Serial.begin(9600); | |
// Wait for the port to open | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
// Send a hello call | |
Serial.println("Hello World HS"); | |
// set the data rate for the SoftwareSerial port | |
mySerial.begin(9600); | |
// Send a hello call | |
mySerial.println("Hello, world SS"); | |
} | |
// Run the program loop | |
void loop() | |
{ | |
// Bounce data from SS Port | |
if (mySerial.available()) | |
{ | |
Serial.write(mySerial.read()); | |
} | |
// Bounce data from HS Port | |
if (Serial.available()) | |
{ | |
mySerial.write(Serial.read()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add the software serial lib | |
#include <SoftwareSerial.h> | |
// software serial #1: RX = digital pin 10, TX = digital pin 11 | |
SoftwareSerial portOne(10, 11); | |
// software serial #2: RX = digital pin 8, TX = digital pin 9 | |
SoftwareSerial portTwo(8, 9); | |
// Initialise the program | |
void setup() | |
{ | |
// Initialise serial port 0 | |
Serial.begin(9600); | |
// Wait for the port to open | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
// Initialise both SS Ports | |
portOne.begin(9600); | |
portTwo.begin(9600); | |
} | |
void loop() | |
{ | |
// By default, the last intialized port is listening. | |
// when you want to listen on a port, explicitly select it: | |
portOne.listen(); | |
// Send a hello call | |
Serial.println("Hello world from p1"); | |
// while there is data coming in, read it | |
// and send to the hardware serial port: | |
while (portOne.available() > 0) | |
{ | |
char inByte = portOne.read(); | |
Serial.write(inByte); | |
} | |
// Send a blank line to separate data from | |
// both ports: | |
Serial.println(); | |
// Perform a listen on the second port | |
portTwo.listen(); | |
// while there is data coming in, read it | |
// and send to the hardware serial port: | |
Serial.println("Hello world from p2"); | |
// while there is data coming in, read it | |
// and send to the hardware serial port: | |
while (portTwo.available() > 0) | |
{ | |
char inByte = portTwo.read(); | |
Serial.write(inByte); | |
} | |
// Send a blank line to separate data from | |
// both ports: | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment