S7prosim using python (connect to plc)

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)))

I tried but doesn't working

Error mssg:

File "D:\simooo\plc.py", line 9, in <module>
print("Result of writing MW10 = "+str(prosim.WriteFlagValue(0, 0, win32com.client.constants.S7_Word, 47)))
TypeError: WriteFlagValue() takes at most 4 arguments (5 given)
 
Try removing the constant definition. That will reduce the number of arguments by one. But I am not sure if it will accept the actual value of 47 or if it will require an actual pointer.

print("Result of writing MW10 = "+str(prosim.WriteFlagValue(10, 0, 47)))
 
You can't write a single word, and also not a single byte. This problem with Python, pyWin32 and Prosim I have mentioned in my first post.

You can write variable with:

valueToWrite = 1234
prosim.WriteFlagValue(12, 0, valueToWrite)

The parameters don't contain any (visible) size information. But in detail the 3rd argument is a variant. A variant contains information of the data type of the variable. If the datatype is byte, then the function writes one single byte, if it's a word then the function writes a word and so on.
But every Python variable is detected as 32 bit integer variable, you can't force a datatype to be 8 or 16 bit.

You'll get similar problems when you want to write a float value, but this can be solved with struct.unpack.

The only method to write a byte or a word with Python / pywin to prosim I know, is to set/reset every single bit of the variable. But when the bits are set/reset not between the OB1 cycles, then you may read an uncomplete variable inside the plc program.

I'd use another language like C#, or write a C dll which encapsulates the prosim handling, and call the C functions from Python.
 
You can't write a single word, and also not a single byte. This problem with Python, pyWin32 and Prosim I have mentioned in my first post.

You can write variable with:

valueToWrite = 1234
prosim.WriteFlagValue(12, 0, valueToWrite)

The parameters don't contain any (visible) size information. But in detail the 3rd argument is a variant. A variant contains information of the data type of the variable. If the datatype is byte, then the function writes one single byte, if it's a word then the function writes a word and so on.
But every Python variable is detected as 32 bit integer variable, you can't force a datatype to be 8 or 16 bit.

You'll get similar problems when you want to write a float value, but this can be solved with struct.unpack.

The only method to write a byte or a word with Python / pywin to prosim I know, is to set/reset every single bit of the variable. But when the bits are set/reset not between the OB1 cycles, then you may read an uncomplete variable inside the plc program.

I'd use another language like C#, or write a C dll which encapsulates the prosim handling, and call the C functions from Python.

i used this script to write M1.4
print("Result of writing MB1 = "+str(prosim.WriteFlagValue(1, 4, True)))
It works very well
but if i tryied to write input point E2.1
print("Result of writing EB2 = "+str(prosim.WriteInputPoint(2, 1, True)))
it does not work

and if u can give a exemple for this methode:
"use another language like C#, or write a C dll which encapsulates the prosim handling, and call the C functions from Python"

Regards!
 
See Plcsim manual. It has sample snippets of how to use the S7Prosim interface from C#, C++ and VB6.

When I simulated the program it gives me an error

code python(part of main program):

if okLeft: #random.randrange(2):
resultLeft = "OK"
print("Result of writing MB0="+str(prosim.writeFlagValue(0, 1, True)))

else:
resultLeft = "NOK"
print("Result of writing MB0="+str(prosim.writeFlagValue(0, 1, False)))

if okRight: #random.randrange(2):
resultRight = "OK"
print("Result of writing MB0="+str(prosim.writeFlagValue(0, 2, True)))

else:
resultRight = "NOK"
print("Result of writing MB0="+str(prosim.writeFlagValue(0, 2, False)))

return resultLeft,resultRight, imagenesProcesadas

////

error:

else:
^
IdentificationError: unexpected indent

//

if u can help me to resolve this problem

Regards!!
 
See Plcsim manual. It has sample snippets of how to use the S7Prosim interface from C#, C++ and VB6.

Hey Thomas, thank u to help me to have solution of this problem

code python:

import snap7
from time import sleep
import struct
import snap7.client as c


plc = snap7.client.Client()
plc.connect("192.168.0.1")

plc.writeMem('QX0.0',True) # write Q0.0 to be true, which will only turn on the output if it isn't connected to any rung in your ladder code
print plc.getMem('MX0.1') # read memory bit M0.1
print plc.getMem('IX0.0') # read input bit I0.0
plc.plc.disconnect()

////

error line ==> plc = snap7.client.Client()

I connected with the plc but the problem stay same
if u can help me to resolve this problem
 

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