USART Multiprocessor communication.

Post here first, or if you can't find a relevant section!
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

USART Multiprocessor communication.

Post by mustaq_ahm »

Hello All,
Good Day!

I was looking arround multiprocessor communication in STM32F103C8T6 which is running in official STM32 core. I would like to test the USART Multiprocessor communication between more than three PCB's. Same as USART asynchronous mode, I could program the microcontrollers to communicate between them.
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.
My question is, if I want to do multiprocessor communication with single master and multiple slave by assigning different adderesses to each slave and calling the address of the required slave in the master should respond to the master. How can I achive this?
Any clues or tutorials or examples are welcome.

A detailed clarification would really help me.

-> I was informed about rotory or dip switch to change the address of the device, if so how can I do that?
-> Also is it possible to change address without the use of rotory or dip switch?
-> How can I proceed further.

Greetings,
Mustaq
by ag123 » Tue Sep 28, 2021 11:37 am
The address is defined in your firmware, the protocol, not in the uart. it is not a hardware concept.

'modbus' can be extremely easy

Code: Select all

void setup() {
	Serial1.begin(115200);
}

#define ADDRESS 0x01;

void loop() {
	if (Serial1.available()){
		uint8_t c = Serial1.read();
		if(c == ADDRESS) {
			// do something
		}	
	}	
}
of course this won't be a functional 'modbus', but it isn't too far from this.

oh i read a little about RS485
https://en.wikipedia.org/wiki/RS-485
that 'enable' pins apparently is about enabling the receiver output or driver output or making them hi-z. I'd guess that would matter more when the slaves need to transmit. As if other slaves are not in hi-z mode, they may instead sink/short the signal to ground. This could also affect the master as rs485 has a different topology.

I'd guess it'd take some effort to figure it out. For purely stm32, you could make that pin hi-z by simply putting it in INPUT mode. But that interfacing external hardware e.g. MAX232 or MAX485 drivers could mean that more signal lines is needed.
Go to full post
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: USART Multiprocessor communication.

Post by mrburnette »

https://forum.arduino.cc/t/multiplexer- ... /543634/20

If your needs are few, softwareSerial may satisfy and you can make an all-s/w implementation.

Personally, as a hardware engineer, I would throw h/w at the problem:

https://www.maximintegrated.com/en/desi ... 5/588.html

Your use case is rs232 many-to-one. This kind of stuff was done many years ago in IT, in-house designs sometimes, but usually with commercial equipment.
https://www.moxa.com/en/products/indust ... ce-servers

Required reading: https://www.maximintegrated.com/en/desi ... 7/723.html
My question is, if I want to do multiprocessor communication with single master and multiple slave by assigning different adderesses to each slave and calling the address of the required slave in the master should respond to the master. How can I achive this?
You may want to consider WiFi where the technology has really blossomed since the Zigbee days:
https://www.google.com/search?q=wifi+ma ... +mesh+data

Then there is the UDP broadcast scenario!
https://www.google.com/search?q=wifi+ud ... o-one+mesh
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USART Multiprocessor communication.

Post by ag123 »

it is called MBUS, and it uses only uart
https://m-bus.com/documentation-wired/04-physical-layer
https://m-bus.com/
it is also called modbus
https://www.controlglobal.com/articles/ ... to-modbus/
https://modbus.org/
https://en.wikipedia.org/wiki/Modbus
and it is mandatory master - slave, master initiates and conclude all comms
slaves only respond

These days, the much faster standard is simply called USB.
The other is called SPI.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

MODBUS protcol addressing.

Post by mustaq_ahm »

If your needs are few, softwareSerial may satisfy and you can make an all-s/w implementation.
Nope, almost every pin has some functions to do in microcontroller. So, I dont think software serial would be efficient solution. Yes, as mentioned by @ag123 , I want to implement MODBUS protcol as I cant change the hardware design right now.
You may want to consider WiFi where the technology has really blossomed since the Zigbee days:
https://www.google.com/search?q=wifi+ma ... +mesh+data

Then there is the UDP broadcast scenario!
https://www.google.com/search?q=wifi+ud ... o-one+mesh
In my project, I dont want to implement any virtual network like Wifi, or, Zigbee, or UDP broadcasts. Right now my requiremnets is with wired communication. 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.

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?

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.

I know, I am little bit devited from the original post, but not completely. The subject of the post says it is about USART Multiprocessor communication but now this has changed to MODBUS protcol addressing.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USART Multiprocessor communication.

Post by ag123 »

The slaves need to know their own addresses, for uart, those control selection lines are not needed, you can keep them enabled throughout.
It is a 'simplex' protocol where it is pretty much request / response and the request is only from the master.
I think modbus can be done on tcp, ethernet etc. it is pretty much not tied to the underlying hardware transport.

I think 'simplex' request (master) / response (slaves) is pretty much necessary, as otherwise u'd need to deal with collision detection if multiple slaves transmit at the same time.

Modbus isn't the only one implementing such protocols, USB has been doing this all along, and it is done for all the millions (billions?) of desktop / laptop and single board computers.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: MODBUS protcol addressing.

