Trouble sending commands from serial to serial1 [SOLVED]

I am working on integrating an Atlas Scientific Dissolved Oxygen sensor with a Photon (on SparkFun Redboard). I can receive output FROM the sensor board and display it in Serial Terminal (on PC). However, sending commands from serial terminal (serial) to device on Serial1 is problematic.

If I hard cycle the power on the sensor, one command will work. Then all commands generate *ER (error) response.

Potentially Useful Facts:
Sensor Board: UART mode, 9600 bps 8 bit no parity 1 stop bit. Output is string type
Connected to Photon Redboard. RX --> TX, TX --> RX

Code (have fiddled with longer code, but this nicely boiled down code does same thing):

void setup(){                                                                
     Serial.begin(9600);                                                     
     Serial1.begin(9600, SERIAL_8N1);                                                    
     delay(3000);  // allow time for sensor to boot up
     }

void loop(){
    // read everything from pc serial terminal, send directly to serial1
    while(Serial.available())
        Serial1.write((char)Serial.read());    // write does no fancy stuff like print might

    // read everything from serial1 (sensor board) and send directly, char for char, to serial (pc terminal)
    while(Serial1.available())
        Serial.write((char)Serial1.read()); 
    
    // delay between readings. sensor isn't supah fast
    delay(1000);
}

OUTPUT: (note, first command succeeds after a hard restart of sensor, as in: pull power line out, reseat. If I reflash same code without hard repowering sensor, even first command fails. Have tried this with various commands found in sensor documentation)
4.35
4.35
4.33
name,?
?NAME, dobot01
*OK
4.35
4.36
name,?
*ER

What terminal program are you using?
How does it terminate any transmission (CR, CR/LF, not at all)?
Your output suggests you do get CR/LF from terminal and your sensor.
But the sensor only expects a CR, so the extra LF will invalidate the next command.

You might want to read the full command from Serial before you send any data to the sensor. Only once you collected all characters for the command send the complete command at once.
For that you could use a terminator character and use Serial.readString() or Serial.readStringUntil()

1 Like

The programming is a little challenging because I’m programming it from Oregon and the device is in another lab near Boulder (YAY… cloud technology). I’ve been suspecting a stray endline and (having not REALLY slung code in about 17 years) trying to figure out how to handle it.

The device is still a little bit strange in its behavior (I think for other reasons, to be chased down separately), but it’s working consistently with a new update in which I check every incoming character from serial terminal and do not transmit endline. As such:

char input_char = '\0';

void setup(){                                                                
     Serial.begin(9600);                                                     
     Serial1.begin(9600, SERIAL_8N1);                                                    
     delay(3000);  // allow time for sensor to boot up
     }

void loop(){
    // read everything from pc serial terminal, send to serial1
    while(Serial.available()) {
        input_char = (char)Serial.read();
        ***// forward all characters EXCEPT endline.***
        ***if (input_char != '\n')***
        Serial1.write(input_char);
    }

    // read everything from serial1 (sensor board) and send directly, char for char, to serial (pc terminal)
    while(Serial1.available())
        Serial.write((char)Serial1.read()); 
    
    // delay between readings. sensor isn't supah fast
    delay(1000);
}