MODBUS implementation in STM32F103C8T6 with STM32 official core in Arduino IDE

Hello all,
Good Day!

I am working on my Master Thesis. I would like to implement Modbus RTU protocol using RS485 UART serial communication between four STM32F103C8T6 with STM32 official core in Arduino IDE.

I have some questions!

1. Is there a full-fleged libary for Modbus RTU protocol using RS485 UART serial communication as mentioned in Modicon Modbus Protocol Reference Guide official document? Literally the whole implementation of Modbus RTU protocol using RS485 UART serial communication for both Master and Slave (Start->Address->Function Code->Data->CRC->Stop) .

*. From my understanding about MODBUS, it is possible to communicate between one Master microcontroller with several other slave microcontrollers. Also it is possible to Broadcast the message or request from master to to all slave microcontrllers


2. If so how can I broadcast the message from Master microcontroller to all the slave microcontrollers? I want to implement this in STM32f103C8T6 to send message to all my slave STM32F103C8T6 microcontrollers. And I want to get back the response from only one slave with the 10 sensor data values. Is this fesible through MODBUS? If yes, let me know how? If no, let me know why?

3. If it is fesible, will there be any collision? If yes, let me know why? If no, let me know how?


My hardware is fixed I can not change anything from my hardware part. I have started my Thesis in company in the software part in programming the STM32F103C8T6 with stm32 official core in Arduino IDE. So, I have four similar PCB board with STM32F103C8T6 have some temperature sensor's, LED's, motors's, PWM generator's. My goal is to make one microcontroller as Master and other three as Slave. And Master should be having HMI (Human Machine Interface.). All the slave microcontrollers will have the same funtionalities as Master except HMI.

My processes should look similar to this:
  1. i. I should be able to give Inputs or set some limits or something like that from HMI to Master microcontroller.
  2. ii. The limits or inputs or whatever must be set in the Master microcontroller and from Master microntroller all those inputs or limits must be set for Slave microcontrollers as well.
  3. iii. Then the Master microntroller should be able to send the broadcast request or message to all the slave microcontrollers to get all the data's.
  4. iv. Then the Master microcontroller must get all the sensor values or states of the LED's or Motor's or whatever from Master microcontroller itself.
  5. v. Then, all this data's to be stored sepeartely in Master microntroller.
  6. vi. (from iii.) When the Master microntroller has sent the broadcast request or message to all the slave microcontrollers. (same as iv. for slaves) Then all the slave microcontrollers must get all their sensor values or states of the LED's or Motor's or whatever from themself. (same as v. for slaves) And stored sepeartely in them itself.
  7. vii. Then Master microcontroller should send request or message to each slave microcontroller, one after one to get the stored values or states of the LED's or Motor's or whatever from the Slave microcontroller to the Master microcontroller.
  8. viii. Then there must be over all check in the Master microcontroller to check all data is available for HMI.
  9. ix. If something is missing then the Master must get the missing data from itself or from slaves.
  10. x. Then the Master to should send all the data to HMI from Master Microcontroller.

So, basically I will be controlling the whole thing with HMI having my Master microntroller connected to it and all other Slave microcontrollers connected to Master microcontroller.

4. Is it possible to do my whole above process in 500 milliseconds? If yes, let me know how? If no, let me know why?

5. Is there any examples of implementing all these?

6. What are difficulties I might face?

7. For what I must be prepared?


Your answers will be really helpfull for my thesis. Let me know your suggestions, oppinions, and guidence. I would like to here from all of you. Thanks in Advance.

Greeting,
Mustaq.
 
I don't know about libraries, but I implemented modbus/rtu from scratch on one of the stm32 mcus. I didn't find it that hard. Essentially, using interrupt driven serial IO, every time a receive interrupt occurred, I re-started a timer (used one of the general purpose timers). The timeout was the 3.5 character end of frame - set up the timer to be one-shot. When a timer interrupt occurred, I set a flag that a frame had been received. To drive this, I used the simplemodbus python package - I used it under both linux and win 10 and it worked well.
A couple of things to be careful of: make sure the crc is right (I just copied the table from the spec). To make sure of the timing, I started with the timer interrupt setting a gpio and sending a serial burst and looking at the serial waveform and timer interrupt on a logic analyzer. The timeout has to agree with the baud rate - the relationship is in the spec.
One thing I found convenient was to break up the composing and sending of the response - I had a function to start the header, function to add data to a message (could be called repeatedly), another to finalize with the crc and actually do the sending. Also had a table describing addresses that could include pointers to handler functions so data could be dynamic - eg, reading an adc.
One thing I really liked about this implementation is that it doesn't get stuck in any weird states - the timer interrupt gets everything set to receive another frame, which won't happen until the response has been sent and received (or response timeout has occurred).
Good luck!
 
I don't know about libraries, but I implemented modbus/rtu from scratch on one of the stm32 mcus. I didn't find it that hard. Essentially, using interrupt driven serial IO, every time a receive interrupt occurred, I re-started a timer (used one of the general purpose timers). The timeout was the 3.5 character end of frame - set up the timer to be one-shot. When a timer interrupt occurred, I set a flag that a frame had been received. To drive this, I used the simplemodbus python package - I used it under both linux and win 10 and it worked well.
A couple of things to be careful of: make sure the crc is right (I just copied the table from the spec). To make sure of the timing, I started with the timer interrupt setting a gpio and sending a serial burst and looking at the serial waveform and timer interrupt on a logic analyzer. The timeout has to agree with the baud rate - the relationship is in the spec.
One thing I found convenient was to break up the composing and sending of the response - I had a function to start the header, function to add data to a message (could be called repeatedly), another to finalize with the crc and actually do the sending. Also had a table describing addresses that could include pointers to handler functions so data could be dynamic - eg, reading an adc.
One thing I really liked about this implementation is that it doesn't get stuck in any weird states - the timer interrupt gets everything set to receive another frame, which won't happen until the response has been sent and received (or response timeout has occurred).
Good luck!
Thank you so much for your reply. Could you share your code for my reference? For me, I am trying to figure out the way, but this is not working out for long time. It will be helpfull if you show your kind courtesy to me and my thesis. :)
 
Top