Switch "On"/"Off" RS485 data lines
You will need a switch of some sort. Maybe a relay, but an analog mux is probably better if you can find a suitable one (low enough resistance and rugged enough for RS-485 (aka high working voltage and ESD protected).
If you can't find higher voltage switches with low enough resistance, you may need to add a repeater in each slave so that the series resistance of the switches doesn't accumulate.
The sequence (which I concocted just now) that happens is this:

All slaves boot up with the next slave connected to the chain.
Master sends a &quot;count-in start&quot; command that is received by all slaves.
In response to the &quot;count-in start&quot; command, all slaves go into a mode expecting count-in communications from the master and disconnect the next slave from the chain leaving only the first slave connected to the master.
Master communicates with the first (and only connected) slave and counts it in.
Upon completion of count-in, the first slave connects the next slave to the chain and waits for a &quot;count-in finished&quot; command. Otherwise it ignores all messages.
The master starts sending count-in messages which are only responded to by the most recently connected slave (since all the others are either disconnected or ignoring everything except for the &quot;count-in finished&quot; command) and counts itself in.
Upon completion of count-in, the slave connects the next slave up and waits for a &quot;count-in finished&quot; command. Otherwise it ignores all messages.
Repeat steps 6 through 7 until all slaves are counted in. You can detect when there are no slaves left with a time-out.
Master sends &quot;count-in finished&quot; command which causes all slaves.




simulate this circuit &ndash; Schematic created using CircuitLab
The way the temrination resistors are portrayed here isn't quite what it needs to be for RS-485. But hopefully it gives you an idea of how to use the DPDT switch to disconnect the next slave while swapping it in for a termination.
I do not recommend using the RS-422 method (mentioned by others) where there is only a dedicated bus running between two adjacent slaves and there is no global bus running to all slaves. I did this on an old project of mine so that the slaves could count themselves in and though it did work, there were problems.
First, it was very slow to propagate messages down the chain in a game of telephone.
Second, sometimes hiccups would happen and messages would stop propagating down the chain mid-way, especially when the system was busier doing other things). It was a DMA, interrupt driven system and I was never able to pinpoint what exactly caused this to be able to fix it. I ended up adding message verification and sending the command in multiple times but never did find the root cause.
Third, it could not repeat long messages down the chain (i.e. data logs) because they the message would get interrupted. I don't know why this happened to this day since the DMA buffer should have been able to handle it. Then again, I can't remember if it sent complete data logs for the slave nearest to the master. It might not have been a fault of the forwarding down the chain.