A Problem with the UART Communication

Hi Guys
I have a HVAC control unit which is based on Atmega324p . This device is connected to other device (Room controller) . I used a serial communication between the devices and every thing ok, but when an electrical noises occur the devices freez.
I send and receive the info by using "Serial.write(buffer, count ); Serial.readBytes(buffer, count );"
I made a simulation in the office (by using a spark generator) and when looking to the communication bits (On the oscilloscope) I had these truths:

1- sometimes, The connection is interrupted but on the oscilloscope the bits have a normal flow .in this
status the loops works normally bus no communication.
2- sometimes,the connection is interrupted and the code freezes ,(I'm planning to use software reset by the
watchdog or something else).
I need to solve this issues by the software (without using any external hardware)

please help me to solve it and thank you very much.

It's not at all clear from your description if the problem is caused by incorrect data being received - incorrect because it has been garbled by interference, OR, if the problem is due to the interference causing the microprocessor to crash.

Serial.readBytes() is a blocking function and is probably not appropriate in a situation where the data flow might be interrupted.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

It makes debugging much easier if you send data in human readable form. I would only send binary data if it was the only way to achieve the required performance.

If you need more assistance please post your two programs.

...R

Just to be sure it's not how you're sending/parsing the serial packets, I suggest using this reliable and easy to use serial transfer library (available through the Arduino IDE Libraries Manager).

Example TX Code:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Code:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Give it a shot and let us know what happens

Does your code properly process and handle bad data, are buffers limited to prevent overflow. Try different power supplies, they could be part of your problem. if the communication wires are more then 1 meter round trip you need buffers, they become great antennas. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil