{"id":850,"date":"2018-02-07T23:00:28","date_gmt":"2018-02-07T21:00:28","guid":{"rendered":"http:\/\/www.rocworks.at\/wordpress\/?p=850"},"modified":"2018-07-04T11:26:38","modified_gmt":"2018-07-04T09:26:38","slug":"python-and-wincc-oa","status":"publish","type":"post","link":"https:\/\/www.rocworks.at\/wordpress\/?p=850","title":{"rendered":"Python and WinCC OA&#8230;"},"content":{"rendered":"<p>Connected Python to WinCC OA through a Websocket Manager. Python programs can connect to WinCC OA and read\/write datapoints. Communication is JSON based, it&#8217;s simple to use in Python, see examples below (ws:\/\/rocworks.no-ip.org can be used for tests, but will not be available all the time).<\/p>\n<p><a href=\"https:\/\/github.com\/vogler75\/oa4j-wss\">https:\/\/github.com\/vogler75\/oa4j-wss<\/a><\/p>\n<ol>\n<li>dpGet<\/li>\n<li>dpSet<\/li>\n<li>dpConnect<\/li>\n<li>dpQueryConnect<\/li>\n<li>dpGetPeriod<\/li>\n<li>&#8230; more functions will be implemented<\/li>\n<\/ol>\n<p>Required Python modules:<\/p>\n<ul>\n<li>pip3 install websocket-client<\/li>\n<li>pip3 install matplotlib<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.rocworks.at\/wordpress\/?attachment_id=851\" rel=\"attachment wp-att-851\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-851\" src=\"http:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2018\/02\/Python-WinCCOA-WSS.png\" alt=\"\" width=\"1125\" height=\"924\" srcset=\"https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2018\/02\/Python-WinCCOA-WSS.png 1125w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2018\/02\/Python-WinCCOA-WSS-300x246.png 300w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2018\/02\/Python-WinCCOA-WSS-768x631.png 768w, https:\/\/www.rocworks.at\/wordpress\/wp-content\/uploads\/2018\/02\/Python-WinCCOA-WSS-1024x841.png 1024w\" sizes=\"auto, (max-width: 1125px) 100vw, 1125px\" \/><\/a><\/p>\n<pre>############################################################\r\n# Open Connection\r\n############################################################\r\nimport json\r\nimport ssl\r\nfrom websocket import create_connection\r\nurl='ws:\/\/rocworks.no-ip.org\/winccoa?username=demo&amp;password=demo'\r\nws = create_connection(url, sslopt={\"cert_reqs\": ssl.CERT_NONE})\r\n\r\n############################################################\r\n# dpGetPeriod\r\n############################################################\r\ncmd={'DpGetPeriod': {\r\n 'Dps':['ExampleDP_Trend1.'],\r\n 'T1': '2018-02-07T18:10:00.000', \r\n 'T2': '2018-02-07T23:59:59.999',\r\n 'Count': 0, # Optional (Default=0)\r\n 'Ts': 0 # Optional (0...no ts in result, 1...ts as ms since epoch, 2...ts as ISO8601)\r\n }}\r\nws.send(json.dumps(cmd))\r\nres=json.loads(ws.recv())\r\n#print(res)\r\nif \"System1:ExampleDP_Trend1.:_offline.._value\" in res[\"DpGetPeriodResult\"][\"Values\"]:\r\n values=res[\"DpGetPeriodResult\"][\"Values\"][\"System1:ExampleDP_Trend1.:_offline.._value\"]\r\n print(values)\r\nelse:\r\n print(\"no data found\")\r\n\r\n# Plot result of dpGetPeriod\r\n%matplotlib inline \r\nimport matplotlib.pyplot as plt\r\nplt.plot(values)\r\nplt.ylabel('ExampleDP_Trend1.')\r\nplt.show()\r\n\r\n############################################################\r\n# dpGet\r\n############################################################\r\ncmd={'DpGet': {'Dps':['ExampleDP_Trend1.', 'ExampleDP_Trend2.']}}\r\nws.send(json.dumps(cmd))\r\nres=json.loads(ws.recv())\r\nprint(json.dumps(res, indent=4, sort_keys=True))\r\n\r\n############################################################\r\n# dpSet\r\n############################################################\r\nfrom random import randint\r\ncmd={'DpSet': {'Wait': True, \r\n 'Values':[{'Dp':'ExampleDP_Trend1.','Value': randint(0, 9)}, \r\n {'Dp':'ExampleDP_Trend2.','Value': randint(0, 9)}]}}\r\nws.send(json.dumps(cmd))\r\nres=json.loads(ws.recv())\r\nprint(json.dumps(res, indent=4, sort_keys=True))\r\n\r\n############################################################\r\n# dpConnect\r\n############################################################\r\nfrom threading import Thread\r\n\r\ndef read():\r\n    while True:\r\n        res=json.loads(ws.recv())\r\n        print(res)\r\nThread(target=read).start()\r\n    \r\ncmd={\"DpConnect\": {\"Id\": 1, \"Dps\": [\"ExampleDP_Trend1.\"]}}\r\nws.send(json.dumps(cmd))\r\n\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Connected Python to WinCC OA through a Websocket Manager. Python programs can connect to WinCC OA and read\/write datapoints. Communication is JSON based, it&#8217;s simple to use in Python, see examples below (ws:\/\/rocworks.no-ip.org can be used for tests, but will &hellip; <a href=\"https:\/\/www.rocworks.at\/wordpress\/?p=850\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,3],"tags":[],"class_list":["post-850","post","type-post","status-publish","format-standard","hentry","category-machine-learning","category-wincc-oa"],"_links":{"self":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=850"}],"version-history":[{"count":11,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":862,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/850\/revisions\/862"}],"wp:attachment":[{"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rocworks.at\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}