Skip to content

Instantly share code, notes, and snippets.

@fairchild
Created April 13, 2009 07:37
Show Gist options
  • Save fairchild/94327 to your computer and use it in GitHub Desktop.
Save fairchild/94327 to your computer and use it in GitHub Desktop.
Simple updated examples of arduino serial communications
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 03_function development
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* the same as examlpe 03 but it deploys 2 reusable functions.
* for doing the same job.
* readSerialString() and printSerialString()
* you just need to instantiate an array that will hold all the chars of the string
* I've put a 100 value for excess, but if you exactly know how many bytes you are expecting
* simply write it down inside the brackets [yourLengthHere]
*
* created 16 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
* updated April 2009
* --------------------------------------------------- */
char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won't work properly
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(Serial.available()>0) {
Serial.print("reading Serial String: "); //optional: for confirmation
while (Serial.available()>0){
strArray[i] = Serial.read();
i++;
Serial.print(strArray[(i-1)]); //optional: for confirmation
}
Serial.println(); //optional: for confirmation
}
}
//Print the whole string at once - will be performed only if thers is data inside it
//you must supply the array variable
void printSerialString(char *strArray) {
int i=0;
if (strArray[i] != 0) {
while(strArray[i] != 0) {
Serial.print( strArray[i] );
strArray[i] = 0; // optional: flush the content
i++;
}
}
}
//utility function to know wither an array is empty or not
boolean isStringEmpty(char *strArray) {
if (strArray[0] == 0) {
return true;
} else {
return false;
}
}
void setup() {
Serial.begin(9600);
Serial.println("Hello World");
}
void loop () {
//read the serial port and create a string out of what you read
readSerialString(serInString);
//do somenthing else perhaps wait for other data or read another Serial string
Serial.println ("------------ arduino is doing somenthing else ");
if( isStringEmpty(serInString) == false) { //this check is optional
Serial.print("Arduino recorded that you said: ");
//try to print out collected information. it will do it only if there actually is some info.
printSerialString(serInString);
Serial.println();
}
//slows down the visualization in the terminal
delay(2000);
}
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* this is the first step for establishing sentence long conversations between arduino and the pc.
* serialRead() reads one byte at a time from the serial buffer.
* so in order to print out the whole sentence at once
* (it is actually still printing one byte at a time but the pc will receive it
* not interupted by newLines or other printString inside you loop)
* You must loop untill there are bytes in the serial buffer and
* and print right away that byte you just read.
* after that the loop can continue it's tasks.
*
* created 15 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */
int serIn; //var that will hold the bytes in read from the serialBuffer
void setup() {
Serial.begin(9600);
}
//auto go_to_the_line function
//void printNewLine() {
// Serial.print(13, BYTE);
// Serial.print(10, BYTE);
//}
void loop () {
//simple feedback from Arduino Serial.println("Hello World");
// only if there are bytes in the serial buffer execute the following code
if(Serial.available()) {
//inform that Arduino heard you saying something
Serial.print("Arduino heard you say: ");
//keep reading and printing from serial untill there are bytes in the serial buffer
while (Serial.available()>0){
serIn = Serial.read(); //read Serial
Serial.print(serIn, BYTE); //prints the character just read
}
//the serial buffer is over just go to the line (or pass your favorite stop char)
Serial.println();
}
//slows down the visualization in the terminal
delay(1000);
}
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* this is the first step for establishing sentence long conversations between arduino and the pc.
* serialRead() reads one byte at a time from the serial buffer.
* so in order to print out the whole sentence at once
* (it is actually still printing one byte at a time but the pc will receive it
* not interupted by newLines or other printString inside you loop)
* You must loop untill there are bytes in the serial buffer and
* and print right away that byte you just read.
* after that the loop can continue it's tasks.
*
* created 15 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */
int serIn; //var that will hold the bytes in read from the serialBuffer
void setup() {
Serial.begin(9600);
}
//auto go_to_the_line function
//void printNewLine() {
// Serial.print(13, BYTE);
// Serial.print(10, BYTE);
//}
void loop () {
//simple feedback from Arduino Serial.println("Hello World");
// only if there are bytes in the serial buffer execute the following code
if(Serial.available()) {
//inform that Arduino heard you saying something
Serial.print("Arduino heard you say: ");
//keep reading and printing from serial untill there are bytes in the serial buffer
while (Serial.available()>0){
serIn = Serial.read(); //read Serial
Serial.print(serIn, BYTE); //prints the character just read
}
//the serial buffer is over just go to the line (or pass your favorite stop char)
Serial.println();
}
//slows down the visualization in the terminal
delay(1000);
}
/* ------------------------------------------------
* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 03_function development
* by beltran berrocal
*
* this prog establishes a connection with the pc and waits for it to send him
* a long string of characters like "hello Arduino!".
* Then Arduino informs the pc that it heard the whole sentence
*
* the same as examlpe 02 but it deploys 2 reusable functions.
* for doing the same job.
* readSerialString() and printSerialString()
* the only problem is that they use global variables instead of getting them passed
* as parameters. this means that in order to reuse this code you should also copy
* the 4 variables instantiated at the beginning of the code.
* Another problem is that if you expect more than one string at a time
* you will have to duplicate and change names to all variables as well as the functions.
* Next version should have the possibility to pass the array as a parameter to the function.
*
* created 15 Decembre 2005;
* copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com>
*
* --------------------------------------------------- */
int serIn; // var that will hold the bytes-in read from the serialBuffer
char serInString[100]; // array that will hold the different bytes 100=100characters;
// -> you must state how long the array will be else it won't work.
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte
int serOutIndx = 0; // index of the outgoing serInString[] array;
/*read a string from the serial and store it in an array
//you must supply the array variable and the index count
void readSerialString (char *strArray, int indx) {
int sb; //declare local serial byte before anything else
Serial.print("reading Serial String: ");
if(serialAvailable()) {
while (serialAvailable()){
sb = serialRead();
strArray[indx] = sb;
indx++;
serialWrite(sb);
}
}
Serial.println();
}
*/
//read a string from the serial and store it in an array
//this func uses globally set variable so it's not so reusable
//I need to find the right syntax to be able to pass to the function 2 parameters:
// the stringArray and (eventually) the index count
void readSerialString () {
int sb;
if(Serial.available()) {
//Serial.print("reading Serial String: "); //optional confirmation
while (Serial.available()){
sb = Serial.read();
serInString[serInIndx] = sb;
serInIndx++;
//serialWrite(sb); //optional confirmation
}
//Serial.println();
}
}
//print the string all in one time
//this func as well uses global variables
void printSerialString() {
if( serInIndx > 0) {
Serial.print("Arduino memorized that you said: ");
//loop through all bytes in the array and print them out
for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++) {
Serial.print( serInString[serOutIndx] ); //print out the byte at the specified index
//serInString[serOutIndx] = ""; //optional: flush out the content
}
//reset all the functions to be able to fill the string back with content
serOutIndx = 0;
serInIndx = 0;
Serial.println();
}
}
void setup() {
Serial.begin(9600);
Serial.println("Hello World");
}
void loop () {
//simple feedback from Arduino
//read the serial port and create a string out of what you read
//readSerialString(serInString, serInIndx);
readSerialString();
//do somenthing else perhaps wait for other data or read another Serial string
Serial.println ("------------ arduino is doing somenthing else ");
//try to print out collected information. it will do it only if there actually is some info.
printSerialString();
//slows down the visualization in the terminal
delay(2000);
}
@medbhmeca
Copy link

hello burton-robotics, did you find a solution to receive mesurement from the tw10sp laser sensor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment