dald

Untitled

Jun 5th, 2020
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String inputString = "";         // a String to hold incoming data
  2. bool stringComplete = false;  // whether the string is complete
  3. String inputString1 = "";         // a String to hold incoming data
  4. bool stringComplete1 = false;  // whether the string is complete
  5. String inputString3 = "";         // a String to hold incoming data
  6. bool stringComplete3 = false;  // whether the string is complete
  7. int index=0;
  8. int baud=115200;
  9. #define commands 5
  10. char *command[]= {
  11.   "help",
  12.   "Hi",
  13.   "To1:",
  14.   "To3:",
  15.   "4"
  16. };
  17.  
  18. void setup() {
  19.   // Open serial communications and wait for port to open:
  20.   Serial.begin(115200);
  21.   while (!Serial) {
  22.     ; // wait for serial port to connect. Needed for native USB port only
  23.   }
  24.  
  25.   inputString1.reserve(200);
  26.   inputString3.reserve(200);
  27.   // Start each software serial port
  28.   Serial1.begin(115200);
  29.   while (!Serial1) {
  30.     ; // wait for serial port to connect. Needed for native USB port only
  31.   }
  32.   Serial3.begin(115200);
  33.   while (!Serial3) {
  34.     ; // wait for serial port to connect. Needed for native USB port only
  35.   }
  36.   Serial.println("Ready!");
  37. }
  38.  
  39. void loop() {
  40.   if (stringComplete) {
  41.     Serial.print(inputString);
  42.     if (inputString.substring(0,5)=="help\n"){
  43.       char *string="";
  44.       int i;
  45.       Serial.println("Help:");
  46.       Serial.print(commands,DEC);
  47.       Serial.println(" commands: ");
  48.       for (i=0;i<commands;i++){
  49.         sprintf(string,"%d : %s size: %d\n",i,command[i], strlen(command[i]));
  50.        
  51.         Serial.print(string);
  52.       }
  53.     }
  54.     if (inputString.substring(0,strlen(command[1]))==command[1]){
  55.       Serial.println("Hello");
  56.     }
  57.     else if (inputString.substring(0,strlen(command[2]))==command[2]){
  58.       Serial.println("Send to 1:");
  59.       Serial.println(inputString.substring(4));
  60.       Serial1.println(inputString.substring(4));
  61.     }
  62.     else if (inputString.substring(0,strlen(command[3]))==command[3]){
  63.       Serial.println("Send to 3:");
  64.       Serial.println(inputString.substring(4));
  65.       Serial3.println(inputString.substring(4));
  66.     }
  67.     else{
  68.       //Serial.println("OK");
  69.     }
  70.       // clear the string:
  71.     inputString = "";
  72.     stringComplete = false;
  73.   }
  74.   if (stringComplete1) {
  75.    
  76.     Serial.print("1:");
  77.     Serial.print(inputString1);
  78.     Serial3.print(inputString1);
  79.  
  80.       // clear the string:
  81.     inputString1 = "";
  82.     stringComplete1 = false;
  83.   }
  84.   if (stringComplete3) {
  85.    
  86.     Serial.print("3:");
  87.     Serial.print(inputString3);
  88.     Serial1.print(inputString3);
  89.  
  90.       // clear the string:
  91.     inputString3 = "";
  92.     stringComplete3 = false;
  93.   }
  94. }
  95.  
  96. void serialEvent() {
  97.  
  98.   while (Serial.available()) {
  99.     // get the new byte:
  100.     char inChar = (char)Serial.read();
  101.     // add it to the inputString:
  102.     inputString += inChar;
  103.     // if the incoming character is a newline, set a flag so the main loop can
  104.     // do something about it:
  105.     if (inChar == '\n') {
  106.       stringComplete = true;
  107.     }
  108.   }
  109. }
  110. void serialEvent1() {
  111.  
  112.   while (Serial1.available()) {
  113.     // get the new byte:
  114.     char inChar = (char)Serial1.read();
  115.     // add it to the inputString:
  116.     inputString1 += inChar;
  117.     // if the incoming character is a newline, set a flag so the main loop can
  118.     // do something about it:
  119.     if (inChar == '\n') {
  120.       stringComplete1 = true;
  121.     }
  122.   }
  123. }
  124.  
  125. void serialEvent3() {
  126.  
  127.   while (Serial3.available()) {
  128.     // get the new byte:
  129.     char inChar = (char)Serial3.read();
  130.     // add it to the inputString:
  131.     inputString3 += inChar;
  132.     // if the incoming character is a newline, set a flag so the main loop can
  133.     // do something about it:
  134.     if (inChar == '\n') {
  135.       stringComplete3 = true;
  136.     }
  137.   }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment