UART/USUART switching

Hi, I have this kind of arduino code:

String readString;
char charb[12];
char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
 
 
void setup(){
    Serial.begin(38400);
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString.toCharArray(charb, 12);
      initGSM();
      send_msg(charb, "OK!");
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

void loopGSM() {
  //none
}
 
void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {'\0'};
  char msg1[160] = {'\0'};
  char ctl_z = 0x1A;
 
  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
  
  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}
 
void sendGSM(char *string){
  Serial.write(string);
  delay(90);
}
 
void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}
 
void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(90);
    readSerialString(Rx_data);
    
    stat = strstr(Rx_data, "OK");
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}
 
void initGSM(){
  
  send_cmd("AT\r\n",1);						
//  send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module	
	
  send_cmd("AT+CMGF=1\r\n",1);			// Set message format to text mode
  //Sucess
  
  Serial.println("Success");
	
  delay(1000);
  delay(1000);
  delay(1000);
}
 
void readSerialString (char *strArray) {
  
  if(!Serial.available()) {
    return;
  }
  
  while(Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}

My problem is that, to be able for me to input a string on the serial monitor, I have to switch the GSM shield to USUART and then next switch the shield into UART to run/start sending the SMS. Is there any way or some modifications needed in my code to get rid of the switching? I want the switch to be fixed because I'm putting it in a casing and switching the shield could be a great hassle and untidy. I need some suggestions, Thanks in advance! :slight_smile:

It looks like you need to use the hardware serial (i.e. the usual USB connection and Pin 0 and 1) for communication with the serial monitor and use the softwareSerial library to create a second serial connection to communicate with the GSM device.

Of course if you are using an Arduino Mega it has 4 hardware serial connections.

...R

Given the following, can you see a serious problem in your code?

[...]
char Rx_data[50];
[...]

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

[...]
    clearString(Rx_data);
[...]

Also besides the horror of mixing Strings and character arrays, a bit of advice: When you use a character array (such as in readSerialString), don't forget to terminate the string. That is, after you're done reading and before you return use:

strArray[i] = '\0';

FWIW,

Brad
KF7FER

Apart from the problem of clearing 100 bytes of a 50-byte array, all you have to do is set the first byte to \0, none of the others matter.


Rob