have to receive tow different data form one arduino to another

i want to receive two data (string), to one arduino to another , for that i am try many different logics, but i did not got solution . finally i got an idea to use serial and i2c communication together, here is my sender and receiver code.

is that possible to communicate with this idea, to receive two data.

i got one data ( data1) but not other (data2).

//sender
#include <Wire.h>

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



String str;
unsigned long data1 = 1;
float data2= 38.243;
String stringVal="";
char charVal[10];

void setup() {
  
  Serial.begin(9600);
  Wire.begin();
  portTwo.begin(9600);
  //data2()
  
  
}

void loop() {
  
  
  
  str = String(data1);
   
   portTwo.println(str);
   
   delay(1000);
   
 dtostrf(data2, 4, 3, charVal);
  Serial.print("charVal(float):");
  for ( int i = 0; i < sizeof(charVal);i++)
  {
    Serial.print(charVal[i]);
  }
  Serial.println();
  for(int i =0; i< sizeof(charVal); i++)
  {
    stringVal+= charVal[i];
  }
  Serial.print("stringVal(float):");
  Serial.println(stringVal); 
  Wire.beginTransmission(4);
  Wire.println(stringVal);
  Wire.endTransmission(); 
  
//delay(1000);
 
  
  

 
   //Serial.print("time:");
   //Serial.println(str);
  
}
// receiver

#include <Wire.h> 

#include <SoftwareSerial.h> 
SoftwareSerial mySerial(2,3); //rx tx 

String inString = "";    // string to hold input
String inString2 = "";    // string to hold input
float data2=0;


unsigned long data1;



void setup() 
{ 
  Wire.begin(4); // join i2c bus with address #4 
  Wire.onReceive(receiveEvent); // register event 
  Serial.begin(9600); 
  mySerial.begin(9600); // join i2c bus (address optional for master) // start serial for output 
  //delay(1000); 
}

void loop() 
{ 
  while(mySerial.available() >0) 
  { 
   int inChar = mySerial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("data1");
      Serial.println(inString.toInt());
      data1 = inString.toInt();
      inString = "";
    }delay(1000);
    
} 
} // function that executes whenever data is received from master // this function is registered as an event, see setup() 

void receiveEvent(int howMany) 
{
  while(1 < Wire.available()) // loop through all but the last 
{
    int inChar = Wire.read();
    if (inChar != '\n') {
      inString2 += (char)inChar;
    }
    else{
      Serial.print("input string:");
      Serial.print(inString);
      Serial.print("\tdata2");
      data2 = inString.toFloat();
      Serial.println(data2);
      inString2 = "";
    }
  }
      }

How are the two Arduinos connected? You can send ALL the data via the Serial process or via the I2C process. It is silly to use both.

Which one do you prefer to use?

first time i used serial communication. but by serial communication it did not worked , so i thought if i use separate protocol to send two different data(string). thats why i am trying to send data1 for serial and data2 from i2c. is that right ? is arduino work with this?

is that right ?

No. There is no reason that sending two values, as a single string, can not be made to work.

is arduino work with this?

It can be made to work. But, it's like hiring a cab to take your coat to work while you drive, with your briefcase in your car. Stupid and wasteful. In the end, you have to get your coat from the cab and your briefcase from your car, so having two different transport mechanisms is stupid.

float pi = 3.14159;
char piStg[10];
dtorstrf(pi, 8, 5, piStg);

int peachCount = 32;

char compMsg[80];
sprintf(compMsg, "<PI: %s, Peaches: %d>", piStg, peachCount);

Serial.print(compMsg);

will send "<PI: 3.14159, Peaches: 32>" to the serial port, and on to the other Arduino. Two values, one string.

Receiving, storing, and parsing that on the other end is not difficult.

thank you very much PaulS...

so basically i have to combine two string form one end and split that on other..

but how can i combine two string cause in first string (float value string) there is "." in between sting , so how can i combine and split this type of string?

plz guide me