{"id":1000308,"date":"2024-12-27T09:48:52","date_gmt":"2024-12-27T01:48:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000308.html"},"modified":"2024-12-27T09:48:55","modified_gmt":"2024-12-27T01:48:55","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8opc%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000308.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528opc\u7ec4\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074943\/191e46c2-4b2d-4ad9-8c35-384fc65b2d37.webp\" alt=\"python\u5982\u4f55\u8c03\u7528opc\u7ec4\u4ef6\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u8c03\u7528OPC\uff08OLE for Process Control\uff09\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<\/strong><code>OpenOPC<\/code>\u5e93\u3001<code>pyOPC<\/code>\u5e93<strong>\u3001\u6216\u8005\u901a\u8fc7COM\u63a5\u53e3\u76f4\u63a5\u4e0eOPC\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1<\/strong>\u3002\u5728\u8fd9\u4e9b\u9009\u9879\u4e2d\uff0cOpenOPC\u662f\u6bd4\u8f83\u6d41\u884c\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u3001\u652f\u6301\u591a\u79cdOPC\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u5177\u6709\u826f\u597d\u7684\u6587\u6863\u652f\u6301\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528OpenOPC\u5e93\u6765\u8c03\u7528OPC\u7ec4\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>OpenOPC\u5e93\u662f\u57fa\u4e8ePython\u7684\u4e00\u4e2a\u5f00\u6e90\u5e93\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4f7f\u7528Python\u811a\u672c\u4e0eOPC\u6570\u636e\u8bbf\u95ee\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\u3002<strong>\u5728\u4f7f\u7528OpenOPC\u5e93\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5OpenOPC\u5e93\u4ee5\u53caPython\u73af\u5883<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u53ef\u4ee5\u5b9e\u73b0\u4e0eOPC\u670d\u52a1\u5668\u7684\u8fde\u63a5\u548c\u6570\u636e\u4ea4\u4e92\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5OpenOPC<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python\u73af\u5883<\/strong><br \/>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u3002\u5efa\u8bae\u4f7f\u7528Python 3.x\u7248\u672c\uff0c\u56e0\u4e3a\u5f88\u591a\u5e93\u73b0\u5728\u90fd\u5728\u5411Python 3\u8fc1\u79fb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5OpenOPC\u5e93<\/strong><br \/>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5OpenOPC\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install OpenOPC-Python3x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6839\u636e\u60a8\u7684Python\u7248\u672c\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u547d\u4ee4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Pywin32\u5e93<\/strong><br \/>OpenOPC\u5e93\u4f9d\u8d56\u4e8ePywin32\u5e93\uff0c\u56e0\u6b64\u4e5f\u9700\u8981\u5b89\u88c5Pywin32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528OpenOPC\u8fde\u63a5\u5230OPC\u670d\u52a1\u5668<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fde\u63a5\u5230OPC\u670d\u52a1\u5668<\/strong><br \/>\u4f7f\u7528OpenOPC\u5e93\u8fde\u63a5\u5230OPC\u670d\u52a1\u5668\u7684\u57fa\u672c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import OpenOPC<\/p>\n<p>opc = OpenOPC.client()<\/p>\n<p>opc.connect(&#39;Matrikon.OPC.Simulation.1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>Matrikon.OPC.Simulation.1<\/code>\u662f\u4e00\u4e2a\u793a\u4f8bOPC\u670d\u52a1\u5668\u540d\u79f0\uff0c\u60a8\u9700\u8981\u5c06\u5176\u66ff\u6362\u4e3a\u5b9e\u9645\u4f7f\u7528\u7684OPC\u670d\u52a1\u5668\u540d\u79f0\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4f\u89c8\u670d\u52a1\u5668\u4e2d\u7684OPC\u9879<\/strong><br \/>\u4e00\u65e6\u8fde\u63a5\u6210\u529f\uff0c\u53ef\u4ee5\u6d4f\u89c8\u670d\u52a1\u5668\u4e2d\u7684OPC\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = opc.list()<\/p>\n<p>print(items)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5217\u51fa\u670d\u52a1\u5668\u4e2d\u53ef\u7528\u7684OPC\u9879\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6OPC\u9879\u6570\u636e<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6OPC\u9879\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = opc.read(&#39;Random.Real8&#39;)<\/p>\n<p>print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>Random.Real8<\/code>\u662f\u4e00\u4e2a\u793a\u4f8bOPC\u9879\u540d\uff0c\u60a8\u9700\u8981\u5c06\u5176\u66ff\u6362\u4e3a\u5b9e\u9645\u7684OPC\u9879\u540d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5199\u5165OPC\u9879\u6570\u636e<\/strong><br \/>\u540c\u6837\uff0c\u53ef\u4ee5\u5199\u5165\u6570\u636e\u5230OPC\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">opc.write((&#39;Random.Real8&#39;, 42.0))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406OPC\u901a\u4fe1\u4e2d\u7684\u5e38\u89c1\u95ee\u9898<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fde\u63a5\u95ee\u9898<\/strong><br \/>\u5728\u8fde\u63a5OPC\u670d\u52a1\u5668\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u8fde\u63a5\u7684\u95ee\u9898\u3002\u8fd9\u901a\u5e38\u662f\u7531\u4e8e\u7f51\u7edc\u914d\u7f6e\u95ee\u9898\u6216\u8005OPC\u670d\u52a1\u5668\u914d\u7f6e\u4e0d\u5f53\u9020\u6210\u7684\u3002\u786e\u4fdd\u9632\u706b\u5899\u5141\u8bb8OPC\u901a\u4fe1\uff0c\u68c0\u67e5OPC\u670d\u52a1\u5668\u7684DCOM\u8bbe\u7f6e\uff0c\u5e76\u786e\u4fdd\u5ba2\u6237\u7aef\u5177\u6709\u8db3\u591f\u7684\u6743\u9650\u8bbf\u95ee\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u8bfb\u5199\u95ee\u9898<\/strong><br \/>\u5728\u8bfb\u53d6\u6216\u5199\u5165OPC\u6570\u636e\u65f6\uff0c\u5982\u679c\u9047\u5230\u95ee\u9898\uff0c\u9996\u5148\u68c0\u67e5OPC\u9879\u540d\u79f0\u662f\u5426\u6b63\u786e\u3002\u53e6\u5916\uff0c\u786e\u4fddOPC\u670d\u52a1\u5668\u6b63\u5e38\u8fd0\u884c\uff0c\u5e76\u4e14\u6ca1\u6709\u5904\u4e8e\u7ef4\u62a4\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><br \/>\u5982\u679c\u5728\u5927\u89c4\u6a21\u6570\u636e\u4ea4\u4e92\u4e2d\u9047\u5230\u6027\u80fd\u74f6\u9888\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6279\u91cf\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\uff0c\u8fd9\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u7f51\u7edc\u901a\u4fe1\u5f00\u9500\u3002\u4f8b\u5982\uff0c\u4f7f\u7528OpenOPC\u7684<code>read<\/code>\u548c<code>write<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5217\u8868\u6765\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5176\u4ed6Python OPC\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86OpenOPC\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0Python\u4e0eOPC\u670d\u52a1\u5668\u7684\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>pyOPC<\/strong><br \/>pyOPC\u662f\u4e00\u4e2a\u8f83\u4e3a\u57fa\u7840\u7684OPC\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684OPC\u6570\u636e\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>opcua<\/strong><br \/>opcua\u5e93\u652f\u6301OPC UA\uff08\u7edf\u4e00\u67b6\u6784\uff09\u534f\u8bae\uff0c\u5b83\u6bd4\u4f20\u7edf\u7684OPC DA\uff08\u6570\u636e\u8bbf\u95ee\uff09\u534f\u8bae\u66f4\u4e3a\u73b0\u4ee3\u5316\uff0c\u9002\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u5b89\u5168\u6027\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>python-opc<\/strong><br \/>\u8fd9\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684OPC\u5e93\uff0c\u9002\u5408\u4e8e\u7b80\u5355\u7684OPC\u6570\u636e\u4ea4\u4e92\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9Python OPC\u5e93\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u73af\u5883\u6761\u4ef6\u8fdb\u884c\u9009\u62e9\u3002<strong>OpenOPC\u5e93\u4ee5\u5176\u6613\u7528\u6027\u548c\u8f83\u597d\u7684\u6587\u6863\u652f\u6301\u800c\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce<\/strong>\uff0c\u4f46\u5982\u679c\u9700\u8981\u66f4\u5148\u8fdb\u7684\u7279\u6027\uff0c\u5982\u8de8\u5e73\u53f0\u652f\u6301\u548c\u5b89\u5168\u6027\uff0copcua\u5e93\u53ef\u80fd\u4f1a\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u4e0eOPC\u901a\u4fe1\u65f6\uff0c\u5e94\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fdd\u73af\u5883\u51c6\u5907\u5145\u5206<\/strong><br \/>\u786e\u4fddPython\u73af\u5883\u548c\u6240\u9700\u7684\u5e93\u90fd\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u4e14\u517c\u5bb9\u4e8e\u5f53\u524d\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5<\/strong><br \/>\u5728\u5b9e\u9645\u90e8\u7f72\u524d\uff0c\u5e94\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u6ce8\u5b89\u5168\u6027<\/strong><br \/>\u5c24\u5176\u662f\u5728\u5de5\u4e1a\u81ea\u52a8\u5316\u73af\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u4e0d\u53ef\u5ffd\u89c6\u7684\u3002\u5e94\u786e\u4fdd\u901a\u4fe1\u7684\u5b89\u5168\u6027\uff0c\u5e76\u59a5\u5584\u7ba1\u7406\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8c03\u7528OPC\u7ec4\u4ef6\uff0c\u5b9e\u73b0\u4e0eOPC\u670d\u52a1\u5668\u7684\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u5982\u4f55\u4e0eOPC\u7ec4\u4ef6\u8fdb\u884c\u901a\u4fe1\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4e0eOPC\u7ec4\u4ef6\u8fdb\u884c\u901a\u4fe1\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e13\u95e8\u7684\u5e93\uff0c\u5982<code>OpenOPC<\/code>\u6216<code>opcua<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u8fde\u63a5\u5230OPC\u670d\u52a1\u5668\u3001\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u3002\u5b89\u88c5\u8fd9\u4e9b\u5e93\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efaOPC\u5ba2\u6237\u7aef\u5b9e\u4f8b\u5e76\u4f7f\u7528\u63d0\u4f9b\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528OPC\u65f6\u9700\u8981\u54ea\u4e9b\u524d\u7f6e\u6761\u4ef6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fde\u63a5OPC\u7ec4\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5\u6240\u9700\u7684OPC\u5e93\uff0c\u5e76\u914d\u7f6e\u597d\u76f8\u5e94\u7684OPC\u670d\u52a1\u5668\u3002\u6b64\u5916\uff0c\u4e86\u89e3OPC\u7684\u57fa\u672c\u6982\u5ff5\u548c\u5de5\u4f5c\u539f\u7406\u5c06\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002\u786e\u4fddPython\u73af\u5883\u5df2\u6b63\u786e\u8bbe\u7f6e\uff0c\u4e14\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u4ee5\u4fbf\u987a\u5229\u8bbf\u95eeOPC\u670d\u52a1\u5668\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u4e0eOPC\u4e4b\u95f4\u7684\u8fde\u63a5\u95ee\u9898\uff1f<\/strong><br \/>\u5982\u679c\u5728\u8fde\u63a5OPC\u670d\u52a1\u5668\u65f6\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u6765\u89e3\u51b3\uff1a\u786e\u8ba4OPC\u670d\u52a1\u5668\u6b63\u5728\u8fd0\u884c\u4e14\u53ef\u8bbf\u95ee\uff0c\u68c0\u67e5\u9632\u706b\u5899\u8bbe\u7f6e\u662f\u5426\u963b\u6b62\u4e86\u8fde\u63a5\u8bf7\u6c42\uff0c\u4ee5\u53ca\u786e\u4fdd\u5ba2\u6237\u7aef\u914d\u7f6e\u4e0e\u670d\u52a1\u5668\u8bbe\u7f6e\u5339\u914d\u3002\u8c03\u8bd5\u65f6\uff0c\u53ef\u4ee5\u67e5\u770b\u65e5\u5fd7\u4fe1\u606f\uff0c\u5e2e\u52a9\u8bc6\u522b\u5177\u4f53\u7684\u8fde\u63a5\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u8c03\u7528OPC\uff08OLE for Process Control\uff09\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528OpenOPC\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1000315,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000308"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1000308"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000308\/revisions"}],"predecessor-version":[{"id":1000316,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000308\/revisions\/1000316"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000315"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}