Serial port

Hello everyone, for some time now I've been designing my solar system in the mountains using an Arduino to read the data and a raspberry to create the web page.
I'm using serial communication to send data from the Arduino to the raspberry and I came across an unusual problem that I can't solve.

This and the code I'm using, I'm reading the data with an RJ-45:

#include <ModbusMaster.h>

#define MAX485_DE      3
#define MAX485_RE_NEG  2
float MaximumInputVoltPVToday = 0;
float MinimumInputVoltPVToday = 0;
float MaximumBatteryVoltToday = 0;
float MinimumBatteryVoltToday = 0;
float ConsumedEnergyToday = 0;
float GeneratedEnergyToday = 0;
float TotalConsumed = 0;
float TotalGenerated = 0;
// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  Serial.begin(115200);
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);
  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  // Modbus communication runs at 115200 baud
  // Modbus slave ID 1
  node.begin(1, Serial);
  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}
//-----------------------------------------
 void Dati (void);
 void loop()
{
  
  Dati();
  delay(5000);
}


void Dati ()
{
    uint8_t resultMain;
    resultMain = node.readInputRegisters(0x3300,19);
  if (resultMain == node.ku8MBSuccess)
  {
    Serial.println("------------");
    //Serial.print("Maximum input volt(PV) today: ");
    MaximumInputVoltPVToday = (node.getResponseBuffer(0x00)/100.0f);
    Serial.println(MaximumInputVoltPVToday);
    //-----------------------------------------------
    //Serial.print("Minimum input volt(PV) today: ");
    MinimumInputVoltPVToday = (node.getResponseBuffer(0x01)/100.0f);
    Serial.println(MinimumInputVoltPVToday);
    //---------------------------------------------
    //Serial.print("Maximum battery volt today: ");
    MaximumBatteryVoltToday = (node.getResponseBuffer(0x02)/100.0f);
    Serial.println(MaximumBatteryVoltToday);
    //-----------------------------------------------
    //Serial.print("Minimum battery volt today: ");
    MinimumBatteryVoltToday = (node.getResponseBuffer(0x03)/100.0f);
    Serial.println(MinimumBatteryVoltToday);
    //----------------------------------------------- 
    //Serial.print("Consumed energy today: ");
    ConsumedEnergyToday = (node.getResponseBuffer(0x04)/100.0f);
    Serial.println(ConsumedEnergyToday);
    //-----------------------------------------------
   // Serial.print("Generated energy today: ");
    GeneratedEnergyToday = (node.getResponseBuffer(0x0C)/100.0f);
    Serial.println(GeneratedEnergyToday);
    //-----------------------------------------------
    //Serial.print("Total consumed: ");
    TotalConsumed = (node.getResponseBuffer(0x0A)/100.0f);
    Serial.println(TotalConsumed);
    //-----------------------------------------------
    //Serial.print("Total generated: ");
    TotalGenerated = (node.getResponseBuffer(0x12)/100.0f);
    Serial.println(TotalGenerated);
  }
}

And in the serial port I read:

3??--------------
116.93
0.33
14.92
11.63
0.03
0.38
14.93
56.47

The values are right but I don't understand why I have that "dirt" at the beginning. Does anybody know what it's from? Clearly the raspberry, not knowing what it is, gives me the wrong thing and it's stuck...
Does anyone know how to eliminate it to get a clean data transmission?
Thank you very much, everyone :slight_smile:

It is a bit unclear what is going on.
Is the ouput what you see on the Pi ?
The problem might be in your Pi code.

You try to send eight values and you are saying that you get the correct eight values except that there is some crud at the very start. So what happens as you go round the loop do you get crud everytime or only the very first time?

I would simplify things just send a single value in the loop to start.
It also might be an idea to put the delay before calling Dati() just to see if it makes a difference.

Thank you very much for the answer, for now the raspberry is not connected so it does not affect what I put and the serial that I read in the communication between arduino and the module Rj-45.I tried to do as you said but the problem is not solved, now I try with a mega arduino, with a serial I read the values and with another I transmit them to the rasperry, I want to see if I can solve...

Sooner or later you might think about making the communication a bit more robust so that (for example) the raspi could tolerate some junk and not get “stuck”.

This would help for other glitches you mayn’t have esperienced yet.

Assuming you have access and can modify the receiving code.

a7

Of course, I can modify the code of the raspberry to delete the data you don't need, I was just asking if it was possible to solve this problem already with Arduino, maybe it was a known problem... I'll manage by cleaning up once the code is received from the raspberry... ::slight_smile:

To expand on what a7 said. You can make reception more robust by using start and end markers in the serial stream as shown in the serial input basics tutorial. Any data that is not framed by the start and end markers is not valid.