Multiplexer Problem with arduino

Hi everyone, I really need your help . Okay here's a intro to what I'm doing. I'm currently trying to use a multiplexer to control 4 serial device by using 1 software serial , and hardware serial for the computer , After tested and proven , I would make use of the hardware serial to program my stuff.

My connection is fairly simple , for arduino board pins 2 , 3, 6, 7 to Multiplexer pin 13, 3, 10, 9 respectively. You can refer to the image for reference:

Here's how's my code look like :

#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2
#define txpin 3                                                                //set the TX pin to pin 3

SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product

int s0 = 7;
int s1 = 6;

char *channel;
char *cmd;

void setup(){                                                                //set up the hardware
  Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
  myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
  inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
  sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
  pinMode(s0 , OUTPUT);
  pinMode(s1 , OUTPUT);  
}



void serialEvent() {                                                         //if the hardware serial port receives a char
  char inchar = (char)Serial.read();                               //get the char we just received
  inputstring += inchar;                                           //add it to the inputString
  if(inchar == '\r') {
    input_stringcomplete = true;
  }                //if the incoming character
}  


void open_channel(){                                  //This function controls what UART port is opened. 

  switch (*channel) {                              //Looking to see what channel to open   

  case '0':                                      //If channel==0 then we open channel 0     
    digitalWrite(s0, LOW);                       //S0 and S1 control what channel opens 
    digitalWrite(s1, LOW);                       //S0 and S1 control what channel opens  
    break;                                         //Exit switch case

  case '1':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, LOW);
    break;

  case '2':
    digitalWrite(s0, LOW);
    digitalWrite(s1, HIGH);
    break;

  case '3':
    digitalWrite(s0, HIGH);
    digitalWrite(s1, HIGH); 
    break;
  }
}



void loop(){                                                                   //here we go....



  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
    myserial.print(inputstring);                                             //send that string to the Atlas Scientific product 
    char carray[inputstring.length() + 1]; //determine size of the array
    inputstring.toCharArray(carray, sizeof(carray)); //put readStringinto an array

    Serial.print("c array:'");
    Serial.print(carray);
    Serial.println("'");

    channel=strtok(carray , ":");            //Let's parse the string at each colon
    cmd=strtok(NULL, ":");                        //Let's parse the string at each colon 
    open_channel();                                 //Call the function "open_channel" to open the correct data path
    myserial.print(cmd);                         //Send the command from the computer to the Atlas Scientific device using the softserial port 
    myserial.print("\r");                        //After we send the command we send a carriage return <CR> 
    input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
  }



  while (myserial.available()) {                                               //while a char is holding in the serial buffer
    char inchar = (char)myserial.read();                                  //get the new char
    sensorstring += inchar;                                               //add it to the sensorString
    if (inchar == '\r') {
      sensor_stringcomplete = true;
    }                   //if the incoming character is a <CR>, set the flag
  }

  Serial.print("channel :");
  Serial.print(channel);
  Serial.println("'");
  delay(500);

  Serial.print("command syntax");
  Serial.print(cmd);
  Serial.println("'");
  delay(500);


  if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
    Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
    //clear the string:
    sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
  inputstring ="";
  sensorstring="";
  delay(500);
}

So My command for switching the multiplexer would be 0 , 1, 2, 3, 4 . So if you were to type something would be like this ---> 0:(your string) <\CR>

But here's what I seen on the serial monitor , though there's data coming out but it's not correct as though all number are jumble up.

Serial monitor Image :

    inputstring.toCharArray(carray, sizeof(carray)); //put readStringinto an array

What is "sizeof(carray)"? carray is a pointer to a location in memory. It's therefore the size of that pointer. That's 16 bits, or 2 bytes.

So, you're copying 2 bytes into an array of random garbage (what happened to be on the stack when the array was allocated).

You should first clear the contents of the array (memset()) then copy the string using the full length of the array, not the size of the pointer to the array.

Better still, don't use the String class, just work direct in character arrays.

Ground Vee pin7. And do read the the data sheet.. Carefully.

Doc

Thanks Doc and majenkotech.

@Doc I apologized that I misread the data sheet as per mention above I did the following and re uploading my program There's changes to the channel output instead of that weird character now it come out 'A Q'. Here's a image of it:

@majenkotech pardon me.. I have a small question related to the solution you gave me is that wouldn't the carray be replace everytime when a new set of data is send in? I also wish that everything can be in character but this set of code works more efficiently in string so I'm in a tough spot... Hope you can give me some simple advise on solving this.. Thanks.