Program structure - s7-1200 MODBUS RTU Master with multiple slaves & multiple registe

naturephoenix

Member
Join Date
Jan 2015
Location
Vienn
Posts
181
Program structure - s7-1200 MODBUS RTU Master with multiple slaves & multiple registe

I'm having problem to make a appropriate structure of my program. Trying to make MODBUS RTU communication between my master s7-1200 and 4 slaves.

From each slave I'm reading same registers.

I have divide registers into 2 groups, for an example. (There are more groups but for this example two are enough)
Group 1-> 4001, 4003, 4005
Group 2-> 4221,4223,4225

Let's look for slave No. 1

Data from all groups I have placed/stored into same DB, just with different adress.

Into first network I called "MB_COMM_LOAD_DB",
1. NW1 -> FB "MB_COMM_LOAD_DB"
Into second network I called ""MB_MASTER_DB"" & I'm reading Group 1
2. NW2 -> FB MB_MASTER_DB
Into third network I called ""MB_MASTER_DB"" & I'm reading Group 2
3. NW2 -> FB MB_MASTER_DB

Difference between NW2 and NW3 is "DATA_ADDR" and "DATA_PTR"
In both cases FB "MB_MASTER_DB" is using same instance data block.

Question 1: Is this right way to do?

---
Now lets expand this on second slave(slave number 2), which has same groups as slave number 1
Question 2: Should I use new instance data block for FB_MASTER_DB for needs of slave 2, if I do then I should change input parameter "MB_DB" of FB "MB_COMM_LOAD_DB". or I just need to use new FB "MB_MASTER_DB" just like it was case of new group of registers into same slave.
Ok I know I need to change MB_ADDR, but is that just all?

Question 3: Should I make sure to not call FB "MB_MASTER_DB" for group 2 of slave No.1 until previous FB "MB_MASTER_DB" for group 1 of slave No.1 return done and no error.

Was planning to store data from second slave into different DB than slave 1. DBs of slave 1 and slave 2 have same structure. Group 1 and Group 2 are same into both slaves.
 
Last edited:
Difference between NW2 and NW3 is "DATA_ADDR" and "DATA_PTR"
In both cases FB "MB_MASTER_DB" is using same instance data block.

Question 1: Is this right way to do?

---
Now lets expand this on second slave(slave number 2), which has same groups as slave number 1
Question 2: Should I use new instance data block for FB_MASTER_DB for needs of slave 2, if I do then I should change input parameter "MB_DB" of FB "MB_COMM_LOAD_DB". or I just need to use new FB "MB_MASTER_DB" just like it was case of new group of registers into same slave.
Ok I know I need to change MB_ADDR, but is that just all?



Use several MB master blocks (FB), but enable only one by time depending of sequencer
(Rise correct MB_master block EN-pin high depending of sequence. Only mode, data_addr, data_len and data_point pins have different values depending of network. Outputs pins you can let unconnected and look output data from instance data block instead (DB641))

For all MB master blocks use only one Instance data block (DB641? on 1200CPU)
That way you need only do port initialization only one time when PLC starts. (MB_master (FB) and (DB) are linked to MB_COMM_load block. If you use same DB641 for all comms, there is only need to call MB_COMM load one time)

INC sequencer number only after little time (100ms-1s) if MB_MASTER_BLOCKs "busy" signal is "0"

Because FB_block is update by sequencer, be careful if you copy MB_master_blocks output signal to several different variables on program side. They are only updated by sequencer also.
-> If PLC restart, clear all busy variables on OB100 block, otherwise you can have some "busy" variables stucked to "1" state and comms are stucked if your sequencer looks them.
Done and error are only at "1" one PLC scan, so resereting of them is no needed necessarily.

Program is little bit less messy, if you only look busy signal directly of DB641.DBX12.1 (FBs own DB-block).
On PLC restart is good practise also to reset MB_MASTER_BLOCK (put FB_MASTER block to OB100 and give reset signal to block)

So on PLC startup
1. reset MB_master_FB
2. delay
3. init_port with MB_COMM_load
4. delay
5. MB_COMM_load OK
6. start modbus and sequencer
7. Communications
...

8. PLC restart -> back to 1.

MB_comm_load needs to call also, if you remove card and reinsert, if PLC is powered. Because you use 1200CPU, you don't probably take care of this.

If you want make com ok/error counters, then inc counters, if MB_comm is called to slave (look sequence number) and Done or error signal is "1" same time.

It should also be possible that you indirectly pass different parameters on one MB_master_Comm and call only it after new parameters are transferred to block.
Tryed to make that work before, but eventually coded with long way and using several FB_master-blocks. It is easier to look after several years than indirectly maded program.

http://www.plctalk.com/qanda/showthread.php?p=617261


Question 3: Should I make sure to not call FB "MB_MASTER_DB" for group 2 of slave No.1 until previous FB "MB_MASTER_DB" for group 1 of slave No.1 return done and no error.

Yes, don't call another MB_master_DB before busy signal is "0", (at least on serial data only one modbus query can be on at one time on same port). Otherwise if you change input parameters of MB_master blocks, it is possibly that you get slaves answer to wrong slave. (You change paramters between MB_master sending and before slave answers back)


However, haven't tested this with Siemens Modbus TCP, on TCP-side it should be possible to use several querys same time.

p.s Thinked more TCP side. You need to give connection ID parameter to MB_client block. If you use same ID-parameter for blocks you need to make sequence to blocks. If you use different IDs for blocks, you can call several blocks same time for different IP-numbers or for same IP-address. Different IDs open different TCP socket I think.
Hovewer, PLC have limit how many sockets can be open simultaneously at same time to/from PLC.
 
Last edited:
use several mb master blocks (fb), but enable only one by time depending of sequencer
(rise correct mb_master block en-pin high depending of sequence. Only mode, data_addr, data_len and data_point pins have different values depending of network. Outputs pins you can let unconnected and look output data from instance data block instead (db641))

for all mb master blocks use only one instance data block (db641? On 1200cpu)
that way you need only do port initialization only one time when plc starts. (mb_master (fb) and (db) are linked to mb_comm_load block. If you use same db641 for all comms, there is only need to call mb_comm load one time)

inc sequencer number only after little time (100ms-1s) if mb_master_blocks "busy" signal is "0"

because fb_block is update by sequencer, be careful if you copy mb_master_blocks output signal to several different variables on program side. They are only updated by sequencer also.
-> if plc restart, clear all busy variables on ob100 block, otherwise you can have some "busy" variables stucked to "1" state and comms are stucked if your sequencer looks them.
Done and error are only at "1" one plc scan, so resereting of them is no needed necessarily.

Program is little bit less messy, if you only look busy signal directly of db641.dbx12.1 (fbs own db-block).
On plc restart is good practise also to reset mb_master_block (put fb_master block to ob100 and give reset signal to block)

so on plc startup
1. Reset mb_master_fb
2. Delay
3. Init_port with mb_comm_load
4. Delay
5. Mb_comm_load ok
6. Start modbus and sequencer
7. Communications
...

8. Plc restart -> back to 1.

Mb_comm_load needs to call also, if you remove card and reinsert, if plc is powered. Because you use 1200cpu, you don't probably take care of this.

If you want make com ok/error counters, then inc counters, if mb_comm is called to slave (look sequence number) and done or error signal is "1" same time.

It should also be possible that you indirectly pass different parameters on one mb_master_comm and call only it after new parameters are transferred to block.
Tryed to make that work before, but eventually coded with long way and using several fb_master-blocks. It is easier to look after several years than indirectly maded program.

http://www.plctalk.com/qanda/showthread.php?p=617261




yes, don't call another mb_master_db before busy signal is "0", (at least on serial data only one modbus query can be on at one time on same port). Otherwise if you change input parameters of mb_master blocks, it is possibly that you get slaves answer to wrong slave. (you change paramters between mb_master sending and before slave answers back)


however, haven't tested this with siemens modbus tcp, on tcp-side it should be possible to use several querys same time.

P.s thinked more tcp side. You need to give connection id parameter to mb_client block. If you use same id-parameter for blocks you need to make sequence to blocks. If you use different ids for blocks, you can call several blocks same time for different ip-numbers or for same ip-address. Different ids open different tcp socket i think.
Hovewer, plc have limit how many sockets can be open simultaneously at same time to/from plc.


thx
 
this is for 1500, but lot is same.

https://www.google.fi/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0ahUKEwiXzJWop7vTAhXCE5oKHS0_ASgQFgg3MAE&url=https%3A%2F%2Fsupport.industry.siemens.com%2Fcs%2Fattachments%2F59057093%2Fs71500_cm_ptp_function_manual_en-US_en-US.pdf%3Fdownload%3Dtrue&usg=AFQjCNHQXJQFRs0oX0uoGfUJZuZtLH9X6g&cad=rja

page 123.


Example
Modbus_Master is called with MODE=0 and DATA_ADDR=10
This job is now active until it is completed with DONE=1 or ERROR=1 or until the time
monitoring configured at the Blocked_Proc_Timeout parameter has expired. If a new
command is started after the watchdog time expires and before the previous command has
been completed, the previous command is aborted without an error message.
If, while this command is running, the instruction is now called a second time with the same
instance data but different MODE and DATA_ADDR parameter settings, this second call is
terminated with ERROR=1 and STATUS=8200.


"Calling the Modbus_Master instruction with different parameter settings

If multiple calls of the Modbus_Master instruction with different settings for MB_ADDR,
MODE, DATA_ADDR or DATA_LEN are placed in your program, you must ensure that only
one of these calls is active at any given time. Otherwise, the error message 16#8200 is
output (interface is busy with an ongoing request).
If a call cannot be processed in full, the watchdog is activated by the Blocked_Proc_Timeout
parameter and terminates the ongoing command parameter settings, this second call is
terminated with ERROR=1 and STATUS=8200."


So writed little bit wrong before, Block itself notices if parameters are changed before query is maded fully and another call goes to error. I think that this wasn't noted on manual before
 
Last edited:
this is for 1500, but lot is same.

https://www.google.fi/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0ahUKEwiXzJWop7vTAhXCE5oKHS0_ASgQFgg3MAE&url=https%3A%2F%2Fsupport.industry.siemens.com%2Fcs%2Fattachments%2F59057093%2Fs71500_cm_ptp_function_manual_en-US_en-US.pdf%3Fdownload%3Dtrue&usg=AFQjCNHQXJQFRs0oX0uoGfUJZuZtLH9X6g&cad=rja

page 123.


Example
Modbus_Master is called with MODE=0 and DATA_ADDR=10
This job is now active until it is completed with DONE=1 or ERROR=1 or until the time
monitoring configured at the Blocked_Proc_Timeout parameter has expired. If a new
command is started after the watchdog time expires and before the previous command has
been completed, the previous command is aborted without an error message.
If, while this command is running, the instruction is now called a second time with the same
instance data but different MODE and DATA_ADDR parameter settings, this second call is
terminated with ERROR=1 and STATUS=8200.


"Calling the Modbus_Master instruction with different parameter settings

If multiple calls of the Modbus_Master instruction with different settings for MB_ADDR,
MODE, DATA_ADDR or DATA_LEN are placed in your program, you must ensure that only
one of these calls is active at any given time. Otherwise, the error message 16#8200 is
output (interface is busy with an ongoing request).
If a call cannot be processed in full, the watchdog is activated by the Blocked_Proc_Timeout
parameter and terminates the ongoing command parameter settings, this second call is
terminated with ERROR=1 and STATUS=8200."


So writed little bit wrong before, Block itself notices if parameters are changed before query is maded fully and another call goes to error. I think that this wasn't noted on manual before


Hey, thank you.

For a two weeks I was out of job due to some personal problems.

Nevermind, I just checked the code you provided the Siemens link for CPU 1500. It's the same for CPU 1200 and it should be working for my needs.

Inside this manual there is description what to do in a case "New slave" or "New communication job".
This example is for 2 slaves. OK I got 4 slaves so I need to change loop and storage DBs for 4 slaves.

Since I have per each slave 3 communication jobs (reading Voltage 40001,3,5 -> reading currents 42001, 42003, 42005, -> reading Alarms staerting from 44001...) I have to extand program so it looks like I have 12 slaves. (4(slaves) * 3(communication jobs)).

So for new communicaion job(same slave but reading data from another group of modbus registers) I act in this program like there is new slave with different DATA ADDRESS and DATA LENGTH and of cource same MODBUS ADDRESS, and for each communication job I need to add another element to the "Master_Comm array in DB Comm_Data".

Of cource this is because I dont want to make some changes inside FOR LOOPS of this program sample.


Only problem is that I'm not sure will internal memory of 50KB be enough, but if it not I can buy just memory card to increase my area.

On the end,
Thx man this is really what helped me.
 
Yeah, Siemens and modbus isn't easy for start.
But at least sample works also with 1200 pretty easily.

For 300/400 series CPU there is no sample programs, so I needed to look S1500 sample and manuals. Long way to 300 couple years ago.
Eventually it is pretty same than on s1500, but without any indirect addressing and loops.
 
Dear Sir,

Hi

Thanks for sharing your project with us it was helpful.

I have a similar project in which i have to connect 4 (up to 6) Load cells to a S7-1200 PLC through a Modbus transducer (made for load cell). In this project i have to read some values from the transducers (like weight...) and write some values (for calibration of the weighing system). It's for a cement batching plant. It has cement weight, water weight, additive weight (maybe 2), and aggregate weight.

I need to weight these and open or close gates or valves or run a conveyor belt. how fast can i weight this 4? can it feel somehow seamless like they are at the same time? I know how Modbus-485 works. let's say it's about 2000kg aggregate, 400kg water, 500kg cement and 50kg additive. and we should do everything in 70 seconds (including discharge of mixer).

how did you solve your problem? can you help me ?

thanks for your help
 

Similar Topics

Hello all, Just to give you an idea of my background, I'm new to the PLC world with a background in IT and currently pursuing my MSEE. I have...
Replies
3
Views
728
OK, (some of) you guys know by now that I'm not a PLC programmer, so I'm not really familiar with how a well-structured PLC program is typically...
Replies
2
Views
1,466
Hello, I am creating a PLC program for a fresh water pump station which will be based on downstream and upstream pressures. There will be several...
Replies
5
Views
2,098
hi guys I have recently completed a basic S7 course and have started creating my own little programs/solutions to small problems using stl. So...
Replies
1
Views
1,219
I am a mechanical engineer with a mechatronics background working closely with a co-worker learning to program Siemens S7-300/400 series PLCs. My...
Replies
3
Views
4,532
Back
Top Bottom