Modbus RTU Master programming using the example of Simatic S7-1200 and Sinamics V20 drives

I have long wanted to talk about the intricacies of programming the exchange using the Modbus RTU protocol in the case when the controller (in our case, S7-1214) is the RTU Master. Recently I was asked to help with the exchange between the PLC and the Sinamics V20 frequency converter, well, why not write a note at the same time, trying to bring the solution of the problem closer to combat conditions.





As a matter of fact, the Germans themselves covered this topic for a long time:





SINAMICS V: Speed ​​Control of a V20 with S7-1200 (TIA Portal) via USS® protocol / MODBUS RTU with HMI





See this example, it is made very sensibly, with visualization,  dialogues and quests  and the ability to expand the application program to poll multiple V20 inverters via several interfaces (S7-1200 allows you to install up to 4 RS-485/422 ports in its basket). The example is done very well and very pedantic. I have already touched on the issues of communications using the Modbus TCP protocol earlier, they are on Habré.





Therefore, I will not explain some of the nuances again, I will just write right away how to do it correctly and conveniently from my point of view, specifically in the case of the V20 survey. The initial setup of the frequency converter is described in the documentation, including the accompanying documentation for the above example. Let's take out only the points that are important for us as introductory ones.





Modbus slave address in the network: 1





Communication parameters: 9600 8-Even-1





Reading slave storage registers:





40110 ZSW "Status word"





40111 HIW "Current speed"





Storage registers for writing:





40100 STW "Control Word"





40101 HSW "Speed ​​reference"





«Telegram off time (ms)» P2014[0] , 2000 (2 ), 130 . Modbus , , 130 , Fault 72.





. . S7-1214 1241 RS-485/422:





Step 7 V15.1 Update 4, CPU — 4.3.





, . Simatic : Modbus_Comm_Load (, ) Modbus_Master ( / /). FB Modbus_Comm_Load , Modbus_Master — , , , , ( ). , ( ) .





, , , SCL. ModbusMasterV20 SCL. « », .. . , Modbus_Master . , , , — «» , .









Init (Bool) — ,





PORT (PORT) —





BAUD (UDINT) —





STOP_BITS (USINT) — «»





PARITY (USINT) — , 0 — , 1 — odd, , 2 — even,





Step UInt, « » « »





Modbus RTU





, .





1.





#instModbusCommLoad.MODE := 4; // RS-485 4!





#instModbusCommLoad.STOPBITS := #STOP_BITS;





Modbus_Comm_Load, «» . , , . , () . ,     , , , . -, :





MODE , . , RS-485 4. 0, .





STOP_BITS — .





Modbus_Comm_Load. PORT ( ) . BAUD PARITY — — «» , . MB_DB . P2P_MB_BASE, Modbus_Master. «» , :





: .





«» CASE, , , .. .





Modbus Master :





#instModbusMaster(REQ := TRUE,
	MB_ADDR := 1,
	MODE := 0,
	DATA_ADDR := 40110,
	DATA_LEN := 2,
	DATA_PTR := #ZSWHIW);
      
      







REQ — . TRUE, , FALSE — . ( Modbus RTU S7-300/S7-400), TRUE





MB_ADDR — Modbus RTU. = 1.





MODE — , 0 — , 1 —





DATA_ADDR — . (  4), 110. Modbus ( RTU, TCP) «» «». . . 2 , 40110. Modbus 3. 3 Modbus. 40110, 109. , . , . , . ( ), , . « 0 », DATA_ADDR 30001. , - , «», . 16 , , 15 . ? 17. .





DATA_LEN — , 2





DATA_PTR — , «» . , , , . — STAT . , , , ( ). , , /.





FB:





,





ZSW — ( )





HIW —





, . , :





- , ( , №1). — - .





, , . , . , , «» , . , « ». . — . , Init . - .





OB1 CPU:





FirstScan «» OB. , CPU.





Port. Step 7, :





, . , , :





- , . ZSW:





Low enabled . , №15, , , 0, 1. watch table , , — , :





, . , , , , (fault, №3).





, WORD , - .





, . , — . , , - . DataV20:





, Inv :





- :





, Real, . , , . , , , .





— . .





:





, , , . , . , V20Data , Real.





№1, №2.





, :





. , , , / CPU.





. , . (MODE), 1, , . , .





, , — . ( ) . . , . , , «» ( ) , Invalid data. , «» , V20 . , , , . , «Control by PLC», . RTFM, !





, ( , ). , 0 16384. . , .





DataV20 «»:





V20 , , , .. 50 . , modbus.





, №1 ( ), №2 , . «» , , .





DataV20 .





25 , Running .





, V20, .





, , . (), (), , .. - , :





, , , , . .





, , , . , , - «» . , , DataV20. :





(mode = 0) 40025. [0..4] of WORD. Real .





, . Modbus_Master DONE Error (, «» , — ).





, Modbus. , 10 , , , «» , 30. , , , , .





We will assume that the frequency converter is stably exchanging information with the PLC when all three requests to it are completed successfully. Therefore, the very last line of our function block will be as follows (we will first add the Connected boolean variable in the DataV20 data block):












All Articles