How can I address the slaves for my master device?

I have not started with my core coding. Actually I need implement a software programming for existing hardware in my Master Thesis. I was looking for small examples on all the topics where I am going to work, so that I can start my programming in full fledge. Right now I am just looking for similar example(single master multiple slave communication with respect to addresing the devices and calling them and processing them.) So that, I can implement in my Master-thesis. I am sharing you some links where I discussed about my project. You could look into it. This link leads to the STM32duino forum, where I discussed something about my master project. If you read all that you will find what I am trying to do.

This is the most active thead in that forum in my profile which is similar to this thread.

I have configured my Hardware Serial USART_1 in PA9 as Tx and PA10 as Rx and PC14 as USART_ENABLE to the maxRS485. Whenever I need to send data from microcontroller_1 to microcontroller_2, then I have to make my eable pin HIGH for microcontroller_1 and LOW for microcontroller_2. Then when I need to send data from microcontroller_2 to microcontroller_1 then I have to change the enable pin as HIGH for microcontroller_2 and LOW for microcontroller_1.

In my project with the existing hardware and available pins for the communication between microcontrollers can be achived only through MODBUS protocol. Since the hardware is already built before my arrival to this project, I can not change any hardware and I should stick to the hardware design and make suitable software programming that communicates between microcontrollers efficiently and faster.

As of now, I am aware of MODBUS protocol. I know that it can have one master and multiple slaves upto 247.

For example, think I am working on four STM32F103C8T6 with STM32 official core. Since all the 4 microcontrollers are identical, I would like assign one microcontroller as master and all others as slave . To do this in existing hardware in MODBUS protocol was quite easy and straight forward with RS485 using same serial pins in all microcontroller with one enable pin.

Since all the microcontrollers are same, I belive they all must have same address (correct me if I am wrong) . Also, to mention here all my microcontrollers has dip switch in it. I am using 94HAB08WT (Coded Rotary Switch with Octal 8 position from 0 to 7) rotary switch. So, I belive with this I could have upto seven slaves and one master in my network.

I think the above configuration works in the principal of MODBUS protocol. For say, let us make microcontroller_1 as Master and microcontroller_2 as Slave . If Master wants request any data from Slave , then the enable_pin of Master must be HIGH for requesting the slave to send data of the I2C communication (lets say temperature sensor) which is already implemented in Slave device and the the enable_pin of Slave must be LOW in order to recive the message from Master. Now slave process the requested message which was sent by Master and send back the required data to the Master . To send back the back the data from Slave to Master , the the enable_pin of Slave must be HIGH and the the enable_pin of Master must be LOW . In order to recive the required data from the Slave to the Master.

So, when I have several slaves, then I should be able to give each slave with unique address with which master can communicate to the respective slave device.

Somewhere in internet, I saw that it is possible to assign master and slave address with dip switches (or rotary switches) for microcontrollers using MODBUS protocol with RS485. If so how?

My question was how can I assign addressess for each device with dip switch (or rotary switch)? For example, if the rotary switch position is in 0 then the corresponding board must act as master and if the rotary switch position is in 1 then the corresponding board must act as slave_1 and positio_2 must be for slave_2 and position_3 must be for slave_3 and so on. How to implement this in programming?

Also, I am attaching the similar image of my MODBUS communication with RS485 between my microcontrollers (consider those Arduino's as my custom made PCB's with STM32F103C8T6 chip running in STM32 official core). I am adding this just for refference purpose.