Correpted Data in serial communication

Dear all,
I have an issue with my serial connection.

Im trying to get a fix amount of data from the Arduino via the serial connection.
Every 3 or 5 messages the content is corrupted.

Normally the tool should send a msg with a length of 44 but it is sending sometimes only a length between 25-33.

Sure, I can filter them out but Im missing values then.

Anyone has an idea what the error could be?

Receive function
bool RecDataRS232() 
{
  static byte ndx = 0;
  char endMarker = '\n';
  char RecData;
    
  while ((Serial.available() > 0) && (RS232NewData == false))
  {  
    RecData = Serial.read();
    
    if (RecData != endMarker)
    {
      RecRS232Char[ndx] = RecData;
      ndx++;
      if(ndx >= AmnRS232RecChars) 
      {
        ndx = AmnRS232RecChars - 1;
      }
    }
    else 
    {
      RecRS232Char[ndx] = '\0'; // terminate the string
      ndx = 0;
      RS232NewData = true;
    }
  }
  
  return RS232NewData;
}
Part in the MainLoop
if(RecDataRS232() == true)
{
  for(int i=0;i<sizeof(SelectionP1Char);i++)
  {
    SelectionP1Char[i] = RecRS232Char[i];
  }

  for(int i=0;i<(AmnRS232RecChars - sizeof(SelectionP1Char));i++)
  {
    SelectionP2Char[i] = RecRS232Char[i+sizeof(SelectionP1Char)];
  }
  
  if(strcmp(SelectionP1Char,"Val") == 0)
  {
    MeasToRS232TXBuff();
    Serial.println(SerialTXBuf);

    Serial.flush();
  }
}

Most likely an issue with the code. My guess is the code on line 1523 is wrong.

Also more info would be great.

I was fighting with the formating to involve my code :smiley:
sorry for that :smiley:

Then the problem is with the "tool", whatever that is, and not the Arduino?

Does the serial data have a format? Such as start of data marker, the data, and an end of data marker.

Here I use start and end of data markers.

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

to properly receive serial data.

The tool is the arduino

I'm sending a string with up to 30 digits without a end sign.

After the arduino receives this messages, it should send a string with 44 digits back,also without the end sign.

If the missing markers are the problem, should the communication not always be incorrect? The changing amount of digits which the arduino sends, doesn't makes sense to me.

Have a look at this tutorial: Serial Input Basics - updated

You say that like I'm stupid for not knowing it! :crazy_face:

Nono I'm the stupid guy because my code doesn't work :joy::joy:

My reply is for you to refer to post#8.

Hi,
What speed is set for both the sender and the receiver?

I was using this help for developing but I will check tomorrow again, also your comment with a code example.

Both are set to 112500.

Not standard. 115200 would be a better choice, unless you are using SoftwareSerial, which does not work at such high speeds.

Post ALL the code.

It was a typo sorry. Im using 115200.
Here is my completed Code.

Arduino.ino

#include "INA219.h"
#include "TorqueSens.h"
#include "RotaryAngleSens.h"
#include "EthernetCommunication.h"
#include "RS232Communication.h"

// ______________________________________________________________________________________________
// Global variable  
char SerialTXBuf[AmnSerialTXBufChars];
char RecRS232Char[AmnRS232RecChars];
bool RS232NewData = false;

bool ConnectionClimateChamberEstablished = false;

void setup(void) 
{  
  //RS232 connection with maximum baudrate for CANoe
  Serial.begin(115200);
  
  //RS232 connection timeout
  Serial.setTimeout(100);
  
  InitINA219();
  InitTorqueSens();
  
  //ConnectionClimateChamberEstablished = true;
}

