You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc.
 
Try our online PLC Simulator- FREE.  Click here now to try it.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old November 23rd, 2017, 04:44 AM   #1
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
S7prosim using python (connect to plc)

Hi
I am trying to apply the Siemens S7ProSim Automation object using
Python. If someone here can help me to do this
thank u
  Reply With Quote
Old November 23rd, 2017, 08:17 AM   #2
gclshortt
Member
Canada

gclshortt is offline
 
gclshortt's Avatar
 
Join Date: Dec 2014
Location: Cobourg
Posts: 2,372
http://www.plctalk.com/qanda/showthread.php?p=417243

The above thread may help you out.
Regards,
__________________
Garry
http://www.accautomation.ca
Connect with us on Facebook: facebook.com/accautomation/
  Reply With Quote
Old November 23rd, 2017, 03:06 PM   #3
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 337
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.
  Reply With Quote
Old November 28th, 2017, 09:12 AM   #4
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by Thomas_v2 View Post
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
  Reply With Quote
Old November 28th, 2017, 12:00 PM   #5
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 337
Quote:
Originally Posted by babido10 View Post
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.
  Reply With Quote
Old November 28th, 2017, 12:06 PM   #6
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by Thomas_v2 View Post
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()

///
  Reply With Quote
Old November 28th, 2017, 12:19 PM   #7
Thomas_v2
Member
Germany

Thomas_v2 is offline
 
Join Date: Apr 2009
Location: Ger
Posts: 337
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.
  Reply With Quote
Old November 29th, 2017, 03:48 AM   #8
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by Thomas_v2 View Post
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
  Reply With Quote
Old November 29th, 2017, 08:56 AM   #9
JOLTRON
Lifetime Supporting Member
United States

JOLTRON is offline
 
JOLTRON's Avatar
 
Join Date: Aug 2006
Location: MI
Posts: 640
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/d...1/S7WSPSCB.pdf
  Reply With Quote
Old November 29th, 2017, 09:01 AM   #10
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by JOLTRON View Post
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/d...1/S7WSPSCB.pdf
No, I asked u about if i want write MW10 , what can i do ?
  Reply With Quote
Old November 29th, 2017, 09:13 AM   #11
JOLTRON
Lifetime Supporting Member
United States

JOLTRON is offline
 
JOLTRON's Avatar
 
Join Date: Aug 2006
Location: MI
Posts: 640
Quote:
Originally Posted by babido10 View Post
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?
  Reply With Quote
Old November 29th, 2017, 09:20 AM   #12
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by JOLTRON View Post
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
  Reply With Quote
Old November 29th, 2017, 09:26 AM   #13
JOLTRON
Lifetime Supporting Member
United States

JOLTRON is offline
 
JOLTRON's Avatar
 
Join Date: Aug 2006
Location: MI
Posts: 640
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?
  Reply With Quote
Old November 29th, 2017, 09:53 AM   #14
babido10
Member
Morocco

babido10 is offline
 
Join Date: Nov 2017
Location: rabat
Posts: 28
Quote:
Originally Posted by JOLTRON View Post
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,win32co m.client.constants.S7_Word, None)))

??!!
  Reply With Quote
Old November 29th, 2017, 10:35 AM   #15
JOLTRON
Lifetime Supporting Member
United States

JOLTRON is offline
 
JOLTRON's Avatar
 
Join Date: Aug 2006
Location: MI
Posts: 640
Quote:
Originally Posted by babido10 View Post
If i want to write MW10, i will use this script :
print("MW0="+str(prosim.WriteFlagValue(0,0,win32co m.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.c onstants.S7_Word, 47)))
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Kivy + Python + PLC + RPi DINT2 LIVE PLC Questions And Answers 7 September 9th, 2016 10:28 AM
PLC Update - Back Issues Collection Now Available! Phil Melore LIVE PLC Questions And Answers 11 October 22nd, 2015 02:17 AM
try to connect PLC via intouch wonderware b_e33 LIVE PLC Questions And Answers 1 April 3rd, 2013 08:06 AM
How to connect AC sensor and motor to PLC Zhen LIVE PLC Questions And Answers 5 April 10th, 2009 06:47 AM
how to connect a PLC to an optocoupler Sean Vyar LIVE PLC Questions And Answers 1 April 5th, 2004 10:52 AM


All times are GMT -5. The time now is 12:58 PM.


.