SoftwareSerial buffer question

If I write to a port:

portOne.write(65);

Will the buffer be cleared?

I have two Arduinos connected together and both are writing to each other. I am simultaneously checking if anything is available in the buffer.

void loop()
{
    Serial.print( portOne.available() );
    Serial.println( " byte available" );

    portOne.write( 65 );
}

My suspicion is available() cannot be called in the same loop as write() because available() counts as reading from the port. I know that you cannot read and write in the same loop.

But could it be that write() just clears the buffer?

The output from Serial is completely separate from the input to Serial. So, no matter what you write or print it will not show up as being available()

Please post a complete sketch illustrating whatever problem you are experiencing

you can read and write in the same loop with a SoftwareSerial instance... not sure what you are saying.

serial data is transmitted and received over separate signal paths that don't interfere with one another an allow simultaneous (duplex) transmission. the serial interface on the Arduino processor has separate buffers for transmission and receiving. this is not true when using an RS485 transceiver external to the processor, for example

of course transmission and reception can be done within the same loop. there's not need to check before transmission, just do a .write() or .print(). of course reception should check if .available() to know if there is something to read.

software serial is intended to behave the same as the hardware serial interface

Not quite, software serial (possibly depending on which library you use) has the problem of not being able to do a loopback test, where you connect the transmits pin directly to the receive pin and attempt to receive what you are sending.

I’ve been working on this for a month now and haven’t found an example of read and write working simultaneously. If you have one, I’d love to see it.

Thanks

I am using two Arduinos that communicate to each other. My setup is not a loopback where the TX and RX pins are connected.

You do not need to make any specific precautions. Just read and write.

Please show your code

where is your "checking" in this code? You don't check, you just a display...

What baud rate are you using? Generally stick to 9600 baud or lower with SoftwareSerial.

I've seen some discussion that SoftwareSerial is only usable as half-duplex, because of its reliance on interrupts and the amount of code that needs to be executed by the receive and transmit routines, there just isn't time to reliably do both simultaneously.

Would really help if you could post your code for BOTH arduinos, getting two boards to work together reliably is not the easiest task.

available() “checks”/ looks at the RX buffer, it returns the number of bytes in the RX buffer

available() does tell you the number of characters available in the receive buffer, but does not affect the contents of the buffer.

write() should have no effect at all on the receive buffer, other than possibly causing an overflow in the receive buffer if you write so much that you exceed the size of the transmit buffer. At that point the write() function will not return until space becomes available in the transmit buffer, which can allow enough time to fill the receive buffer completely and start losing characters.

My baud rate is 9600.

Reading and writing cannot occur simultaneously, yes.

Both arduinos have the same code:

include SoftwareSerial

SoftwareSerial portOne(2, 3);

void setup()
{
   Serial.begin(9600);
   portOne.begin(9600);
}
void loop()
{
   Serial.print(portOne.available());
   Serial.println(“ byte available”);

   portOne.write(65);
}

I wrote the code above off memory cuz I’m on away from the computer.

I expect both RX buffers to fill up to their max bytes of 64 because I am not reading the RX buffer.

I am using available() to see how many bytes are in the RX buffer.

The outcome from running this code is: both arduinos print “0 byte available”

All of this makes sense and is consistent to my understanding.

That code, running on both boards, is basically a loopback test - you are saturating the transmit so that it runs continuously, and you will not be able to receive simultaneously with SoftwareSerial.

Communications between two boards is generally one boards sends a complete message, second board processes the message and responds, first boards processes that message and responds, etc.

have the pins been configured as OUTPUT and INPUT?

SoftwareSerial handles that.

There are no "simultaneous" reading and writing in your code.

The reason that the code does receive nothing is not in "simultaneous" reading, I suspect that you merely connect your boards incorrectly.

Also I apologize if my replies come off as rude, I just don’t like using and think it’s unnecessary to use fluff filler words.

Thank you for taking the time to help me.

Most of my code has been focused on saturating the TX line.

Are you saying that I should not saturate the TX line because it doesn’t allow the RX line to be read?