void loop(void) 
{ 
  byte n = 0;
  
  static byte EthernetIPAddress[4];

  int RecEthernetPort;
  
  char SelectionP1Char[3];
  char SelectionP2Char[AmnRS232RecChars-sizeof(SelectionP1Char)];

  char *RecEthernetIP;
  
  
  if(RecDataRS232() == true)
  {
    for(int i=0;i<sizeof(SelectionP1Char);i++)
    {
      SelectionP1Char[i] = RecRS232Char[i];
    }

    for(int i=0;i<(AmnRS232RecChars - sizeof(SelectionP1Char));i++)
    {
      SelectionP2Char[i] = RecRS232Char[i+sizeof(SelectionP1Char)];
    }
    
    if(strcmp(SelectionP1Char,"Val") == 0)
    {
      MeasToRS232TXBuff();
      Serial.println(SerialTXBuf);

      Serial.flush();
    }
    else if(strcmp(SelectionP1Char,"ZPA") == 0) 
    {
      RotaryAngleMeasurement(1);  //Set Zero Point
    }
    else if(strcmp(SelectionP1Char,"IP|") == 0)
    {
      RecEthernetIP = strtok(SelectionP2Char,".:");

      while(RecEthernetIP != NULL)
      {  
        if(n < 4)
        {
          EthernetIPAddress[n] = (byte)atoi(RecEthernetIP);
        }
        else
        {
          RecEthernetPort = atoi(RecEthernetIP);
        }
       
        n = n + 1;
        
        RecEthernetIP = strtok(NULL,".:");
      }

      ConnectionClimateChamberEstablished = InitEthernet(EthernetIPAddress[0],EthernetIPAddress[1],EthernetIPAddress[2],EthernetIPAddress[3],RecEthernetPort);
      
      //Serial.print("Status of InitEthernet: ");
      //Serial.println(ConnectionClimateChamberEstablished);
      
      if(ConnectionClimateChamberEstablished == true)
      {
        Serial.print("ClimateChamberConnectionSuccessful");  
      }
      else
      {
        Serial.print("ClimateChamberConnectionFailed");
      }
       
    }
    else
    {

    }
    
    resetRS232TXBuf();
  }
  //delay(30);
}
RS232Communication.cpp

#include "RS232Communication.h"

#include "INA219.h"
#include "TorqueSens.h"
#include "RotaryAngleSens.h"
#include "ClimateChamber.h"

char MeasBuf[8];

bool RecDataRS232() 
{
  static byte ndx = 0;
  char endMarker = '\n';
  char RecData;
    
  while ((Serial.available() > 0) && (RS232NewData == false))
  {  
    RecData = Serial.read();
    
    if (RecData != endMarker)
    {
      RecRS232Char[ndx] = RecData;
      ndx++;
      if(ndx >= AmnRS232RecChars) 
      {
        ndx = AmnRS232RecChars - 1;
      }
    }
    else 
    {
      RecRS232Char[ndx] = '\0'; // terminate the string
      ndx = 0;
      RS232NewData = true;
    }
  }
  
  return RS232NewData;
}

void MeasToRS232TXBuff()
{
  ltoa(TorqueMeasurement(),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf);  
  
  ltoa(RotaryAngleMeasurement(0),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf);  
  
  ltoa(MeasurementINA219(1),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf); 
  
  ltoa(MeasurementINA219(2),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf); 
  
  ltoa(MeasurementINA219(3),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf);
  
  ltoa(MeasurementINA219(4),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf);

  ltoa(getClimaticChamberTemp(0),MeasBuf,10);
  strcat(SerialTXBuf,MeasBuf);
}

void resetRS232TXBuf()
{
  for(int i=0;i<sizeof(SerialTXBuf);i++)
  {
    SerialTXBuf[i] = '\0';
  }

  for(int i=0;i<sizeof(MeasBuf);i++)
  {
    MeasBuf[i] = '\0';
  }
  
  RS232NewData = false;
}

The problems seems not the receiving on the arduino.
I worked again through the tutorial and my code. After some debugging with the serial monitor, I saw that the data is not sending properly from the arduino :frowning:

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