Search code examples
arduinouartusart

Device not responding to UART commands


I am using an Arduino mega2560 and an EZO EC(Electrical Conductivity) and am trying to send a command using the Serial.print() function. I am using the Arduino IDE 1.6.7.

I have some code that seems to work fine which I found online. But I want to know why my code will not work. The EC sensor does not seem to get the data I am sending. No data seems to being sent.

I know it is not my connection as I have tested the setup with the code that works it runs fine and as expected.

Here is the code I found online that works:

String inputstring = "";                              
String sensorstring = "";                             
boolean input_string_complete = false;              
boolean sensor_string_complete = false;             

void setup() {                                        
  Serial.begin(9600);                                 
  Serial3.begin(9600);                                
  inputstring.reserve(10);                           
  sensorstring.reserve(30);                          

}


void serialEvent() {                                 
  inputstring = Serial.readStringUntil(13);          
  input_string_complete = true;                       
}


void serialEvent3() {                                
  sensorstring = Serial3.readStringUntil(13);        
  sensor_string_complete = true;                      
}


void loop() {                                        
  float wt = 28.9;
  String tempCal = "T,";
  tempCal += wt;
  if (input_string_complete == true) {                
    Serial3.print(inputstring);
    Serial3.print("\r");                             
    inputstring = "";                                 
    input_string_complete = false;                    
  }

  if (sensor_string_complete == true) {               
    if (isdigit(sensorstring[0]) == false) {          
      Serial.println(sensorstring);                  
    }
    else                                             
      print_EC_data();                                
    }
    sensorstring = "";                                
    sensor_string_complete = false;                   
  }
}


void print_EC_data(void) {                           

  char sensorstring_array[30];                        
  char *EC;                                          
  char *TDS;                                          
  char *SAL;                                         
  char *GRAV;                                         
  float f_ec;                                         

  sensorstring.toCharArray(sensorstring_array, 30); 
  EC = strtok(sensorstring_array, ",");              
  TDS = strtok(NULL, ",");                            
  SAL = strtok(NULL, ",");                            
  GRAV = strtok(NULL, ",");                         

  Serial.print("EC:");                                
  Serial.println(EC);                                 
  Serial.print("TDS:");                             
  Serial.println(TDS);                               
  Serial.print("SAL:");                            
  Serial.println(SAL);                                
  Serial.print("GRAV:");                             
  Serial.println(GRAV);                               
  Serial.println();                                  

//f_ec= atof(EC);                                     
}

Here is my code:

void setup() {
  Serial.begin(9600);
  Serial3.print(9600);
}
void loop() {
  Serial3.print("R/r");
  Serial.print("R/r");
  delay(2000);
}

The Serial3.print just doesn't get sent to the sensor. But the other code also sends a string using the Serial3.print() function an it works fine. I do not know what I am doing wrong.

I understand that I need to write a procedure to take in anything that comes in from the sensor. But nothing seems to be even sent to the sensor in the first place!

Any help would be greatly appreciated. Thank you


Solution

  • You're using slash, not backslash. Change this

    Serial3.print("R/r");
    

    to this:

    Serial3.print("R\r");
    

    And don't use the String class. It'll mess you up. :) Just use char arrays, and fill them up as characters are available in loop. When the \r finally arrives, process the array:

    char inputString[16];
    int  inputStringIndex = 0;
    
    void loop()
    {
      if (Serial.available()) {
        char c = Serial.read();
        if (c == '\r') {
          inputString[ inputStringIndex ] = '\0'; // NUL-terminate string
          inputStringIndex = 0; // reset for next time
    
          Serial3.print( inputString );
          Serial3.print( '\r' );
    
        } else if (inputStringIndex < sizeof(inputString)-1) {
          inputString[ inputStringIndex++ ] = c;
        }
      }
    

    Do something similar for the response line. This will also avoid blocking inside a SerialEvent. :P