鍦–ODESYS杞欢涓娇鐢≒ython-CODESYS璧勬簮涓嬭浇鍖-閰峰痉缃 - CoDeSys杞欢鏁欑▼涓嬭浇,CoDeSys宸ユ帶璧勮闂ㄦ埛
HSC6490-cn涓浗鍒堕狅紝涓浗鑺瘇
鏌ョ湅: 2152|鍥炲: 2

鍦–ODESYS杞欢涓娇鐢≒ython

[澶嶅埗閾炬帴]
  • TA鐨勬瘡鏃ュ績鎯
    鎿︽睏
    2019-8-13 14:13
  • 绛惧埌澶╂暟: 32 澶

    [LV.5]甯镐綇灞呮皯I

    鍙戣〃浜 2020-6-17 10:55:40 | 鏄剧ず鍏ㄩ儴妤煎眰 |闃呰妯″紡
    Python鏄竴绉嶅姩鎬佽瑷銆傛偍鍙互浠庣畝鍗曠殑绾挎х紪绋嬫牱寮忥紙鎵瑰鐞嗘枃浠讹級寮濮嬶紝鐒跺悗鍐嶆坊鍔犲繀瑕佸拰鏇村己澶х殑鍔熻兘锛屼緥濡傛潯浠讹紝寰幆锛屽嚱鏁帮紝寮傚父锛岀被鍜屾ā鍧椼侾ython鍦ㄨ繍琛屾椂妯″紡涓嬫洿涓哄吀鍨嬶紝瀹冧娇鐢ㄨ嚜鍔ㄥ瀮鍦炬敹闆嗗櫒鏉ヤ繚鎶ょ▼搴忓憳鍏嶅彈鎰忓鎹熷潖鏁翠釜绯荤粺銆
    IronPython鏄.NET鐨凱ython瀹炵幇锛屽苟鍏佽瀹屽叏璁块棶.NET妗嗘灦鍜岀被銆侷ronPython瑙i噴鍣ㄧ殑瀹炵幇鍩轰簬Python鐗堟湰2.7銆
    鍦–ODESYS杞欢涓娇鐢≒ython鍏ラ棬
    • 浣跨敤鏂囨湰缂栬緫鍣紝鍒涘缓涓涓猦ello.py鏂囦欢锛屽湪鍏朵腑杈撳叆浠ヤ笅鍐呭锛歱rint("Hello, automation!")
    • 鍚姩CODESYS锛岀劧鍚庡崟鍑诲伐鍏封h剼鏈f墽琛岃剼鏈枃浠讹紝鍦ㄦ枃浠剁郴缁熶腑閫夋嫨鏂囦欢hello.py銆
    bVbGDIu.jpg
    • 鍦ㄦ秷鎭鍥句腑锛屼細鏄剧ず濡備笅锛
    1460000022516562.jpg
    Python鐨勫熀鏈娉曪紙甯︽湁绀轰緥锛
    Python涓嶤璇█鐩镐技锛屼絾鏄湁涓浜涙槑鏄剧殑鍖哄埆鍜岀嫭鐗圭殑灞炴с
    Python涓嶤鍜孲T绛夎瑷涔嬮棿鏈鏄庢樉鐨勮娉曞尯鍒槸Python瑙f瀽鍣ㄩ氳繃缂╄繘璇嗗埆鍧楃粨鏋勩傛病鏈塀EGIN/END鎴栧ぇ鎷彿{}鏉ヨ瘑鍒殑鍧桰F/ELSE鐨勬潯浠朵笅锛孎OR鍜學HILE寰幆锛屾垨鍑芥暟銆
    娉ㄩ噴浣跨敤#寮澶村苟寤朵几鍒拌琛岀殑鏈熬銆傚湪婧愪唬鐮佺殑绗竴琛屽拰绗簩琛屼腑锛屾偍鍙互璁剧疆涓涓壒娈婄殑鏍囪鏉ュ0鏄庢枃浠剁殑缂栫爜銆傚鏋滀笉闇瑕丄SCII瀛楃锛屾垜浠缓璁偍浣跨敤UTF-8浣滀负缂栫爜銆
    鍑轰簬璋冭瘯鐩殑锛屾偍鍙互浣跨敤print鏉ョ畝鍖栬緭鍑恒備娇鐢%杩愮畻绗︼紝鎮ㄥ彲浠ュ疄鐜扮被浼间簬C鍑芥暟鐨勫姛鑳絧rintf()銆傝緭鍑烘樉绀哄湪CODESYS鐨勬秷鎭鍥句腑銆
    渚嬬▼锛
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    
    # 瀹氫箟涓涓甫鏈夊弬鏁癷鐨勫嚱鏁
    def do_something(i):
        # if 鍒嗘敮
        if i>0:
            print("The value is: %i" % i)
            sum += i
            print("The new sum is: %i" % sum)
    
        # else if 锛堝彲閫夛紝鍙互娌℃湁锛屼篃鍙互鏄涓猠lif鍒嗘敮锛
        elif i==0:
            print("The sum did not change: %i" % sum)
    
        # 鏈鍚庣殑else鍒嗘敮锛堝彲閫夛級銆
        else:
            handle_error()
    
    # 姝诲惊鐜
    while True:
        print("I got stuck forever!")
    灞炰簬鍚屼竴涓潡鐨勬墍鏈夊唴瀹归兘蹇呴』缂╄繘鐩稿悓鐨勮窛绂汇傛嫭鍙峰拰鑺辨嫭鍙风瓑鍏冪礌鐨勪紭鍏堢骇楂樹簬缂╄繘銆備互涓嬩唬鐮佹鍗充娇鏄互涓嶈壇鐨勭紪绋嬮鏍肩紪鍐欑殑锛屼絾鍗存槸瀹屽叏姝g‘鐨勩
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # 璀﹀憡锛氫笅闈㈢殑鏍峰紡涓嶅ソ銆 瀛╁瓙浠紝涓嶈鍦ㄥ灏濊瘯锛
    foo = 0
    bar = 1
    if foo >= bar:
                   print("foobar")
    else:
        print(
              "barfoo"
    )
    涓洪伩鍏嶆涔夛紝闇瑕侀伩鍏嶅湪鏂囦欢涓贩鐢ㄥ埗琛ㄧ鍜岀┖鏍笺
    Python鍖哄垎澶у皬鍐欙紝涓嶴T鐩镐技涓旂浉鍙嶃傚叧閿瓧锛屼緥濡俤ef锛宨f锛宔lse锛屽拰while锛屽繀椤绘槸灏忓啓锛堢浉杈冧簬ST瑙勫垯锛氬叧閿瓧鏄ぇ鍐欙級銆備袱涓爣璇嗙锛堜緥濡傗 i鈥濆拰鈥 I鈥濓級琛ㄧず涓や釜涓嶅悓鐨勫彉閲忋
    浠ヤ笅鍏抽敭瀛楀湪Python涓槸淇濈暀瀛楋紝涓嶅厑璁哥敤浣滃彉閲忥紝鍑芥暟绛夌殑鏍囪瘑绗︼細
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    and | as | assert | break | class | continue | def | del | elif | else | except | exec | finally | for | from | global | if | import | in | is | lambda | not | or | pass | print | raise | return | try | while | with | yield
    鍙橀噺鍜屾暟鎹被鍨
    Python鏄竴绉嶅姛鑳藉己澶х殑鍔ㄦ佺被鍨嬪寲璇█-鎵鏈夌被鍨嬩俊鎭兘鍦ㄨ繍琛屾椂杩涜璇勪及銆 鍙橀噺鍙繚瀛樺璞$殑寮曠敤锛屼互鍙婂璞$煡閬撳畠鐨勭被鍨嬶紝鑰屼笉鏄彉閲忋 褰撶▼搴忓憳灏濊瘯鎵ц涓嶅彲鑳界殑鎿嶄綔锛堜緥濡傦紝娣诲姞鏁存暟鍜屽瓧绗︿覆锛夋椂锛孭ython鍦ㄨ繍琛屾椂寮曞彂寮傚父銆
    鍥犳锛屾病鏈夊彉閲忓強鍏剁被鍨嬬殑澹版槑銆傚湪Python涓紝浠呭垱寤哄彉閲忎互涓哄叾鍒嗛厤鍊笺傝繖鍦ㄧ被鍨嬪己涓旈潤鎬佺殑C鍜孲T涓畬鍏ㄤ笉鍚屻傛瘡涓彉閲忛兘鐢ㄤ竴绉嶇被鍨嬪0鏄庯紝骞朵笖鍦ㄧ紪璇戞椂锛岀紪璇戝櫒妫鏌ヨ绫诲瀷鍜岃繍绠楃鏄惁琚厑璁搞
    鍙傝冧互涓嬬ず渚嬫潵澶勭悊鍙橀噺锛
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # 灏嗘暣鏁1鍒嗛厤缁欏彉閲廼锛堝悓鏃垛滃垱寤衡濆彉閲忊濓級
    i = 1
    
    # 灏嗗瓧绗︿覆鈥渇oobar鈥濆垎閰嶇粰鍙橀噺s 
    s = "foobar"
    
    # 灏5鍔犲埌鏁存暟i锛岃繖绛夋晥浜巌 = i + 5 
    i += 5
    # 鐜板湪i鐨勫间负鏁存暟6
    
    # 灏濊瘯瀵筰鍜宻鐩稿姞锛屾墽琛屾椂浼氭姏鍑哄紓甯
    # TypeError: unsupported operand type(s) for +: 'int' and 'str'
    result = i + s
    
    # 鍒犻櫎鍙橀噺鍚屾椂鍙互鈥滃彇娑堝0鏄庘
    # 杩涗竴姝ヨ闂彉閲廼灏嗗紩鍙慛ameError寮傚父锛
    # 鐢变簬璇ュ彉閲忓凡涓嶅瓨鍦
    del i
    
    i += 5 # 鐜板湪鎶涘嚭寮傚父: NameError: name 'i' is not defined
    鎵鏈夌幇鏈夊彉閲忎粎寮曠敤涓涓笺傚湪Python涓紝娌℃湁鏈垎閰嶆垨鏈垵濮嬪寲鐨勫彉閲忋備负浜嗚〃绀烘病鏈夊硷紝Python鎻愪緵浜嗕竴涓壒娈婄殑瀵硅薄锛歂one銆傚湪C鎴朣T涓紝鎮ㄥ皢浣跨敤绌烘寚閽堛傚畠鐨勫敮涓鐩殑鏄〃杈锯滆繖閲屾病鏈夊尖濓紝灏界None瀹為檯涓婃槸璇ョ被鐨勭幇鏈夊疄渚婲oneType銆
    鏁板肩被鍨嬪拰娴偣鏁
    涓嶪EC鎴朇涓殑鏁板崄绉嶆暣鏁扮被鍨嬬浉姣旓紝Python涓彧鏈変竴绉嶆暣鏁扮被鍨嬨侾ython涓殑鏁存暟绫诲瀷娌℃湁鍥哄畾澶у皬銆傜浉鍙嶏紝瀹冧滑浼氭牴鎹渶瑕佸闀匡紝骞朵笖浠呭彈鍙敤鍐呭瓨鐨勯檺鍒躲
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from __future__ import print_function
    
    i = 1
    print(i)
    
    j = 0x1234   # 鍗佸叚杩涘埗, IEC鐨16#1234锛屽崄杩涘埗鐨4660
    k = 0o123    # 鍏繘鍒, IEC鐨8#123锛屽崄杩涘埗鐨83
    l = 0b101010 # 浜岃繘鍒, IEC鐨2#101010锛屽崄杩涘埗鐨42
    print(j, k, l)
    
    m = (2 + 3)*10 # 鐜板湪k鐨勫间负50
    print(m)
    
    n = 10 ** 100 # 10鐨100娆℃柟
    print(n)
    杈撳嚭缁撴灉锛
    1460000022516563.jpg
    Python涓彧鏈変竴绉嶆诞鐐圭被鍨嬶紝绫讳技浜嶪EC鏁版嵁绫诲瀷LREAL銆傚畠鎻愪緵64浣岻EEE娴偣杩愮畻銆
    璇硶鍦ㄥぇ澶氭暟鎯呭喌涓嬬被浼间簬鍩轰簬C鐨勮瑷锛
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # 涓涓畝鍗曠殑娴偣鏁... 
    a = 123.456 
    
    #鍖呭惈鏁存暟鍊2 
    b = 2鐨勬诞鐐规暟
    
    #鍙互蹇界暐鍓嶅闆
    c = .3锛冧笌0.3鐩稿悓
    
    # 鎸囨暟/绉戝琛ㄧず褰㈠紡
    d = -123e-5
    涓や釜鐗规畩鎯呭喌鏄疶rue鍜孎alse锛屼袱涓父閲忓畾涔変簡甯冨皵鍊笺傚畠浠殑琛屼负涓庢暣鏁板0鍜岀浉浼1锛屼笉鍚屼箣澶勫湪浜庡皢瀹冧滑杞崲涓哄瓧绗︿覆骞惰繑鍥炲叾鍚嶇О鏃躲
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # 鍐呯疆鍑芥暟鈥渢ype鈥濆彲鐢ㄤ簬鏌ヨ鍊肩殑绫诲瀷銆
    print("True:  ", True, type(True))
    print("False: ", False, type(False))
    print("1:     ", 1, type(1))
    print("False + 0: ", False + 0, type(False + 0))
    print("True * 5: ", True * 5, type(True * 5))
    杈撳嚭缁撴灉锛
    1460000022516561.jpg
    瀛楃涓
    鍦↖ronPython涓紝瀛楃涓插缁堥噰鐢║nicode锛屼笖涓轰换鎰忛暱搴︺傚鏋滃皢瀹冧滑鍖呭惈鍦'鎴栤滀腑锛屾病鏈変换浣曞尯鍒傚瓧绗︿覆涔熷彲浠ヤ娇鐢ㄤ笁寮曞彿"""鎴'''锛屾潵鍏佽浣跨敤澶氳瀛楃涓叉枃瀛椼
    涓嶤鐩镐技锛屽彲浠ヤ娇鐢ㄥ弽鏂滄潬锛堬級琛ㄧず鐗规畩瀛楃锛岃孖EC涓负姝や娇鐢ㄤ簡缇庡厓绗﹀彿锛$锛夈
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    from __future__ import print_function
    
    a = "a simple string"
    b = 'another string'
    c = "strings may contain 'quotes' of the other type."
    d = "multiple string literals" ' are concatenated ' '''by the parser'''
    e = "Escaping: quotes: \" \' backslash: \\ newline: \r\n ascii code: \x40"
    f = """triple-quoted strings may contain newlines, "single"
    'quotes' and '''multiquotes''' of the other type"""
    g = "脺艌铆莽强膽醛 is also possible: 鍖椾含, 袦芯褋泻胁邪, 螒胃萎谓伪, 丕賱賯丕賴乇丞"
    h = r"c:\raw\strings\retain\backslashes.txt"
    
    # 閬嶅巻涓婇潰瀹氫箟鐨勬墍鏈夊彉閲忕殑搴忓垪锛
    for i in (a,b,c,d,e,f,g,h):
        print(i) # 鎵撳嵃鍙橀噺鐨勫唴瀹
    杈撳嚭缁撴灉锛
    1460000022516559.jpg
    Python娌℃湁瀛楃绫诲瀷銆 閫氳繃浣跨敤闀垮害涓1鐨勫瓧绗︿覆鏉ヨ〃绀哄瓧绗︺傞氳繃杩欑鏂瑰紡锛岄氳繃瀛楃涓茶繘琛岃凯浠f垨鍦ㄥ瓧绗︿覆涓娇鐢ㄤ笅鏍囧皢杩斿洖鍗曞瓧绗﹀瓧绗︿覆銆
    鍒楄〃鍜屽厓缁勶紙鏁版嵁闆嗭級
    鍒楄〃鍜屽厓缁勫熀鏈笂瀵瑰簲浜嶤鍜孖EC涓殑鏁扮粍锛屼絾鏄湁涓浜涙槑鏄剧殑鍖哄埆锛
    • 濮嬬粓妫鏌ョ储寮曡闂傝闂叿鏈夋棤鏁堢储寮曠殑鍒楄〃鎴栧厓缁勫皢寮曞彂寮傚父銆
    • 鍒楄〃鍜屽厓缁勯兘鍙互鍖呭惈涓嶅悓绫诲瀷鐨勫厓绱狅紙鍏朵粬鍒楄〃鍜屽厓缁勪篃鍙互锛夈傝孋鍜孖EC鍒欎笉鍚岋紝鏁扮粍鍙兘鍖呭惈鍗曚竴绫诲瀷鐨勫厓绱犮
    • 鍒楄〃鏄姩鎬佺殑锛屽彲浠ラ殢鏃舵坊鍔狅紝鍒犻櫎鎴栨浛鎹㈠厓绱犮
    • 鍏冪粍涓嶅彲鏇存敼锛氬垱寤哄厓缁勫悗锛屽皢鏃犳硶鍐嶅鍏惰繘琛屼慨鏀广
    鍒楄〃鏄娇鐢╨ist()鏋勯犲嚱鏁板垱寤虹殑銆傘傚厓缁勬槸浣跨敤tuple()鏋勯犲嚱鏁板垱寤虹殑銆
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from __future__ import print_function
    print("Testing tuples and lists")
    
    # 瀹氫箟涓涓厓缁勶紝鍏舵暟瀛椾粠1鍒10锛
    t = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print("Tuple:", t)
    
    # 鎴戜滑鍙互璁块棶鍏冪粍鐨勭6涓厓绱犮
    # 涓嶤涓竴鏍凤紝绱㈠紩璁℃暟浠0寮濮嬨
    print("Element 5:", t[5])
    
    # 浣跨敤鑼冨洿璇硶鍙繘琛屾洿寮哄ぇ鐨勬搷浣滐細
    print("Range[2:5]:", t[2:5]) # 锛冧笅闄愭槸鍖呭惈鐨勶紝涓婇檺鏄帓闄ょ殑銆
    print("Range[2\:\:2]:", t[2\:\:2]) # 浠庣3涓厓绱犲紑濮嬶紝骞堕棿闅旀墦鍗板厓绱犮
    print("Range[-3:-1]:", t[-3:-1]) # 浠庢渶鍚庝竴涓厓绱犵殑绗3涓紑濮嬶紝鍦ㄦ渶鍚庝竴涓厓绱犱箣鍓嶇粨鏉燂紙涓婇檺鏄帓闄ょ殑锛
    print("Range[\:\:-1]:", t[\:\:-1]) # 閫嗗悜鎵撳嵃
    
    # 鍒楄〃绫讳技浜庡厓缁... 
    l = [11, 12, 13, "8", t] # 鍖呭惈娣峰悎绫诲瀷锛3涓暣鏁帮紝涓涓瓧绗︿覆鍜屼笂闈㈠畾涔夌殑鍏冪粍銆
    print("List:", l)
    
    # ...浣嗘槸鍙互鍔ㄦ佹坊鍔犳垨鍒犻櫎鍏冪礌銆
    l.append(9) # 鍦ㄥ垪琛ㄤ腑娣诲姞9銆
    print("List with 9:", l)
    print("List Range[3:6:2]:", l[3:6:2]) # 鎵撳嵃绗4涓拰绗6涓厓绱犮
    
    del l[1] # 鍒犻櫎绱㈠紩1銆12鐨勫厓绱
    print("Removed[1]:", l)
    del l[1:3] # 鍒犻櫎绱㈠紩1鍜2锛屽嵆13鍜'8'銆
    print("Removed[1:3]:", l)
    杈撳嚭缁撴灉锛
    1460000022516560.jpg
    瀛楀吀
    Python杩樺叿鏈夊搱甯岃〃绫诲瀷锛堜篃绉颁负鈥 hashmap鈥濓級銆備笌鍒楄〃涓嶅悓锛屽畠鍙互鐢ㄤ换浣曞厓绱狅紙渚嬪瀛楃涓诧級杩涜绱㈠紩銆傚叾鏋勯犲嚱鏁颁负dict()銆
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from __future__ import print_function
    print("Testing dictionaries")
    
    # 澹版槑涓涓寘鍚笁涓潯鐩殑瀛楀吀锛岀涓変釜鏄垪琛
    d = {1: "a", 2: "b", "my list": [1, 2, 3]}
    print(d)
    
    # 鎵撳嵃閿1鐨勫
    print(d[1])
    
    # 鍒犻櫎甯︽湁"my list"閿殑鍊
    del d["my list"]
    
    # 娣诲姞甯︽湁閿3鐨勫4
    d[3] = 4
    print(d)
    
    # 濡傛灉鎵句笉鍒板瘑閽ワ紝鍒欌済et鈥濇柟娉曡繑鍥炵浜屼釜鍙傛暟銆
    print(d.get(1, 42))
    print(d.get(23, 42))
    
    # 鎵撳嵃瀛楀吀涓殑鎵鏈夐敭
    for key in d:
        print(key)
    
    # 璁块棶鏈煡閿殑绱㈠紩灏嗘姏鍑"KeyError"寮傚父!
    print(d[23])
    缁撴灉杈撳嚭锛
    1460000022516564.jpg
    鐒跺悗鍦ㄦ渶鍚庝竴琛屼腑锛岃剼鏈粓姝細
    1460000022516565.jpg
    鎮ㄥ彲浠ラ氳繃鍗曞嚮鈥 璇︾粏淇℃伅鈥濇寜閽煡鐪嬪爢鏍堣窡韪傚湪杩欓噷锛屾偍鍙互鎵惧埌鏈夊叧琛屽彿27鍜屾湭鐭ュ瘑閽23鐨勪俊鎭
    1460000022516568.jpg
    寰幆
    涓嶤鍜孲T鐨刦or涓嶅悓锛孭ython涓殑寰幆涓嶈绠楀惊鐜彉閲忥紝鑰屾槸鍦ㄥ簭鍒椾笂杩涜杩唬銆傝繖绉嶅簭鍒楀彲浠ユ槸瀛楀吀锛屽垪琛紝鍏冪粍锛屽瓧绗︿覆涓殑瀛楃鎴栨枃浠朵腑鐨勮銆
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from __future__ import print_function
    
    print("Enumerating over a simple list:")
    for i in (1,2,3,4):
        print(i, end=", ") # end=灏嗘崲琛岀鏇挎崲涓衡滐紝鈥 
    print()                # 浣嗗湪鏈渚嬬殑缁撳熬鎴戜滑浠嶇劧闇瑕佹崲琛岀銆
    
    print("Enumerating over the characters in a string:")
    for i in "CODESYS": # 瀛楃琛ㄧず涓洪暱搴︿负1鐨勫瓧绗︿覆銆
        print(i, end=", ")
    print()
    
    print("Enumerating over the integers 1 to 4:")
    for i in range(1, 5): # 涓婇檺鏄帓闄ょ殑銆
        print(i, end=", ")
    print()
    
    print("Enumerating using xrange:")
    for i in xrange(5): # xrange涓巖ange绫讳技锛屼絾鏄渶瑕, 浣嗘槸鍦ㄨ緝澶ц寖鍥村唴闇瑕佽緝灏戠殑鍐呭瓨銆
        print(i, end=", ")
    print()
    
    print("Enumerating including the item number:")
    for i, v in enumerate("CODESYS"):
        print(i, v)
    缁撴灉杈撳嚭锛
    1460000022516567.jpg
    闄や簡for寰幆澶栵紝Python杩樺叿鏈墂hile涓嶤鍜孲T涓殑寰幆闈炲父鐩镐技鐨勫惊鐜細
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    i = 0
    while i < 3;
        print(i)
        i += 1
    if/else绫讳技浜庡叾浠栫紪绋嬭瑷涓殑鏋勯犮
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from __future__ import print_function
    i = int(system.ui.query_string("Please enter an integral number..."))
    if i < 0:
        print("Your number was negative.")
    elif i > 0:
        print("Your number was positive.")
    else:
        print("It seems your number was zero.")
    else鍒嗘敮鏄彲閫夌殑锛屼篃鍙互鏄浂锛屼竴涓垨澶氫釜elif鍒嗘敮銆
    鍑芥暟锛岀被鍜屾柟娉
    Python鍏佽浣跨敤鏂规硶瀹氫箟鍑芥暟鍜岀被銆傚叿鏈夋柟娉曠殑绫诲熀鏈笂绫讳技浜嶴T涓殑鍔熻兘鍧楋紝鎴栬呯被浼间簬C ++锛孞ava鎴朇锛冪瓑璇█鐨勭被銆備絾鏄紝Python涓嶆敮鎸佹帴鍙c傛湁鍏宠缁嗕俊鎭紝鍙互鍙傝働ython鏂囨。浠ュ畾涔夊嚱鏁板拰绫汇
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    #瀹氫箟涓涓叿鏈夊悕绉皊um鍜屼袱涓弬鏁癮鍜宐鐨勫嚱鏁帮細
    def sum(a, b):
        return a + b # we return the sum of a and b.
    
    # 鐜板湪鍙互璋冪敤涓婇潰瀹氫箟鐨勫嚱鏁帮細
    print(sum(5,7))
    
    # 鐜板湪瀹氫箟涓涓被Foo锛
    class Foo:
        # The class gets a method "bar".
        # Note: for methods, the first parameter is always "self" and
        # points to the current instance. This is similar to "this" in
        # ST and other languages.
        def bar(self, a, b):
            print("bar(%s,%s)" % (a,b))
    
    # 鍒涘缓璇ョ被鐨勫疄渚嬶細
    f = Foo()
    
    # 璋冪敤瀹炰緥鐨刡ar鏂规硶銆
    f.bar("some", "params")
    妯″潡鍜屾爣鍑嗗簱
    鍦↖EC涓紝鎮ㄥ彲浠ュ鍏ュ簱浠ヤ緵鍏朵粬涔﹂潰浠g爜閲嶅浣跨敤銆備綔涓烘寕浠讹紝Python涓湁鍙兘瀵煎叆妯″潡銆
    瑕佸垱寤鸿嚜宸辩殑妯″潡锛岃缂栧啓涓涓狿ython鏂囦欢锛岃鏂囦欢瀹氫箟瑕佹彁渚涚殑鍔熻兘鍜岀被銆傚皢姝ゆ枃浠朵繚瀛樺埌涓庣ず渚嬭剼鏈浉鍚岀殑鐩綍涓傚鏋滃懡鍚嶆枃浠朵负mymodule.py锛屽垯鍙互浣跨敤import mymodule瀵煎叆銆
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from math import cos, pi
    
    print(pi) # prints 3.14159265359
    
    print(cos(pi)) # prints -1.0
    浠ヤ笅鍖呭惈鏇村绀轰緥锛岃繖浜涚ず渚嬭闂湁鍏虫搷浣滅郴缁燂紝Python鐗堟湰鍜岃В閲婂櫒鐨勪俊鎭細
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    import os
    print(os.environ["OS"])
    
    from sys import platform, version, executable
    print(platform)
    print(version)
    print(executable)
    1460000022516566.jpg
    浣跨敤Python鑴氭湰璁块棶CODESYS鍔熻兘
    CODESYS涓鸿剼鏈彁渚涚殑鎵鏈夊璞″拰鍛戒护涔熷湪Python妯″潡鈥渟criptengine鈥濅腑銆傛瘡褰撳惎鍔ㄨ剼鏈椂锛岄兘浼氶殣寮忓鍏
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    from scriptengine import
    杩欐牱鍙互杞绘澗璁块棶CODESYS銆備絾鏄紝濡傛灉鎮ㄧ殑鑴氭湰瀵煎叆浜嗛渶瑕佽闂瓹ODESYS API鐨勬ā鍧楋紝鍒欒繖浜涙ā鍧楀繀椤昏嚜宸卞鍏criptengine妯″潡銆
    鍦ㄤ笅琛ㄤ腑锛屾偍灏嗘壘鍒板彲浠ュ湪Python鑴氭湰涓敤浣滃叆鍙g偣鐨勪富瑕佸璞★紙绫诲埆锛夈傛湁鍏冲叆鍙g偣鐨勫叏闈㈡枃妗o紝璇峰弬闃匔ODESYS ScriptEngine鐨凙PI鍙傝冩枃妗c
    bVbGDI6.jpg
    绀轰緥锛氭墦鍗板綋鍓嶉」鐩殑璁惧鏍
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    # 鍚敤鏂扮殑python3鎵撳嵃璇硶
    from __future__ import print_function
    
    # 鎵撳嵃鍑哄綋鍓嶆墦寮鐨勯」鐩腑鐨勬墍鏈夎澶囥
    
    print("--- Printing the devices of the project: ---")
    
    # 瀹氫箟鎵撳嵃鍔熻兘銆
    # 姝ゅ嚱鏁颁互鎵璋撶殑鈥渄ocstring鈥濆紑澶达紝杩欐槸鍦╬ython涓褰曞嚱鏁扮殑鎺ㄨ崘鏂瑰紡銆
    def print_tree(treeobj, depth=0):
        """ Print a device and all its children
    
        Arguments:
        treeobj -- the object to print
        depth -- The current depth within the tree (default 0).
    
        The argument 'depth' is used by recursive call and
        should not be supplied by the user.
        """
    
        # 濡傛灉褰撳墠瀵硅薄鏄澶囷紝鎴戜滑灏嗘墦鍗板悕绉板拰璁惧鏍囪瘑銆
        if treeobj.is_device:
            name = treeobj.get_name(False)
            deviceid = treeobj.get_device_identification()
            print("{0}- {1} {2}".format("--"*depth, name, deviceid))
    
        # 涓哄瓙瀵硅薄閫掑綊璋冪敤print_tree鍑芥暟銆
        for child in treeobj.get_children(False):
            print_tree(child, depth+1)
    
    # 閬嶅巻鎵鏈夐《绾у璞★紝骞朵负鍏惰皟鐢╬rint_tree鍑芥暟銆
    for obj in projects.primary.get_children():
        print_tree(obj)
    
    print("--- Script finished. ---")
    璁惧鏍戯紙浠庘滆澶団濊鍥撅級鏄剧ず鍦ㄦ秷鎭鍥句腑锛屾墍鏈夐潪璁惧瀵硅薄鍧囪蹇界暐锛
    1460000022516569.jpg
    绀轰緥锛氳鍙栧彉閲
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    from __future__ import print_function
    
    # 蹇呰鏃跺叧闂墦寮鐨勯」鐩細
    if projects.primary:
        projects.primary.close()
    
    # 鎵撳紑椤圭洰
    proj = projects.open(r"D:\data\projects\Ampel.project")
    
    # 灏嗏淎mpel.project鈥濊缃负娲诲姩搴旂敤绋嬪簭
    app = proj.active_application
    onlineapp = online.create_online_application(app)
    
    # 鐧诲綍鍒拌澶
    onlineapp.login(OnlineChangeOption.Try, True)
    
    # 濡傛灉搴旂敤绋嬪簭鐨勭姸鎬佷笉鏄湪鈥滆繍琛屸濓紝鍒欒缃负鈥滆繍琛屸
    if not onlineapp.application_state == ApplicationState.run:
        onlineapp.start()
    
    # 绛夊緟1绉
    system.delay(1000)
    
    # 璇籭Var1鐨勫
    value = onlineapp.read_value("PLC_PRG.iVar1")
    
    # 鍦ㄦ秷鎭鍥炬垨鍛戒护琛屼腑鏄剧ず鍊
    print(value)
    
    # 浠庤澶囨敞閿骞跺叧闂淎mpel.project鈥 
    onlineapp.logout()
    proj.close()
    绀轰緥锛氬垱寤哄拰缂栬緫POU
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    from __future__ import print_function
    
    STRUCT_CONTENT = """\
        a : BOOL;
        b : BIT;
        c : BIT;
    """
    
    UNION_WHOLE = """\
    TYPE MyUnion :
    UNION
        Zahl : INT;
        Prozent : MyAlias;
        Bits : MyStruct;
    END_UNION
    END_TYPE
    """
    
    proj = projects.primary
    
    folder = proj.find('DataTypes', recursive = True)[0]
    
    # 鍒涘缓涓涓粨鏋凞UT锛屽苟灏嗗彉閲忓垪琛ㄦ彃鍏ョ浜岃绗0鍒楃殑姝g‘浣嶇疆锛堣缂栧彿浠庣0琛屽紑濮嬶級
    struktur = folder.create_dut('MyStruct') # 榛樿涓篋utType.Structure
    struktur.textual_declaration.insert(2, 0, STRUCT_CONTENT)
    
    # 鍒悕绫诲瀷閫氳繃鍩烘湰绫诲瀷鑾峰彇鍏垛滃唴瀹光濓紝璇ュ熀鏈被鍨嬪皢鏈缁堝湪澹版槑閮ㄥ垎涓互涓琛屽舰寮忓嚭鐜帮細
    # TYPE MyAlias : INT (0..100); END_TYPE
    bereich = folder.create_dut('MyAlias', DutType.Alias, "INT (0..100)")
    
    # 闄や簡灏嗗彉閲忔敞鍏ュ埌鐜版湁鐨勫0鏄庝腑涔嬪锛岃繕鍙互鍙浛鎹㈠畬鏁寸殑澹版槑閮ㄥ垎锛屽寘鎷牱鏉夸唬鐮併
    union = folder.create_dut('MyUnion', DutType.Union)
    union.textual_declaration.replace(UNION_WHOLE)
    绀轰緥锛氱敤鎴风晫闈/涓庣敤鎴风殑浜や簰
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    from __future__ import print_function
    
    """鍦ㄦ秷鎭瓨鍌ㄥ拰UI涓婃墽琛屼竴浜涙祴璇曘"""
    
    print("Some Error, Warning and Information popups:")
    system.ui.error("Fatal error: Everything is OK. :-)")
    system.ui.warning("Your bank account is surprisingly low")
    system.ui.info("Just for your information: 42")
    
    print("Now, we ask the user something.")
    res = system.ui.prompt("Do you like this?", PromptChoice.YesNo, PromptResult.Yes);
    print("The user selected '%s'" % res)
    
    print("Now, the user can choose between custom options:")
    res = system.ui.choose("Please choose:", ("First", 2, 7.5, "Something else"))
    print("The user selected option '%s'" % str(res)) # res鏄竴涓厓缁
    
    print("Now, the user can choose several options:")
    res = system.ui.select_many("Please select one or more options", PromptChoice.OKCancel, PromptResult.OK, ("La Premiere", "The Second", "Das Dritte"))
    print("The returned result is: '%s'" % str(res)) # res鏄竴涓厓缁
    
    print("Now, the user can select files and directories")
    res = system.ui.open_file_dialog("Choose multiple files:", filter="Text files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*", filter_index = 0, multiselect=True)
    print("The user did choose: '%s'" % str(res)) # res鏄竴涓厓缁勶紝鍥犱负multiselect涓簍rue銆
    
    res = system.ui.save_file_dialog("Choose a file to save:", filter="Text files (*.txt)|*.txt|Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*", filter_index = 0)
    print("The user did choose: '%s'" % res)
    
    res = system.ui.browse_directory_dialog("Choose a directory", path="C:\\")
    print("The user did choose: '%s'" % res)
    
    print("Now we query a single line string")
    res = system.ui.query_string("What's your name?")
    print("Nice to meet you, dear %s." % res)
    
    print("Now we query a multi line string")
    res = system.ui.query_string("Please tell me a nice story about your life!", multi_line=True)
    if (res):
        print("Huh, that has been a long text, at least %s characters!" % len(res))
    else:
        print("Hey, don't be lazy!")
    
    print("Username and passwort prompts...")
    res = system.ui.query_password("Please enter your favourite password!", cancellable=True)
    if res:
        print("Huh, it's very careless to tell me your favourite password '%s'!" % res)
    else:
        print("Ok, if you don't want...")
    
    res = system.ui.query_credentials("Now, for real...")
    if res:
        print("Username '%s' and password '%s'" % res) # res鏄2鍏冪粍
    else:
        print("Sigh...")
    绀轰緥锛氭搷浣滈」鐩俊鎭璞
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    from __future__ import print_function
    
    proj = projects.load("D:\Some.library")
    
    info = proj.get_project_info()
    
    # 璁剧疆涓浜涘
    info.company = "Test Library Ltd"
    info.title = "Script Test Project"
    info.version = (0, 8, 15, 4711)
    info.default_namespace = "testlibrary"
    info.author = "Python von Scriptinger"
    
    # 搴撳伐鍏烽摼涓帹鑽愮殑涓浜涘
    info.values["DefaultNamespace"] = "testlibrary"
    info.values["Placeholder"] = "testlibrary"
    info.values["DocFormat"] = "reStructuredText"
    
    # 鐜板湪鎴戜滑璁剧疆涓涓嚜瀹氫箟/渚涘簲鍟嗙壒瀹氱殑鍊笺
    info.values["SpecialDeviceId"] = "PLC0815_4711"
    
    # 鍚敤璁块棶鍣ㄥ姛鑳界殑鐢熸垚锛屽洜姝EC搴旂敤绋嬪簭鍙互鍦ㄤ俊鎭睆骞曚腑鏄剧ず鐗堟湰銆
    info.change_accessor_generation(True)
    
    # 骞跺皢搴撹缃负鍙戝竷
    info.released = True;
    
    proj.save()
    绀轰緥锛氳皟鐢ㄥ閮ㄥ懡浠ゅ苟瀵煎叆PLCOpenXML鏂囦欢
    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    # encoding:utf-8
    # 閫氳繃鍛戒护琛宻vn瀹㈡埛绔粠Subversion瀵煎叆PLCOpenXML涓殑璁惧銆
    
    # 鍚敤鏂扮殑python 3鎵撳嵃璇硶
    from __future__ import print_function
    
    import sys, os
    
    # 涓浜涘彉閲忓畾涔夛細
    SVNEXE = r"C:\Program Files\Subversion\bin\svn.exe"
    XMLURL = "file:///D:/testrepo/testfolder/TestExport.xml"
    PROJECT = r"D:\test.project"
    
    # 娓呯悊鎵鏈夋墦寮鐨勯」鐩細
    if projects.primary:
        projects.primary.close()
    
    # 浠嶴ubversion鑾峰彇plcopenxml鏁版嵁銆
    # 灏嗙▼搴忕殑杈撳嚭鎹曡幏鍒皒mldata鍙橀噺涓
    # 'with'鏋勯犺嚜鍔ㄤ负鎴戜滑鍏抽棴鎵撳紑鐨勭閬撱
    with os.popen('"' + SVNEXE + '" cat ' + XMLURL, 'r') as pipe:
        xmldata = pipe.read()
    
    # 鍒涘缓涓涓柊椤圭洰锛
    proj = projects.create(PROJECT)
    
    # 灏嗘暟鎹鍏ラ」鐩
    proj.import_xml(xmldata, False)
    
    # 鏈鍚庝繚瀛樸 :-)
    proj.save()
    
    print("--- Script finished. ---")
    楂樼骇绀轰緥锛氫粠SVN璋冪敤搴撳苟灏嗗叾瀹夎鍦–ODESYS涓[backcolor=rgba(0, 0, 0, 0.75)]

    [Python] 绾枃鏈煡鐪 澶嶅埗浠g爜
    import tempfile
    
    if projects.primary:
        projects.primary.close()
    
    tempdir = tempfile.mkdtemp()
    URL = "svn://localhost/testrepo/trunk/SvnTestLibrary/"
    
    proj = svn.checkout(URL, tempdir, "testlibrary", as_library=True)
    proj.save()
    
    repo = librarymanager.repositories[0]
    librarymanager.install_library(proj.path, repo, True)
    
    proj.close()




    +10
    鎵撹祻榧撳姳涓涓嬶紒
    鍥炲

    浣跨敤閬撳叿 涓炬姤

  • TA鐨勬瘡鏃ュ績鎯

    2022-4-11 22:35
  • 绛惧埌澶╂暟: 35 澶

    [LV.5]甯镐綇灞呮皯I

    鍙戣〃浜 2020-8-10 18:48:18 | 鏄剧ず鍏ㄩ儴妤煎眰
    鐪嬩笉鎳傦紝涓鑴歌挋钄
    +10
    鎵撹祻榧撳姳涓涓嬶紒

    璇ョ敤鎴蜂粠鏈鍒

    鍙戣〃浜 2020-8-24 17:33:22 | 鏄剧ず鍏ㄩ儴妤煎眰
    鐚摜 鍙戣〃浜 2020-8-10 18:48
    鐪嬩笉鎳傦紝涓鑴歌挋钄

    鐜板湪瓒婃潵瓒婂鐨勮蒋浠堕兘瀵筆ython鎻愪緵浜嗘敮鎸侊紝灏ゅ叾IRpython浣垮緱.net杞欢鑳藉鐩存帴浜巔ython浜や簰
    +10
    鎵撹祻榧撳姳涓涓嬶紒
    鎮ㄩ渶瑕佺櫥褰曞悗鎵嶅彲浠ュ洖甯 鐧诲綍 | 绔嬪嵆娉ㄥ唽 鏂版氮寰崥鐧婚檰

    鏈増绉垎瑙勫垯

    椤堕儴qrcode搴曢儴
    鍏虫敞閰峰痉缃戣闃呭彿锛岃幏鍙栨洿澶氳祫璁紒

    QQ|灏忛粦灞|鎵嬫満鐗|Archiver|閰峰痉璁哄潧 ( 鑻廔CP澶16065247鍙-1 )|缃戠珯鍦板浘

    GMT+8, 2024-4-25 06:41 , Processed in 0.549340 second(s), 69 queries .

    閰峰痉缃

    © 2001-2013 Hicodesys. 鎶鏈敮鎸 by 閰峰痉缃

    蹇熷洖澶 杩斿洖椤堕儴 杩斿洖鍒楄〃