Skip to content

Instantly share code, notes, and snippets.

@domenicomonaco
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenicomonaco/9d151fcc568201cd8297 to your computer and use it in GitHub Desktop.
Save domenicomonaco/9d151fcc568201cd8297 to your computer and use it in GitHub Desktop.
Simple Arduino Serial comunication, with echo of entire line received and calculation of time elapsed to read entire line.
/**
*
* Author: Domenico Monaco
*
* Description:
* Simple Arduino Serial comunication, with echo of entire line received
* and calculation of time elapsed to read entire line. This example is
* extension of https://gist.github.com/kiuz/d59519e0de7677df42c3/
*
* Source: https://gist.github.com/kiuz/9d151fcc568201cd8297
*
* License: GNU v2 2014
**/
int incomingByte = 0; // for incoming serial data
long int timeStartCom = 0;
long int timeEndCom = 0;
long int charsIN = 0;
bool waiting = false;
String line="";
void setup() {
Serial.begin(115200);
}
void loop() {
readLine();
handeShake(line);
}
void handeShake(String message){
if(message.length!=0){
Swerial.println("Leggo messaggio");
}
}
void readLine(){
// send data only when you receive data:
if (Serial.available() > 0) {
if(charsIN==(long int)0){
timeStartCom = millis();
timeEndCom = 0;
waiting = false;
}
charsIN++;
incomingByte = Serial.read();
line = line + (char)incomingByte;
delay(1); //Delay of Serial transfer
}else{
if(waiting==false){
waiting = true;
timeEndCom = millis();
///PRINT PREVUOUS MESSAGE
Serial.print("Message: ");
Serial.println(line);
Serial.print("Recived ");
Serial.print(charsIN);
Serial.print("chars in ");
Serial.print(timeEndCom-timeStartCom);
Serial.print("millis; ");
Serial.print((float)(timeEndCom-timeStartCom)/charsIN);
Serial.print(" mills for chars;");
Serial.print(" ");
Serial.print((float)charsIN/(timeEndCom-timeStartCom));
Serial.println("char/millis;");
Serial.println("---------------------");
line = "";
timeStartCom = 0;
charsIN = (long int)0;
Serial.println("");
Serial.println("Waiting for message...");
Serial.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment