Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String inputString = ""; // a String to hold incoming data
- bool stringComplete = false; // whether the string is complete
- String inputString1 = ""; // a String to hold incoming data
- bool stringComplete1 = false; // whether the string is complete
- String inputString3 = ""; // a String to hold incoming data
- bool stringComplete3 = false; // whether the string is complete
- int index=0;
- int baud=115200;
- #define commands 5
- char *command[]= {
- "help",
- "Hi",
- "To1:",
- "To3:",
- "4"
- };
- void setup() {
- // Open serial communications and wait for port to open:
- Serial.begin(115200);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- inputString1.reserve(200);
- inputString3.reserve(200);
- // Start each software serial port
- Serial1.begin(115200);
- while (!Serial1) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- Serial3.begin(115200);
- while (!Serial3) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- Serial.println("Ready!");
- }
- void loop() {
- if (stringComplete) {
- Serial.print(inputString);
- if (inputString.substring(0,5)=="help\n"){
- char *string="";
- int i;
- Serial.println("Help:");
- Serial.print(commands,DEC);
- Serial.println(" commands: ");
- for (i=0;i<commands;i++){
- sprintf(string,"%d : %s size: %d\n",i,command[i], strlen(command[i]));
- Serial.print(string);
- }
- }
- if (inputString.substring(0,strlen(command[1]))==command[1]){
- Serial.println("Hello");
- }
- else if (inputString.substring(0,strlen(command[2]))==command[2]){
- Serial.println("Send to 1:");
- Serial.println(inputString.substring(4));
- Serial1.println(inputString.substring(4));
- }
- else if (inputString.substring(0,strlen(command[3]))==command[3]){
- Serial.println("Send to 3:");
- Serial.println(inputString.substring(4));
- Serial3.println(inputString.substring(4));
- }
- else{
- //Serial.println("OK");
- }
- // clear the string:
- inputString = "";
- stringComplete = false;
- }
- if (stringComplete1) {
- Serial.print("1:");
- Serial.print(inputString1);
- Serial3.print(inputString1);
- // clear the string:
- inputString1 = "";
- stringComplete1 = false;
- }
- if (stringComplete3) {
- Serial.print("3:");
- Serial.print(inputString3);
- Serial1.print(inputString3);
- // clear the string:
- inputString3 = "";
- stringComplete3 = false;
- }
- }
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag so the main loop can
- // do something about it:
- if (inChar == '\n') {
- stringComplete = true;
- }
- }
- }
- void serialEvent1() {
- while (Serial1.available()) {
- // get the new byte:
- char inChar = (char)Serial1.read();
- // add it to the inputString:
- inputString1 += inChar;
- // if the incoming character is a newline, set a flag so the main loop can
- // do something about it:
- if (inChar == '\n') {
- stringComplete1 = true;
- }
- }
- }
- void serialEvent3() {
- while (Serial3.available()) {
- // get the new byte:
- char inChar = (char)Serial3.read();
- // add it to the inputString:
- inputString3 += inChar;
- // if the incoming character is a newline, set a flag so the main loop can
- // do something about it:
- if (inChar == '\n') {
- stringComplete3 = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment