PaulS:
But the idea is still there that when you invoke Serial.available() while the sw serial port is still receiving data, you wont be able to get the whole data.
Rubbish. There is no way to know when the serial port has finished receiving data, because that only makes sense in terms of one specific packet.
However what I want is NOT a magic way.
Yes, it is.
I simply want to be able to port my code originally written in C for the PIC mcu to the arduino platform.
We haven't seen any code, yet.
The serial port is an open ended connection. Data can arrive at any time. As each byte arrives, it is put in a buffer. Determine how much data is in the buffer, using Serial.available() (or the instance of SoftwareSerial's available() method). Read any buffered data using Serial.read() (or the equivalent software instance's read() method). Data can arrive while you are reading. That data will be put in the buffer, on the other end you are reading from.
There are no problems reading from the buffer while the instance is stuffing data in the other end.
Is it very hard to understand that you can determine the end of transmission of a particular string when after the last character of data is received, and no other character follows that after a specified time will already be indicative of end of string or transmission?
Let's put it this way ....
I send a string, say "1234567890" (with no LF or CR after the 0) at a constant baud rate. Between sending the characters 1 and 2, there is a certain time that elapsed, say 100usecs and this is constant between each characters betwen 2 and 3 and so on. So this goes on for the other characters until the character "0". After receiving the character "0", if after 100 usecs or more, there are no new characters received, then this would already be the marker that indicates that the whole string has been received. This way, any variable length string can be read w/o any end of string terminators.
The way I do this on the h/w uart port (interrupt driven serial reception) is when the interrupt happens as a new character is received, I set a h/w timer that will also interrupt when the set time has elapsed. This timer is always reset to 0 each time a new character is received within the timeout period. If the last character was received, no new character will reset timer and hence after the elapse time, it will cauase an interrupt. During this timer interrupt, I raise a flag to inidicate that all data from the unknown length string has been received. In the main loop, this flag is always polled, and when the buffer is read, the flag is cleared.
What I want is to be able to also do this on the SoftwareSerial uart.
If I do this with sofwareserial.available() while in the middle of receiving data, softwareserial would only return the partial numbers of characters and then I wont be able to capture the whole string.