4.2 1 1 1 1 1 1 1 1 1 1 Rating 4.20 (5 Votes)

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

As I think a lot of people involved into the automation world  know , Siemens always shown a kind of "adversity" to equip its products with not-native Siemens protocols. One of these, born on far 1979 but still common, is the Modbus protocol for networks TCP/IP. Anyway, available from few years, Siemens release its on licensed-library in order to use Modbus TCP communication into the PN type CPU (300, 400, 400H), into the CP (343, 443) or into the CPU-PN equipped ET200S. Recently I had the possibility to test this library developing an application (S7-317 2PN/DP)  who needs to run as Modbus Master (Client) reading / writings data to some relay controller (Areva) in order to control an hydroelectric power plant. Then I though to wrote this tutorial as a memo for some future similar jobs. Wish it'll be useful you too.

Into this article I'll explain hot to implement to modbus TCP Client (Master) functionality into a CPU S7-315 2PN/DP firmware 3.2. Here follow hardware and software needed coponents:

  • Software S7 Open Modbus TCP CPU PN Single License cod.2XV9450-1MB02 v.1.3 (for details take a look at)
  • Software Simatic Step 7 v.5.5 service pack 3
  • CPU S7 315 2PN/DP cod 6ES7315-2EH14-0AB0 + MMC (512kb is enough)

 

  • Install the Open Modbus TCP CPU PN Single License package. A new Windows menu item will be added into Simatic menu: SIMATIC MODBUS TCP WIZARD. More over the MODBUS_PN_CPU library library will get into your Simatic Step7 resources.
  • Create then a new Simatic project (in this article I call it "MbTCP"), then add into hardware configuration the CPU and then you can add following FB and the DB (besides the normal needed OB, void unattended CPU stop such as OB80, OB82, OB86, OB121, OB122,..)
    • FB102 (MODBUSPN) (v.3.4) : this is the heart of Simatic Modbus library . Copy it from the MODBUS_PN_CPU library included in Simatic
    • FB63 (TSEND): copy it from the Standard Library, Communication Blocks included in Simatic
    • FB64 (TRCV):copy it from the Standard Library, Communication Blocks included in Simatic
    • FB65 (TCON): ccopy it from the Standard Library, Communication Blocks included in Simatic
    • FB103 (TCP_COMM): Copy it from the MODBUS_PN_CPU library included in Simatic
    • FB104 (MOD_CLI): Copy it from the MODBUS_PN_CPU library included in Simatic
    • FB105 (MOD_SERV) : Copy it from the MODBUS_PN_CPU library included in Simatic
    • FC10 (EQ_STRING):Copy it from the Standard Library, IEC Function Blocks included in Simatic
    • DB2 (MODBUS_PARAM) : Copy it from the MODBUS_PN_CPU library included in Simatic
    • DB3 (License DB) : Copy it from the MODBUS_PN_CPU library included in Simatic

  

  • After insert the bottom listed FB and DB, it's necessary to specify the Modbus communication parameters such as IP address, TCP port, target registers for R/W,.. You can choose 2 way to do it:
    • Manually: editing the DB2 (MODBUS_PARAM) contained value
    • By the Simatic Wizard (I suggest this..easier)
  • Start the Wizard added during the installation of S7 Open Modbus TCP CPU PN

  

  • Then press "Next"
  • In the pop-up, set the project you want edit ("MbTCP")  and the "Block" directory path (as I show in following picture). Then press "Next"

  • Into the open pop-up the Wizard requests to locate the DB2 (MODBUS_PARAM), then click the "change" option.  Then press "Next"

  • It's time now to define the working mode fo the CPU: Client=Master or Server=Salve. In my sample I set it as Client by click the "single write (function code 05/06)" option. Then press "Next"

  • NOTE: Into the pop up "Modbus General Parameters, the 3 options means:
    • 1 – Click it if need PLC run as server (Slave), Leave it blank if need to run as Client (Master)
    • 2 – Click it to enable the Modbus writing functionality (reading functionality is always active in Client Mode)
    • 3 – Click it to enable the CPU (in Server mode) auto connect to the Client (do not use it..the FB102 already provides this functionality by its parameters)
  • Into the new open pop-up fill the underlined fields as shown. Then press "Next"

  • Communication Partners pop up description:
    • 1 – Connection ID (it represent the Simatic internal reference ID). This ID must be provide as Input parameters into the FB102 at the "ID"
    • 2 – Connection Name (freely editable)
    • 3 – Connection Type (Active=Client=Master, Passive=Server=Slave)
    • 4 – IP Address of communication partner (or TCP/RTU converter if present). It has to be into the same network
    • 5- Siemens hardware type  (CPU, ET200,…)
  • Set the communication TCP port (usually the 502). Then press "Next"

  • Set the read and write targets as show below:

  • NOTE:
    • 1 - Modbus data type: possible selection between: Coils, Input, Holding Register, Input Register
    • 2 - first resister to read /write. In case of value >0, it represent the offset. (in my sample I start from 17)
    • 3 - last register to read /write
    • 4 - Target data DB. Into it, the first record correspond to the "start Address", 17 in my sample
  • Then press "Next"

  

  • If another CPU/project need to be edit then click  "Configure another connection" otherwise click "Finish"

  •  Insert into the S7 project the FB102 instance (onto OB1 or a OB1' called FC/FB) as following shown:
  •  The instance DB  (named ModbusPN_DI) will create during the "call" instruction of FB102. It'll contain FB102's requested parameters as:
    • ID: [word]=connection ID . It has to be the value specified during wizard (pop up Communication Partners)
    • DB_PARAM:[BLOCK_DB]  parameters DB (DB2 according to wizard)
    • RECV_TIME: [TIME]=max receiving time (time-out)
    • CONN_TIME: [TIME]=max establish connection time (time-out)
    • KEEP_ALIVE:[TIME]=not used
    • ENQ_REQ: [BOOL]= read/write request (rising edge). Good habit is to manage this bit according to communication status (STATUS_MODBUS) and the DONE_NDR  bit (prevoius job done)

 

Parameter    In/Out     Type      Description    Value range 
ID IN WORD    Connection-ID, must be identical to the associated parameter in the local connection description    1 to 4095    yes  
DB_PARAM IN BLOCK_DB    Number of the parameter DB     
RECV_TIME IN TIME    Monitoring Time: Wait for data from communication partner
Shortest adjustable time is 20 ms
 
CONN_TIME IN TIME    Monitoring Time: Wait for establishing or termination of the connection
Shortest adjustable time is 100 ms
 
KEEP_ALIVE IN TIME     Not used  
ENQ_REQ IN BOOL CP is Client: Initiate request at positive edge
CP is Server: Ready to receive at TRUE signal
 
 DISCONNECT IN  BOOL S7 is Client: TRUE: the connection will be terminated after reception of the response
S7 is Server: TRUE: the connection will be terminated when ENQ_ENR = FALSE
 
 REG_KEY  IN  STRING[17]    Registration key  to activate the license     
 LICENSED  IN  BOOL License state of the function block
Block is licensed
Block is not licensed

 TRUE

FALSE

 BUSY     OUT BOOL  Operating state of the T-functions (TCON, TDISCON, TSEND or TRCV)
Job processing
No job processing active

 TRUE

FALSE

 CONN_ESTABILISHED  OUT  BOOL

Connection established

Connection end

TRUE

FALSE
 DONE_NDR      OUT  BOOL CP is Client: TRUE: Active request finished without errors
CP is Server: TRUE: Request from the client was executed and answered
 
 ERROR      OUT  BOOL

An error has occurred (TRUE)
No error has occurred (FALSE)

TRUE

FALSE

 STATUS_MODBUS   OUT  WORD  Error number for protocol errors when evaluating a Modbus telegram (if >0)  
 STATUS_CONN  OUT  STRING[8] Error number for connection errors during execution of the T-blocks (TCON, TSEND,TRCV, DISON),.. (if >0)  
UNIT    IN/OUT BYTE Unit identification (INPUT if in CLIENT mode, OUTPUT if in SERVER mode)  0 to 255
DATA_TYPE    IN/OUT BYTE Data type to be accessed (INPUT if inCLIENT mode, OUTPUT if in SERVER mode)

1=Coil

2=Input Status

3=Holding register

4=Input register

 START_ ADDRESs  IN/OUT  WORD MODBUS start address (INPUT if in CLIENT mode, OUTPUT if in SERVER mode)  0 to 65535
 LENGTH  IN/OUT  WORD Number of registers to be processed (INPUT if in CLIENT mode, OUTPUT if in SERVER mode)

1..2000 Coil Read

1..1968 Write Coil

1..2000 Read Input Status

1..125 Read Holding Register

1..123 Write Holding Register

1..125 Read Input Register

  TI  IN/OUT  WORD  Transaction Identifier (INPUT if in CLIENT mode, OUTPUT if in SERVER mode) . This value MUST be increase for each new ENS_REQ  
WRITE_READ  IN/OUT BOOL INPUT if in CLIENT mode, OUTPUT if in SERVER mode

 TRUE= write

FALSE= only read

         
         

 

Feel free to contact me if you have any questions or comments!