Software serial not sending series of data properly

Hello i am trying to use arduino nano as sender and arduino mega 2560 as receiver. The sender takes input from a sensor and give the data to receive via serial communication. Following is code for sender

void USART0_init(int bd)
{
  float crystal = 16000000.0;
  int val = (crystal / (16.0 * bd)) - 1;
  UBRR0H = 0;
  UBRR0L = val;
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = ((1 << UCSZ00) | (1 << UCSZ01));
}
void USART_send0( unsigned char data)
{
  while (!(UCSR0A & (1 << UDRE0)));
  UDR0 = data;
}
unsigned char USART_receive0()
{
  while (!(UCSR0A & (1 << 7)));
  return UDR0;
}

void USART_putstring0(char* StringPtr)
{
  while (*StringPtr != 0x00)
  {
    USART_send0(*StringPtr);
    StringPtr++;
  }
}
void print_float(float num1)
{
  boolean isNegative = true;
  byte i = abs(num1);
  if (num1 < 0)
  {
    isNegative = true;
    num1 = -num1;
}
  else
    isNegative = false;
  byte fraction = (num1 - i) * 100;
  
  USART_send0('s');
  if (isNegative)
  {
   
    USART_send0('-');
   
  }
  else
  {
    USART_send0('+');
  }
 
  USART_send0(i);
 
  USART_send0(fraction);
}

And i am using software serial in mega

float get_val_software()
{
  char temp = 's';
  if (!skip)
  {
    
    temp = sofser.read();
    Serial.print("Its temp:");
    Serial.println(temp); 
  }
  if ( temp == 's' || skip)
  {
    char sign = sofser.read();
   Serial.print(" Its sign: ");
    Serial.println(sign); 
    byte i = sofser.read();
    Serial.print("  Its i:");
    Serial.println(i); 
    byte fraction = sofser.read();
    Serial.print("  Its fraction:");
    Serial.println(fraction);
    if (sign == '+')
      return (i + (fraction / 100.0));
    else if (sign == '-')
      return -(i + (fraction / 100.0));
    else
    {
      while (sofser.read() != 's');
    skip = 1;
    return get_val_software();
    }
  }
  else
  {
    while (sofser.read() != 's');
    skip = 1;
    return get_val_software();
  }
}

The receiver sends a signal using a digital pin asking sender to send the data. Though this is working properly with hardware rx of receiver but goes out of sync for software serial. Though resetting the sender does bring it in sync for for a moment before it goes out of sync again. I am using pin 10 as rx which is supports change interrupts. Cannot use hardware serial as i have 6 senders. Baud rate used is 9600.
Any help will be appreciated :slight_smile:

Cannot use hardware serial as i have 6 senders.

How many instances of SoftwareSerial are you planning to run on the Mega ?

Why aren’t you using one of the three spare HardwareSerial ports on the Mega?

Have a look at the examples in Serial Input Basics - simple reliable 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

…R

I have just the checked the serial communication between NANO and MEGA using the following hardware setup and sketches. I have to use SUART Port for NANO as the UART Port is engaged with IDE/SM for sketch uploading/debugging. This is to establish that MEGA is working well with DPin-10 as a Software Serial Receive Pin. (edit)

STX(3) of NANO --------------> SRX (10) of MEGA
SRX(2) of NANO <-------------- STX (11) of MEGA
GND of NANO <----------------> GND od MEGA

NANO Sketch

#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);

void setup() 
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop() 
{
  byte n = Serial.available();
  if (n !=0)
  {
    char x = Serial.read();
    SUART.print(x);
  }
}

MEGA Sketch:

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);  // SRX = DPin10, STX = DPin-11
void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);

}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    char x = SUART.read();
    Serial.print(x);
  }
}

I have just the checked the serial communication between NANO and MEGA using the following hardware setup and sketches.

Sorry, but I am not sure what that is supposed to prove

The OP says that he needs to use 6 senders which implies that he needs to use 3 instances of a soft UART on the Mega along with 3 of the hardware UARTs

I have understood from OP's post that he has problem to receive data over DPin-10 of MEGA when it is being used as receive pin of Software Serial Port.

My Post#3 is submitted to convey the message that there is no problem to receive data over DPin-10 of MEGA when it has been used as receive pin of Software Serial Port.

Did you note this ?

The receiver sends a signal using a digital pin asking sender to send the data.

and the fact that he/she is using port manipulation to manage the serial port(s)

UKHeliBob:
Did you note this ?and the fact that he/she is using port manipulation to manage the serial port(s)

That is the issue which works well; but, the problem is in SRX-pin (DPin-10) of MEGA.