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
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'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 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
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?
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)))
??!!