Software serial returning number of available byte = 0, but still returning data when read from it

Hii
i am facing issues with library software serial.
according to the library, .available() function returns the number of bytes available for reading in the serial buffer. but here it is always returning 0 and breaking the loop with condition

 if myserial.available() > 0{
}

Here is the test program in short for referance
actually i am transmitting some data through modbus and reading its request, so the request1[]
is the request format of data which needs to be transmitted to modbus slave

//RX
#include <SoftwareSerial.h>
const byte rxpin = 3;
const byte txpin = 2;
byte c;
byte request1[] = { 0x1, 0x4, 0x00, 0x32, 0x00, 0x2, 0xD0, 0X04 };
SoftwareSerial myserial(rxpin, txpin);  //rx pin , tx pin of the device sending data

void setup() {
  myserial.begin(9600);
  Serial.begin(9600);
  pinMode(7, OUTPUT);

  digitalWrite(7, HIGH);
  myserial.write(request1, sizeof(request1));
  digitalWrite(7, LOW);
}

void loop() {
  if (myserial.available() > 0) {
    while (myserial.available() > 0) {
      Serial.print("No of bytes available for reading ::>");
      Serial.println(myserial.available());
      c = myserial.read();
      Serial.print("Value read in hex ::>");
      Serial.println(c, HEX);
    }
  }
}

here is the output at serial monitor

Interesting;

For every byte you receive at 9600 baud, you are expecting to print out around 60 bytes, also at 9600 baud.

ohk, but it has nothing to take with sofwareserial.available() function, and how it is breaking the loop if its alue is 0??

     Serial.println(Serial.available());

Would it make more sense if you printed the number of bytes available from mySerial ?

Why the nested if/while?

didn't you get the pins wrong (according to the comment)

when you do
myserial(rxpin, txpin);

  • rxPin is the pin on which to receive serial data ==> Tx pin of the device
  • txPin is the pin on which to transmit serial data ==> Rx pin of the device

why not
.available functions returns no of bytes available for reading. and the data received at that serial port is stored in buffer.

so it make sense
it tells whether data is available for reading or not

if condition is for checking whether data is available or not
and while is for read untill buffer is empty

A while does both :smiley:

i wrote that coments considering "the device sending data" for the arduino i am using
as it is sending the byte request.
sorry for confusion

do you have any clue about the solution for this problem??

well it does,
i will correct it,
many thanks

do u have any solution for the actual problem

Post your code with the corrections so far mentioned, and your observations.

#include <SoftwareSerial.h>
const byte rxpin = 3;
const byte txpin = 2;
byte c;
byte request1[] = { 0x1, 0x4, 0x00, 0x32, 0x00, 0x2, 0xD0, 0X04 };
SoftwareSerial myserial(rxpin, txpin);  

void setup() {
  myserial.begin(9600);
  Serial.begin(9600);
  pinMode(7, OUTPUT);

  digitalWrite(7, HIGH);
  myserial.write(request1, sizeof(request1));
  digitalWrite(7, LOW);
}

void loop() {
    while (myserial.available() > 0) {
      Serial.print("No of bytes available for reading ::>");
      Serial.println(Serial.available());
      c = myserial.read();
      Serial.print("Value read in hex ::>");
      Serial.println(c, HEX);
  }
}

can you confirm what is connected where ?
did you join grounds ?

yes
circuit is all rigt with proper grounding
i am using a re485 to uart converter with arduino and is using an energy meter as slave.

if you want to simulate the same problem, please use two arduino with software serial, programming one for sending continuous data nad with another receiving data at software serial port

the code seems fine(*) so I suspect the wiring or the power

  • (*) besides a few quirks

its really perfect, i just rechecked

well we want to trust you but it would be better for everyone if you were sharing a drawing of the circuit so that we all have a full view of the system.

are you sure it's 9600 bauds ? what about voltage ?

YES BAUD IS 9600, voltage is 5 volt for power and for logic also,
wait i am sharing circuit diagram