How can I address the slaves for my master device?

Hello all,
Good Day!

I have four identical custom made boards. I would like to configure one of them as master device and other three as slave devices. The connection between all the boards are given in Modbus portocol using RS485 in same UART serial pins availble in the board with one enable pin.

In my custom made boards the dip switch is attched. I am aware that, I am able to change the address of my slaves with the help of dip switch, but I dont know how to implement this in programming. In internet, I was looking for similar examples, but I could not see any. So, I thought of asking this question in this forum.

Any help would be really greatful to me to continue with my project. Any tutorials, or examples, or suggestions, or oppinions, or guidence to me are most welcome.

With friendly Greetings,
Mustaq.

Read the dipswitch. If pattern "this", address "that", and so on.

the slave address is hard coded with the traces to the chip.

are you asking to be able to tell a device to be on address X in software one day, then on address Y on another day ?

Nope, I dont know my slave address. I am not talking about the device in which the addresses are hard coded.

I am working on microcontrollers. To be precise, I am working on STM32F103C8T6 with STM32 official core in Arduino IDE.

Since all the 4 microcontrollers are identical, I would like assign one microcontroller as master and all others as slave. To achive this, I read about MODBUS protocol in internet and implemented the MODBUS protocol with RS485 using serial pins in microcontroller. Since all the microcontrollers are same, I belive they all must have same address (correct me if I am wrong). So, I added dip switches to the microcontroller where I could able to change the address of the microcontroller using dip switch.

So, when I have several slaves, then I should 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 slave address with dip switches for microcontrollers.

My question was how can I assign slave address to my slave device with dip switch? and how to implement this in programming?

Yes, I know how this dip switch works. I have read the datasheet. I am using 94HAB08WT (Coded Rotary Switch with Octal 8 position from 0 to 7). So, I belive with this I could have upto seven slaves and one master in my network.

So, my question is,

I would try to read the dipswitch in setup, transform it to an address and then use that address down in the code.

How do you convert the dip switch to address?
How do you assign address for each device(1 master and 3 slave) with dip switch?
How do call a slave device from master with respective address?
How do you code that all?

I did not understand clearly. Could you please explain me in detail. or with Examples, or with code sinppets, or with some references or with any sources?????

If You properly post code, autofomatted in the IDE, and use code tags, </>, when pasting, and post the schematics for the board, maybe I can say something.

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.

No way I will use the time needed to plow through all material You posted here. It's not at all needed to handle the dip switch matter.

My guess is that You're producing papers but lacks simple knowledge like how to read some switch inputs and assign values to a variable.

Sorry but I jump off this train.

Good luck.

No, the slave address is defined in software. If I recall correctly, the address is passed as a parameter in the modbus begin() function. You would need to read your switch settings and combine them together to create an address - do this in setup() before calling the modbus begin() function so you can pass the address.

1 Like

Connect 4 pins from your STM32 to the pins labeled 1,2,4,8 on the rotary DIP switch. For example, using PORTA

PORTA pin 0 connects to pin "1" of DIP
PORTA pin 1 connects to pin "2" of DIP
PORTA pin 2 connects to pin "4" of DIP
PORTA pin 3 connects to pin "8" of DIP

Then at setup, determine the value of the DIP switch

uint8_t DIP_VALUE = 0;

DIP_VALUE = (uint8_t) (PORTA_IN & 0x0F);

If the modbus address is 8 bits, you can then OR DIP_VALUE to a common address, lets say 0xA0

address = 0xA0 | DIP_VALUE;

So depending on what type of DIP switch you have, you can have the following addresses depending on the value of the DIP

0xA0
0xA1
0xA2
0xA3
0xA4
0xA5
0xA6
0xA7
0xA8
0xA9
0xAA
0xAB
0xAC
0xAD
0xAE
0xAF

You were right now, I figured it out. After getting to know how to implement this. I felt vey bad for my self.

@hzrnbgy Thanks your answerfett gedruckter Text, slightly opened my mind and I also stoped thinking out of box and started thinking very simply and this leads to my answer.

Anyways thanks to all whoever spend their time and energy to enlighten me in my project.

No chain is stronger then the weakest link we say here. Get knowing the small links and advance towards the complete chain.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.