How is the handshaking usually with a max485 iIC?

I have a setup in which a raspberry pi is going to talk to a bunch of arduinos via RS485. All arduinos have those max485 breakout boards and the Rpi has a usb to 485 module.

I have the communication working for the most part. But still I have some difficulties. I am currently testing on my computer with about ~25cm wire for the bus. I noticed that on 115200BPS I'd get distorted transmissions. I dont know if that is normal I have not deliberately used a 120R on the one Rs485 module. (I dont know atm if they have those by default). On 9600BPS I have no distortions. Not that I can see.

Prior of sending a message I digitalWrite the transmission direction pin high. And I use a short timer to write it back to low. I am aware that this is not the best way to do this.

At first I tried to call this code continously:

if( Serial.availableForWrite() == 0 ) digitalWrite( transmissionLine, LOW ) ;

I thought that this did not entirely work, though ofcourse it may have been something different causing me the problems.

I still believe that it is the right approach and that I did something else wrong. But I am still wondering: Is this the right approach? And if not, what do people usually do with the transmission direction pins of 485 devices?

The data transmissions are only 3 bytes in size each, so I am ok to use the blocking flush() function if that does do the trick. The tasks of the arduinos are not extremely time critical and delays of 3 bytes on 9600BPS is deemed tolerable.

Kind regards,

Bas

When I played with an Arduino UNO, for transmission I simply set the control pin for RE and DE high to put the MAX485 into transmit mode and did my Serial.print(). I then called Serial.flush() and as soon as that returned, I set the control pin low again.

Serial.flush() returns once the last character/byte has been transmitted so you don't need to put a delay in the code that you need to keep tweaking each time you change the baud rate.

I found an rs485 library, and they do the exact same thing.

I also learned that the direction line must be high if you want to use the USB cable.

I altered my round robin task:

void flushSerialBus() {
    if( debugMode == 0 ) {                     // when debugmode is active, the line must remain high.
        Serial.flush();
        digitalWrite(transmissionDir, LOW); // go to receiving mode 
        digitalWrite(ledPin, LOW ); 
    }
}

This function is called continuously and should do the trick.