Advertisement
Guest User

Untitled

a guest
May 25th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include "AConfig.h"
  2. #if(HAS_Slave)
  3.  
  4.  
  5. #include <Arduino.h>
  6. #include "Slave.h"
  7.  
  8.  
  9. #include "CPin.h"
  10. #include "NConfigManager.h"
  11. #include "NModuleManager.h"
  12. #include "NCommManager.h"
  13.  
  14. #if(HAS_STD_CAPE)
  15. #include "CCape.h"
  16. #endif
  17.  
  18. #if(HAS_OROV_CONTROLLERBOARD_25)
  19. #include "CControllerBoard.h"
  20. #endif
  21.  
  22. #include <SoftwareSerial.h>
  23.  
  24.  
  25. #define rx 14                                          //define what pin rx is going to be
  26. #define tx 15                                          //define what pin tx is going to be
  27.  
  28. SoftwareSerial myserial(rx, tx);                      //define how the soft serial port is going to work
  29.   char sensorstring_array[30];                        //we make a char array
  30.   char *EC;                                           //char pointer used in string parsing
  31.   char *TDS;                                          //char pointer used in string parsing
  32.   char *SAL;                                          //char pointer used in string parsing
  33.   char *GRAV;                                         //char pointer used in string parsing
  34.   float f_ec;                                         //used to hold a floating point number that is the EC
  35.  
  36. String inputstring = "";                              //a string to hold incoming data from the PC
  37. String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
  38. boolean input_string_complete = false;                //have we received all the data from the PC
  39. boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
  40.  
  41. void Slave::Initialize() {                                        //set up the hardware
  42.          Serial.begin(9600);                                      //set baud rate for the hardware serial port_0 to 9600
  43.   myserial.begin(9600);                               //set baud rate for the software serial port to 9600
  44.   inputstring.reserve(10);                            //set aside some bytes for receiving data from the PC
  45.   sensorstring.reserve(30);                           //set aside some bytes for receiving data from Atlas Scientific product
  46. }
  47.  
  48. void Slave::Update( CCommand& commandIn)
  49. {                                         //here we go...
  50. while(Serial.available()){
  51.  inputstring = Serial.readStringUntil(13);           //read the string until we see a <CR>
  52.   input_string_complete = true;    
  53. }
  54.  
  55.  
  56.   if (input_string_complete) {                        //if a string from the PC has been received in its entirety
  57.     myserial.print(inputstring);                      //send that string to the Atlas Scientific product
  58.     myserial.print('\r');                             //add a <CR> to the end of the string
  59.     inputstring = "";                                 //clear the string
  60.     input_string_complete = false;                    //reset the flag used to tell if we have received a completed string from the PC
  61.   }
  62.  
  63.   if (myserial.available() > 0) {                     //if we see that the Atlas Scientific product has sent a character
  64.     char inchar = (char)myserial.read();              //get the char we just received
  65.     sensorstring += inchar;                           //add the char to the var called sensorstring
  66.     if (inchar == '\r') {                             //if the incoming character is a <CR>
  67.       sensor_string_complete = true;                  //set the flag
  68.     }
  69.   }
  70.  
  71.  
  72.   if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
  73.     if (isdigit(sensorstring[0]) == false) {          //if the first character in the string is a digit
  74.       Serial.println(sensorstring);                   //send that string to the PC's serial monitor
  75.     }
  76.     else                                              //if the first character in the string is NOT a digit
  77.     {
  78.  
  79.  
  80.   sensorstring.toCharArray(sensorstring_array, 30);   //convert the string to a char array
  81.   EC = strtok(sensorstring_array, ",");               //let's pars the array at each comma
  82.   TDS = strtok(NULL, ",");                            //let's pars the array at each comma
  83.   SAL = strtok(NULL, ",");                            //let's pars the array at each comma
  84.   GRAV = strtok(NULL, ",");                           //let's pars the array at each comma
  85.  
  86.   Serial.print("EC:");                                //we now print each value we parsed separately
  87.   Serial.println(EC);                                 //this is the EC value
  88.  
  89.   Serial.print("TDS:");                               //we now print each value we parsed separately
  90.   Serial.println(TDS);                                //this is the TDS value
  91.  
  92.   Serial.print("SAL:");                               //we now print each value we parsed separately
  93.   Serial.println(SAL);                                //this is the salinity value
  94.  
  95.   Serial.print("GRAV:");                              //we now print each value we parsed separately
  96.   Serial.println(GRAV);                               //this is the specific gravity
  97.   Serial.println();                                   //this just makes the output easier to read
  98.                                 //then call this function
  99.     }
  100.     sensorstring = "";                                //clear the string
  101.     sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  102.   }
  103. }
  104. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement