Problem reading serial (2 bytes, 1200 baud 8N2)

Hello blynkers,

I’m stuck with my project so I need your help. :slight_smile:

I’m trying to control my pellet stove with arduino mega 2560. At the beginning I connected my computer with stove’s serial interface and perform plenty of tests and I find out what I should send over serial to the stove, and what stove send me back. When I send 2 bytes, stove responds with 2 bytes. I don’t need first byte from the respond, only second.

Serial0 on the mega is used for serial monitor, Serial1 is used for communication.
Baudrate: 1200
Data bits: 8
Parity: None
Stop bits: 2

If I send same 2 byte command every 5 seconds, stove should always respond with same 2 bytes, but for some reason, I’m not able to read responds correctly. Forst couple of cycles I’m getting correct values, and than something goes wrong, and responds mess up. After 20-30 cycles single correct value is recieved again, but I have to find reliable solution for reading second byte from the respond. Can you take a look to my code? Project is much complex but I tried to simplified it just for serial reading issue demonstration. Firstly I tried to read serial in loop but then I saw in documentation that blynk doesn’t like that so I modified loop to be 2 lines only, and add timer… What else I can try? Thanks in advance.

#define BLYNK_PRINT Serial          
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>    
#include <SimpleTimer.h>

SimpleTimer timer;

char auth[] = "xxx";

void setup()
{
  //Serial for serial monitor
  Serial.begin(1200, SERIAL_8N2);   

  //Serial1 for communication with stove
  Serial1.begin(1200, SERIAL_8N2);  
  
  Blynk.begin(auth);
  
  // execute refreshWidget every 5 seconds
  timer.setInterval(5000L, refreshWidget); 
}

void refreshWidget(){        
    
    //executing readFunction with 2 bytes as parameters return one int
    int valueWidget = readFunction(0x00, 0x21); 
    Blynk.virtualWrite(V10, valueWidget);
    Serial.print("Second received byte is ");
    Serial.println(valueWidget, HEX);
}

int readFunction(byte firstByte, byte secondByte) //
{ 
  //array of 2 bytes need to be sent over serial
  byte readCommand[] = {firstByte, secondByte};
  Serial1.write(readCommand, sizeof(readCommand));

  //stove responds also sending back 2 bytes
  byte stoveRespond[2];

  // read data only when you receive 2 bytes:
        if (Serial1.available() >= 2) {
          for (int i=0; i<2; i++) {
          stoveRespond[i] = Serial1.read();
          }
  }

  //return only second received byte as int
  return (int)stoveRespond[1]; 
}

void loop()
{
  Blynk.run();
  timer.run();
}

Try setting up another timer faster than 1 second. Then do your read serial.
looks like the response is already passed by before you get a chance to capture the serial info.

1 Like

Hi friend. I have simmilar problem as yours. How did you solve it?

@iDiesel This is an old topic. Please create your own topic with plenty of details (same as XYZ is not enough). Thank you.