Post by mustaq_ahm »

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.
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.
The slaves need to know their own addresses, for uart, those control selection lines are not needed, you can keep them enabled throughout.
I do not understand clearly, I do not get the point why you said this.
It is a 'simplex' protocol where it is pretty much request / response and the request is only from the master.
Lets say I have four microntrollers. Each has several sensors with I2C communication having their own addressess. And one of the microntroller should act as Master and get all the datas from the other microntrollers (slave microcontrollers). And this Master microcontroller will process with all the datas it got from the sensors which it has and also from sensor data values which it got from other microcontrollers (slave microcontrollers). So, I dont think it must be simplex protocol. As you said, yes it is pretty much request and response and the request is from Master and the response is from Slave. But the Slave micontroller needs to give back the required message to the master microcontroller.
I think modbus can be done on tcp, ethernet etc. it is pretty much not tied to the underlying hardware transport.
Yep, just in recent days, MODBUS is done on TCP or Ethernet or whatever availble. MODBUS is also capable of hardware transport with the physical hardware RS485.
I think 'simplex' request (master) / response (slaves) is pretty much necessary, as otherwise u'd need to deal with collision detection if multiple slaves transmit at the same time.
I think for collision, I can send request from Master to all the other slave microcontrollers to send the required data one by one.
Modbus isn't the only one implementing such protocols, USB has been doing this all along, and it is done for all the millions (billions?) of desktop / laptop and single board computers.
Yes, your right, but now availble hardware configuration is suitable only for MODBUS protocol I think.

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.
Image
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USART Multiprocessor communication.

Post by ag123 »

'enable' pins are not needed if the master is the only one initiating all the requests, i.e. the master sends a request to a specific address, then only the slave on that address respond on the line. that is about the only way you can tie all the TX lines of all the slaves together and connect all that TX to a single RX on master. Then the TX on master goes to the RX of all the slaves.

The slaves need to respond only to packets that they are specifically addressed to. Hence, the slaves need to know their own address, hardcoded etc.

There is a way i can *invent* a 'dynamic' address scheme though, you can define a 'broadcast' address, such that after you send that broadcast packet, all the slaves initializes a random delay, select a *random address* and reply using that random address. This would only work if you can tell between the devices. You would also need to *invent* address collision protocols etc. Hence, an easier way is to simply hardcode the address for every slave.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: MODBUS protcol addressing.

Post by mustaq_ahm »

Okay, so you say that wiring and connection are correct and the communication can happen even if I leave all the enable pins in HIGH. If so how can I hardcode the address of the slaves so, that master can identify the each slave with respective? Also, you are saying that there is no need of otary switch as well?

Could you please explain me more about this? Any example code snippets to achive my goal? Any references or sources where I can get my answer? How to achieve my goal?
There is a way i can *invent* a 'dynamic' address scheme though, you can define a 'broadcast' address, such that after you send that broadcast packet, all the slaves initializes a random delay, select a *random address* and reply using that random address. This would only work if you can tell between the devices. You would also need to *invent* address collision protocols etc. Hence, an easier way is to simply hardcode the address for every slave.
I do not understand this clearly.
Last edited by mustaq_ahm on Tue Sep 28, 2021 11:40 am, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: USART Multiprocessor communication.

Post by ag123 »

The firmware in every slave is different, it needs to hardcode that particular address for the slave. As you tie your TX on the master, to RX of all the slaves. So when your master sends the packet, all the slaves receive the same packet. But that you need to program it so that only the particular addressed (hardcoded in firmware or otherwise) slave respond. There are lots of programming to be done in such protocols. But perhaps there are already implementations out there.

I think many SCADA systems probably runs on something like this, possibly running entire plants or production lines.

For addressing, the best examples are to learn from i2c or Ethernet, i2c addresses or Ethernet mac addresses are pretty much hardcoded and saved in firmware. For some i2c modules there is no way to change that address, this limits it to a single device on the i2c bus.\
This is the same with modbus.
Last edited by ag123 on Tue Sep 28, 2021 11:36 am, edited 1 time in total.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: MODBUS protcol addressing.

Post by mustaq_ahm »

Are you sure the each of my microcontroller has their uniqe address.

From your saying: If I use the one of my microntroller as master then I do not need to worry about the address of my master. But I should already know my slaves address so that master can send only to the respective slaves. Right? (I am aware of all the I2C addressess which is connected to several sensors for communication in all of my microntrollers and I know, I can not change the address of my I2C communication)

But, I dont think my custom made PCB's with STM32F103C8T6 has their own and unique address. If they have their own and unique address, where can I find the address of my slave microntrollers. I have alredy read the almost everything from datasheet of STM32F103C8T6, I found nothing. If I am lacking in some knowledge, please improvise me. Guide me to the correct path.

But perhaps there are already implementations out there.
I could able see only for single master and single slave. Not for single master and multiple slave.


Because when I was seraching about UART communication. Somewhere in internet, I read that it is necessary to change the address of the slave device using dip switch, if we use single master and multiple slave. (Just sharing information what I read.)
Last edited by mustaq_ahm on Tue Sep 28, 2021 12:23 pm, edited 4 times in total.
Post Reply

Return to “General discussion”