send and receive data over RS485

Hello everyone,
I am working on a project using the RS485. here is how it should work:

  • the Arduino sends 3 values to the computer via SoftwareSerial with intervals of one second
  • between those actions it should listen for user input and if received, it should update the output voltage of a DAC.
    It sends the values without any problem, but when it does not receive any value I send. I know that the RS485 is half duplex and my guess is that sending values regularly from the Arduino might clear the buffer.
    If that is the problem I have no idea how to go around it.
    Thank you in advance!
    Here is the code I wrote:
#include <SPI.h>
#include <SoftwareSerial.h>

/*
 * only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, 
 * A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
 * 
 */
const int rx = 10;
const int tx = 11;
const int ep = 3;
const bool tra = HIGH;  //transmit state
const bool rec = LOW;   //receive state

const int sendVolt = 10;
const int readTemp1 = A5;
const int readTemp2 = A6;
const int readTemp3 = A7;

int sendVal = 0;
int readVal = 0;
int val = 0;

int input;

//----------SPI----------//
byte VoltByte0 = 0;
byte VoltByte1 = 0;

/*
DAC 1 Voltage
MCP4921 pin —> Arduino NANO
1 Vdd-> 5 volt power supply – can come from Arduino
2 CS->Arduino pin 10/$SS$
3 SCK->Arduino pin 13/SCK
4 SDI->Arduino pin 11/MOSI
5 LDAC->Ground
6 Vrefa->5 volt power supply – for reference use only
7 AVss->Ground
8 Vouta->Analog voltage output signal (DAC)
*/

/*
The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)

MAX485---->arduino
DI->11 (TX)
RO->10 (RX)
DE,RE-> 7

rsUSB----->MAX485
A->A
B->B
*/

//----------- for sending values at a given rate-----------
unsigned long previousMillis = 0;
int interval = 1000; // time to wait between transmission of temperatures
bool STOP = false;


SoftwareSerial mySerial(rx, tx); // RX, TX

void setup()
{
    pinMode(sendVolt, OUTPUT);
    pinMode(ep, OUTPUT);
    digitalWrite(ep, rec);
    
    mySerial.begin(9600);
    Serial.begin(9600);
    SPI.begin();
    SPI.setBitOrder(MSBFIRST);
    SPI.setClockDivider(SPI_CLOCK_DIV2);
    digitalWrite(sendVolt, HIGH);
}

void loop()
{
    RSVoltage();
}


void RSVoltage()
{
    if(!STOP)
    {
        if(mySerial.available()) // for some reason this condition is never true. I don't know why
        {
          Serial.println("available");
            sendVal=mySerial.parseInt();
            if(sendVal == 0){
                STOP = true;
            }
            mySerial.println("1");

            //----------send voltage----------//
            //----- sendVal will be 0 for OFF-reset ----------//
            val=sendVal;
            
            VoltByte0 = byte(sendVal);
            sendVal = sendVal >> 8;
            VoltByte1 = byte(sendVal | 0b00110000);
            
            digitalWrite(sendVolt, LOW);
            SPI.transfer(VoltByte1);
            SPI.transfer(VoltByte0);
            digitalWrite(sendVolt, HIGH);
        }
        else
        if(!mySerial.available()) //redundant check to avoid clearing the buffer
        {
          String out = "";

            //-------creating some values for testing------------------------
            digitalWrite(ep, tra);
            out += (readVal-1432);
            out += " ";
            out += (-readVal*readVal+12500);
            out += " ";
            out += (readVal++);
            if(!mySerial.available()) //redundant check to avoid clearing the buffer
              mySerial.println(out);       
            digitalWrite(ep, rec);
            //-------------------------------------
        }
        delay(interval);
     
    }
    else
    {
      Serial.println("stopped");
      if(mySerial.available())
        STOP = false;
    }
}

If you use a hardware Serial, e.g. on an Arduino Mega, you can find out whether problems are caused by your code or by SoftwareTerminal. Did you also try AltSoftSerial?