Serial1.available() Stange behaviour in loop

Hello everyone, I’m new to device os but I’m facing something a bit odd on my Particle Electron which is running deviceOS 3.3.0. As you can see below in the loop I’m triggering an external function and the Particle.publish() when there is data on the serial buffer with while(Serial1.available()) this code works perfectly fine on Arduino (excluding the Particle.publish() of course) BUT for some reason I got executed everything that is inside the while even if there is nothing connected to serial1 of the electron, this happen even immediately after the boot. Did someone encountered a problem like this on the Electron ? Do I need to do something to handle this while loop properly ? Is there a reason for Serial1 becoming available even if no data is sent on that serial port ? Thanks a lot to everyone will join

void loop() {
  // The core of your code will likely live here.
    while(Serial1.available()) {
    SerialBuffer = Serial1.readString();
    Serial5.println(SerialBuffer);
    packet p = parseString(SerialBuffer);
    Serial5.println("*-> AT Response = " + p.at.atcommand);
    Serial5.println("*-> AI service = " + p.at.AIservice);
    Particle.publish("LIP Payload: ",String(p.lip));
  }
}

It could be that some transient signal “crosstalk” puts some chaff into the RX buffer - particularly when your pins are “floating”.

One way to remove that would be adding this to the end of your setup()

  while(Serial1.read() >= 0);

This will flush out any debris in the RX buffer before you move on to your loop().

Thank you so much for your help. I figured out the problem and it was hardware related, a faulty jumper cable on the serial pin caused the issue, but I kept your code suggestion in the code, just to be sure.

Thanks a lot !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.