Your UNO sends up to 4 characters-- 3 ascii values and the '\n' new line. "xxx\n"
The for loop in the reading routine is an error. You should just read the incoming data to the new line. You will pick up both values being sent without trying to read through twice.
atoi needs to be null terminated, and its best to add the terminator as you read in order to accommodate less than 3 digit values.
This example reads serial data on pin 16.
const byte numChars = 4;
char readBuf[numChars];
static byte ndx = 0;
byte tempf = 0;
byte humif = 0;
byte val = 0;
#define RXD2 16
#define TXD2 17
void setup()
{
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
Serial.println("Starting");
}
void loop()
{
if (Serial2.available())
{
//for (int i = 0; i < 2; i++)
//{
char c = Serial2.read();
if (c != '\n')
{
readBuf[ndx] = c;
ndx++;
readBuf[ndx] = '\0'; //null terminate next index
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
val += 1;
switch (val)
{
case 1:
{
tempf = atoi(readBuf);
ndx = 0;
Serial.println(tempf);
break;
}
case 2:
{
val = 0;
humif = atoi(readBuf);
ndx = 0;
Serial.println(humif);
break;
}
}
}
//}
}
}