Problem with RS485 direction control during multitasking

I’m running a BL4S200 which runs an HTTP server and also talks over a 485 bus to a bunch of smart devices. I originally structured my mid-level driver to transmit a small block of data and then do a waitfordone looking for the block terminator character. As soon as I see the terminator, I switch the transmitter off so the addressed device can respond. The remote devices begin returning their response about 1.5 mSec after they receive the command terminator.

In a costate, I am running an HTTP server and if it happens to get a request that delays the return through the above mentioned waitfordone, the remote device will begin sending its response before I get the transmitter disabled. Of course, the data is corrupted due to contention and sometimes none of it gets through.

I see a possible solution in sending my block of data except for the terminator, count that many characters reflected back to the receiver, then send the terminator character and do a blocking call to serCgetc and disable the transmitter as soon as I see the echo. Since I’m running at 57600 BAUD, the hang time should be less than 20 uSec.

Does anyone have a better approach?