script python , help!!!

babido10

Member
Join Date
Nov 2017
Location
rabat
Posts
28
Hello

I wanna do script python ( if detect color ==> Set memory bit )


WriteMemory(plc,0,1,S7WLBit,True)
if 'orange': (0, 140, 255)==1


this script is correct ???!!
 
That script is not correct. If you post the full script it would be easier to give feedback. Please use code tags when posting, whitespace is very important in Python and the forum's code tags will preserve it.

This line is nonsensical:
Code:
if 'orange': (0, 140, 255)==1
The if test will always be True because 'orange' is a non-empty string. You need to have a variable to compare against the string. Something like:

Code:
if color == 'orange':
I'm not sure what this is supposed to do, but I am sure it's not what you want:
Code:
(0, 140, 255)==1
Presumably you want to use your PLC library to set a bit here. What you're actually doing is creating a Python tuple and comparing it 1, then discarding the result.
 
That script is not correct. If you post the full script it would be easier to give feedback. Please use code tags when posting, whitespace is very important in Python and the forum's code tags will preserve it.

This line is nonsensical:
Code:
if 'orange': (0, 140, 255)==1
The if test will always be True because 'orange' is a non-empty string. You need to have a variable to compare against the string. Something like:

Code:
if color == 'orange':
I'm not sure what this is supposed to do, but I am sure it's not what you want:
Code:
(0, 140, 255)==1
Presumably you want to use your PLC library to set a bit here. What you're actually doing is creating a Python tuple and comparing it 1, then discarding the result.

i used library snap7

not error in all of my script

this part of script:

# define standard colors for circle around the object
colors = {'blue': (255, 0, 0), 'yellow': (0, 255, 217),
'orange': (0, 140, 255)}


if colors=='orange' :
WriteMemory(plc, 0, 1, S7WLBit, True)

///
I wanna set M0.1 when detected color orange but nothing happened
!!
 
We're missing some information about your script as mts5096 stated. The problem with your example above is you are asking if colors is equal to orange, but colors is a dict with multiple "things" in it.

By your description, you should be asking what your circle is equal to. I don't know anything about your circle but I'll provide an example:

Code:
colors = {'blue': (255, 0, 0),
          'yellow': (0, 255, 217),
          'orange': (0, 140, 255)}

circle1 = (0,140,255)

# find dict key based on color value
circle_color =[key for key, value in colors.iteritems() if value == circle1][0]

if circle_color == 'orange':
    print 'it sure does'
else:
    print 'nope, it does not'

One thing to keep in mind is that if the color value of circle1 is not in the colors dict, it will not be happy. You would probably want to use a try/catch.
 
Or
Code:
colors = {'blue': (255, 0, 0),
          'yellow': (0, 255, 217),
          'orange': (0, 140, 255)}

circle1 = (0,140,255)

if circle1 == colors['orange']:
    print 'it sure does'
else:
    print 'nope, it does not'
 
Or
Code:
colors = {'blue': (255, 0, 0),
          'yellow': (0, 255, 217),
          'orange': (0, 140, 255)}

circle1 = (0,140,255)

if circle1 == colors['orange']:
    print 'it sure does'
else:
    print 'nope, it does not'

Haha, that looks way better!
 

Similar Topics

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")...
Replies
0
Views
1,527
When I simulated the program it gives me an error code python(part of main program): if okLeft: #random.randrange(2): resultLeft = "OK"...
Replies
2
Views
1,736
Hi all ; if someone here can help me to convert this code to python code: public static libnodave.daveOSserialType fds; public static...
Replies
11
Views
3,726
Hello, I am learning to create shapes and VB Scripts in HMIWeb Display Builder. I wanted to change color of my rounded rectangle by script. I...
Replies
0
Views
71
Hello, I have a quick question. I have been using scripts to change the color of buttons. The reason, I am usually using multiple hmiruntime.tags...
Replies
1
Views
82
Back
Top Bottom