#ifndef TZ51_BiSerial
#define TZ51_BiSerial

#include "Arduino.h"
#include <SoftwareSerial.h>

void Sendcmd (int broche,String s){
  //Setup the sending serial connection with TX as pin 2 and RX as a throwaway pin 30
  //Serial.print("send - ");
  //Serial.println(s);
  SoftwareSerial mySerial(-1,broche);
  mySerial.begin(57600);
  s.replace("\n","");
  s.replace("\r","");
  //Send the character and alert the user it was sent 
  mySerial.println(s);
  //The sending connection will close as it goes out of scope and the destructor is called
}

String recieverRunMaster(int broche){
  //Setup the recieving serial connection with RX as pin 2 and TX as a throwaway pin 30
  SoftwareSerial mySerial(broche,-1);
  mySerial.begin(57600);
  //For one second print any recieved data to the PC terminal
  long time = millis();
  String reponse="";
  String s1 = "";
  int incomingByte=0;
  bool timeout=false;
  bool msgcomplet=false;
  while ((timeout==false)&&(msgcomplet==false)) {
    if ( millis() - time > 600){
      timeout=true;
    }
    if (mySerial.available()) {
                // read the incoming byte:
                incomingByte = mySerial.read();
                time = millis();
                if (incomingByte==10) { 
                  msgcomplet=true;
                  s1.replace("\n","");
                  s1.replace("\r","");
				  reponse=s1;
                  //Serial.println(s1);          
                } else {
                s1+=char(incomingByte);
                }
        }
  } 
  if (msgcomplet==false) {
    //Serial.println("Error");
	reponse=F("ERROR");
  }
  //Serial.print("Rec - ");
  //Serial.println(reponse);
  return reponse;
}

#endif
