S7prosim using python (connect to plc)

babido10

Member
Join Date
Nov 2017
Location
rabat
Posts
28
Hi
I am trying to apply the Siemens S7ProSim Automation object using
Python. If someone here can help me to do this
thank u
 
Short description:

1) Install "Python for Windows extensions"
from: https://sourceforge.net/projects/pywin32/
Install the version which corresponds to the exact version of Python you are using.

2) Register Prosim for use with pywin32
Start "PythonWin", select from menu Tools -> COM makepy utility.
Select "Siemens S7ProSim COM Object" library.

3) You can use the prosim methods from python code like this:

import win32com.client
prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.Connect() # Connect to Plcsim

But there are some limitations / problems due to the variant datatype handling of the Prosim COM object and in pywin32.
You can't write byte or word variables, because the Prosim write-methods detect the size you want to write by the variant data type of the value in the parameter of the method.
And you can't set the exact variant type in python to the one you would need, it's always a 32 bit integer.

Because of this, I have written for a C dll which handles the Prosim object and exports the functions I need in Python.
And from Python I call the functions from my C dll. Another advantage is that this doesn't need pywin32.
 
Short description:

1) Install "Python for Windows extensions"
from: https://sourceforge.net/projects/pywin32/
Install the version which corresponds to the exact version of Python you are using.

2) Register Prosim for use with pywin32
Start "PythonWin", select from menu Tools -> COM makepy utility.
Select "Siemens S7ProSim COM Object" library.

3) You can use the prosim methods from python code like this:

import win32com.client
prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.Connect() # Connect to Plcsim

But there are some limitations / problems due to the variant datatype handling of the Prosim COM object and in pywin32.
You can't write byte or word variables, because the Prosim write-methods detect the size you want to write by the variant data type of the value in the parameter of the method.
And you can't set the exact variant type in python to the one you would need, it's always a 32 bit integer.

Because of this, I have written for a C dll which handles the Prosim object and exports the functions I need in Python.
And from Python I call the functions from my C dll. Another advantage is that this doesn't need pywin32.

Hi Sir,

I did all the steps but when I run the simulation it gives nothing so did not connect to the plcsim!!
I dont know why if u have solution for this , thank u Sir
 
Hi Sir,

I did all the steps but when I run the simulation it gives nothing so did not connect to the plcsim!!
I dont know why if u have solution for this , thank u Sir

Did you try to read or write values?
Only connecting does nothing else than connecting to plcsim (Plcsim needs to be running), after that you can call the other methods like ReadFlagValue.
 
Did you try to read or write values?
Only connecting does nothing else than connecting to plcsim (Plcsim needs to be running), after that you can call the other methods like ReadFlagValue.

i try to run it via Python but not working
this the script python used to do this tell if correct or not

code:
import win32com.client

from Tkinter import *

#Create a windows
root = Tk()

def connect(event):
prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.Connect() # Connect to Plcsim

button_1=Button(root, text="connect",)
button_1.bind("<Button-1>", connect)
button_1.grid(row=0,sticky=E)

def RUN(event):
prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.SetState() # RUN to Plcsim

button_3=Button(root, text="RUN",)
button_3.bind("<Button-3>", RUN)
button_3.grid(row=2,sticky=E)

root.mainloop()

///
 
I'd start with a simple console based tests before going on with Tk.

Simple example to read MW0:

prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.Connect()
print ("PLCSIM status: " + prosim.GetState())
print("MW0 = " + str(prosim.ReadFlagValue(0, 0, win32com.client.constants.S7_Word, None)))
prosim.Disconnect()

I'd pack all the Plcsim handling in a separate python class.
 
I'd start with a simple console based tests before going on with Tk.

Simple example to read MW0:

prosim = win32com.client.Dispatch("S7wspsmx.S7ProSim.1")
prosim.Connect()
print ("PLCSIM status: " + prosim.GetState())
print("MW0 = " + str(prosim.ReadFlagValue(0, 0, win32com.client.constants.S7_Word, None)))
prosim.Disconnect()

I'd pack all the Plcsim handling in a separate python class.

Thank u a lot Sir;

If for exemple I want to write MW12 , what can i do ?

Regards
 
I am not familiar with the S7ProSim but if you look at the documentation you can see that the "ReadFlagValue(0, 0," in the line that reads MW0 are the byte, bit addresses

So if you change:
print("MW0 = " + str(prosim.ReadFlagValue(0, 0, win32com.client.constants.S7_Word, None)))
to
print("MW12 = " + str(prosim.ReadFlagValue(12, 0, win32com.client.constants.S7_Word, None)))

You should get your desired results

Documentation:
https://cache.industry.siemens.com/dl/files/855/1139855/att_29424/v1/S7WSPSCB.pdf
 
I am not familiar with the S7ProSim but if you look at the documentation you can see that the "ReadFlagValue(0, 0," in the line that reads MW0 are the byte, bit addresses

So if you change:
print("MW0 = " + str(prosim.ReadFlagValue(0, 0, win32com.client.constants.S7_Word, None)))
to
print("MW12 = " + str(prosim.ReadFlagValue(12, 0, win32com.client.constants.S7_Word, None)))

You should get your desired results

Documentation:
https://cache.industry.siemens.com/dl/files/855/1139855/att_29424/v1/S7WSPSCB.pdf

No, I asked u about if i want write MW10 , what can i do ?
 
Thank u a lot Sir;

If for exemple I want to write MW12 , what can i do ?

Regards

You asked for MW12 in this reply. Either way, if you look at the documentaiton or what I had to change to get it to work from 0 to 12... it should be pretty straight forward what needs to be changed to get it to read MW10.

EDIT:

I now see that you want to write and not read. I assume there is a 'writeflag' methode that will achive this. Have you looked in the documentation I linked?
 
You asked for MW12 in this reply. Either way, if you look at the documentaiton or what I had to change to get it to work from 0 to 12... it should be pretty straight forward what needs to be changed to get it to read MW10.

EDIT:

I now see that you want to write and not read. I assume there is a 'writeflag' methode that will achive this. Have you looked in the documentation I linked?

Sorry Sir but you did not understand what i want, i asked to write MW10 (the script python)
if u know how can i do it
 
I included in my edit that I noticed you wanted to write, not read.

But either way in the documentation I linked there is a 'WriteFlag' that is very similar to the 'ReadFlag'. Have you read and/or tried that?
 
I included in my edit that I noticed you wanted to write, not read.

But either way in the documentation I linked there is a 'WriteFlag' that is very similar to the 'ReadFlag'. Have you read and/or tried that?

If i want to write MW10, i will use this script :
print("MW0="+str(prosim.WriteFlagValue(0,0,win32com.client.constants.S7_Word, None)))

??!!
 
If i want to write MW10, i will use this script :
print("MW0="+str(prosim.WriteFlagValue(0,0,win32com.client.constants.S7_Word, None)))

??!!

The print("MW0=" was fitting when reading the value of of a memory location. I don't believe it would be necessary anymore. You should probably replace it with something along the lines of 'Result of writing MW10 = '

WriteFlagValue(0,0 shows what byte, bit you want to write.
So it should be updated to WriteFlagValue(10,0

None))) at the is a pointer to the data that is to be written. You can try insterting an integer or something directly and see if it errors. The example below should write 47 to mw10

I would try something along the lines of:

print("Result of writing MW10 = "+str(prosim.WriteFlagValue(10,0,win32com.client.constants.S7_Word, 47)))
 

Similar Topics

Hello I'd like to use S7ProSim COM Objects in MATLAB. But I can not find the object name. I tried to register the DLL file (s7wspsmx.dll) with...
Replies
0
Views
1,615
How to connect to COM object S7ProSim (version 5.4) and receive events OnScanFinished, OnConnectionError using Delphi?
Replies
0
Views
1,741
Hello.. I would like to ask a straightforward question. For S7-PLCSIM, we use S7ProSim to read input/output images, but what about a real PLC...
Replies
0
Views
2,325
Could someone show me simple source code written in C# Visual Studio 2005 for S7ProSim? I just want to have an idea about how the process is...
Replies
0
Views
2,078
When I try to write a PIW to a simulated PLC in PLCSIM, I get a Data Type Error. Code...
Replies
14
Views
9,165
Back
Top Bottom