How to read Data from a Siemens SPS with RevPi7 C

In this tutorial you will learn how to read data from a Siemens PLC. We have also created an example project for this tutorial. You can download the example project for the TIA portal here and join in right away. For this you will need:

-Your RevPi Connect
-A Siemens PLC
-The program QMod Master

 Example

  1. An output (lamp with the address Q0.0) is set for a certain time on the Siemens PLC.
  2. This is read using the “RevPi7 module” and is mapped in the process image.
  3. In conjunction with a “ModbusTCP SLAVE Module” it is possible to read the state of the output using a Modbus TCP Master (QModMaster).

The example project was created with TIA Portal V15 and was used with a SIMATIC S7-1200.

Configure project structure in PiCtory

  • Enter the IP address of your RevPi Core in your browser’s address bar.

The login window opens.

pictory-anmelden
  • Log in with the user name “admin”.
  • Enter your password. You’ll find it on the sticker on the side of your RevPi Core.
  • Click on “Login”.
  • Click on “Start” to open PiCtory.
Apps Reiter Screenshot

 

  • Assemble your structure on the virtual DIN rail.

Tip! If you are working with PiCtory for the first time, there’s a tutorial covering the basics of PiCtory here.

The next step is to configure the RevPi7 module. Remember that the values we set here are suitable for this example. You may need different values for your application.

  • Specify the PLC characteristics:

  • Define the Action ID.

  • Save the configuration. You have two options:
    – File>Save As. If you want to edit the file again you will need to open it.
    – File> Save as Start-Config. The file will be available immediately the next time you start PiCtory.

  • Click on Tools>Reset Driver to restart the driver.

The next step is to create a Python script to copy the data from the RevPi7 module to the Modbus TCP slave.

  • Open the shell and log in there.
  • Create the file Scriptname.py in nano:
sudo nano Scriptname.py
  • Write the following script to the file:
#!/usr/bin/python
import time
import struct

f=open("/dev/piControl0","wb+",0)

while true:
	time.sleep(0.01) #time in seconds
	f.seek(11)  #offset Input_1 RevPi7 Modul
	x = f.read(1)
	s = struct.unpack('B',x)
	i = s[0] 

	f.seek(1157)  #offset Output_1_i03 Modbus TCP Slave Modul
	i=s[0]
	x = struct.pack('B',i) 
	f.write(x)
  • Save the file and close it.
  • Start the script.
 pythnon3 Scriptname.py &
  • Now open the program QMod Master.
    You will now be able to read the data from the PLC here.