{"id":1103180,"date":"2025-01-08T16:07:40","date_gmt":"2025-01-08T08:07:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103180.html"},"modified":"2025-01-08T16:07:57","modified_gmt":"2025-01-08T08:07:57","slug":"python%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9ec%e5%af%84%e5%ad%98%e5%99%a8%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103180.html","title":{"rendered":"python\u5982\u4f55\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065124\/65d9ba3e-6d14-44f8-b3be-2a919431ec86.webp\" alt=\"python\u5982\u4f55\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u5229\u7528\u5916\u90e8\u5de5\u5177\u3001\u901a\u8fc7\u4f4e\u7ea7\u786c\u4ef6\u63a5\u53e3\u3001\u4f7f\u7528\u7cfb\u7edf\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5916\u90e8\u5de5\u5177\u5982RW Everything\u6216\u901a\u8fc7\u4f4e\u7ea7\u786c\u4ef6\u63a5\u53e3\uff08\u5982I\/O\u7aef\u53e3\uff09\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\uff0c\u4ee5\u53ca\u5728\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\u65f6\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5229\u7528\u5916\u90e8\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1\u3001RW Everything<\/h4>\n<\/p>\n<p><p>RW Everything \u662f\u4e00\u6b3e\u5f3a\u5927\u7684\u786c\u4ef6\u4fe1\u606f\u8bfb\u53d6\u548c\u4fee\u6539\u5de5\u5177\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u786c\u4ef6\u5bc4\u5b58\u5668\uff0c\u5305\u62ecEC\u5bc4\u5b58\u5668\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528RW Everything\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0b\u8f7d\u5b89\u88c5RW Everything<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7dRW Everything\u5e76\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li><strong>\u6253\u5f00RW Everything<\/strong>\uff1a\u542f\u52a8\u7a0b\u5e8f\u540e\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u4e2a\u7528\u6237\u754c\u9762\uff0c\u663e\u793a\u4e86\u5404\u79cd\u786c\u4ef6\u4fe1\u606f\u3002<\/li>\n<li><strong>\u8bbf\u95eeEC\u5bc4\u5b58\u5668<\/strong>\uff1a\u5728RW Everything\u7684\u83dc\u5355\u680f\u4e2d\uff0c\u9009\u62e9\u201cAccess\u201d-&gt;\u201cEC\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u6253\u5f00EC\u5bc4\u5b58\u5668\u7684\u7a97\u53e3\u3002<\/li>\n<li><strong>\u8bfb\u53d6\u548c\u4fee\u6539\u5bc4\u5b58\u5668\u503c<\/strong>\uff1a\u5728EC\u5bc4\u5b58\u5668\u7a97\u53e3\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u5f53\u524d\u5bc4\u5b58\u5668\u7684\u503c\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u3002\u4fee\u6539\u540e\uff0c\u70b9\u51fb\u201cWrite\u201d\u6309\u94ae\u4ee5\u4fdd\u5b58\u66f4\u6539\u3002<\/li>\n<\/ol>\n<p><h4>2\u3001Python\u8c03\u7528RW Everything<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u811a\u672c\u8c03\u7528RW Everything\u7684\u547d\u4ee4\u884c\u63a5\u53e3\u6765\u5b9e\u73b0\u81ea\u52a8\u5316\u4fee\u6539\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def read_ec_register(address):<\/p>\n<p>    result = subprocess.run([&#39;rw.exe&#39;, &#39;\/command=EC:read&#39;, f&#39;\/address={address}&#39;], capture_output=True, text=True)<\/p>\n<p>    return result.stdout<\/p>\n<p>def write_ec_register(address, value):<\/p>\n<p>    subprocess.run([&#39;rw.exe&#39;, &#39;\/command=EC:write&#39;, f&#39;\/address={address}&#39;, f&#39;\/value={value}&#39;])<\/p>\n<h2><strong>\u8bfb\u53d6EC\u5bc4\u5b58\u56680x20\u7684\u503c<\/strong><\/h2>\n<p>address = &#39;0x20&#39;<\/p>\n<p>print(&quot;Current Value: &quot;, read_ec_register(address))<\/p>\n<h2><strong>\u4fee\u6539EC\u5bc4\u5b58\u56680x20\u7684\u503c<\/strong><\/h2>\n<p>new_value = &#39;0xFF&#39;<\/p>\n<p>write_ec_register(address, new_value)<\/p>\n<p>print(&quot;New Value: &quot;, read_ec_register(address))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u4f4e\u7ea7\u786c\u4ef6\u63a5\u53e3<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbf\u95eeI\/O\u7aef\u53e3<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eeI\/O\u7aef\u53e3\u6765\u8bfb\u53d6\u548c\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u3002\u8fd9\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>inb<\/code>\u548c<code>outb<\/code>\u6307\u4ee4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import fcntl<\/p>\n<h2><strong>\u5b9a\u4e49I\/O\u7aef\u53e3\u5730\u5740<\/strong><\/h2>\n<p>EC_DATA_PORT = 0x62<\/p>\n<p>EC_CMD_PORT = 0x66<\/p>\n<h2><strong>\u6253\u5f00I\/O\u7aef\u53e3<\/strong><\/h2>\n<p>fd = os.open(&quot;\/dev\/port&quot;, os.O_RDWR)<\/p>\n<p>def ec_write_cmd(cmd):<\/p>\n<p>    fcntl.ioctl(fd, EC_CMD_PORT, cmd)<\/p>\n<p>def ec_write_data(data):<\/p>\n<p>    fcntl.ioctl(fd, EC_DATA_PORT, data)<\/p>\n<p>def ec_read_data():<\/p>\n<p>    return fcntl.ioctl(fd, EC_DATA_PORT, 0)<\/p>\n<h2><strong>\u53d1\u9001\u8bfb\u547d\u4ee4<\/strong><\/h2>\n<p>ec_write_cmd(0x80)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>value = ec_read_data()<\/p>\n<p>print(f&quot;EC Register Value: {value}&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e<\/strong><\/h2>\n<p>ec_write_cmd(0x81)<\/p>\n<p>ec_write_data(0xFF)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7cfb\u7edf\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>ctypes<\/code>\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>ctypes<\/code>\u5e93\u6765\u8c03\u7528\u7cfb\u7edfAPI\u8fdb\u884c\u4f4e\u7ea7\u522b\u7684\u786c\u4ef6\u8bbf\u95ee\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u5b9a\u4e49I\/O\u7aef\u53e3\u5730\u5740<\/strong><\/h2>\n<p>EC_DATA_PORT = 0x62<\/p>\n<p>EC_CMD_PORT = 0x66<\/p>\n<h2><strong>\u52a0\u8f7d\u7cfb\u7edf\u5e93<\/strong><\/h2>\n<p>libc = ctypes.CDLL(&quot;libc.so.6&quot;)<\/p>\n<p>def ec_write_cmd(cmd):<\/p>\n<p>    libc.outb(cmd, EC_CMD_PORT)<\/p>\n<p>def ec_write_data(data):<\/p>\n<p>    libc.outb(data, EC_DATA_PORT)<\/p>\n<p>def ec_read_data():<\/p>\n<p>    return libc.inb(EC_DATA_PORT)<\/p>\n<h2><strong>\u53d1\u9001\u8bfb\u547d\u4ee4<\/strong><\/h2>\n<p>ec_write_cmd(0x80)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>value = ec_read_data()<\/p>\n<p>print(f&quot;EC Register Value: {value}&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e<\/strong><\/h2>\n<p>ec_write_cmd(0x81)<\/p>\n<p>ec_write_data(0xFF)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u662f\u4e00\u9879\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u9700\u8981\u8c28\u614e\u8fdb\u884c\u3002<strong>\u5229\u7528\u5916\u90e8\u5de5\u5177\u3001\u901a\u8fc7\u4f4e\u7ea7\u786c\u4ef6\u63a5\u53e3\u3001\u4f7f\u7528\u7cfb\u7edf\u5e93<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002\u5728\u6267\u884c\u8fd9\u4e9b\u64cd\u4f5c\u65f6\uff0c\u52a1\u5fc5\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u5b89\u5168\u6027\uff0c\u907f\u514d\u56e0\u64cd\u4f5c\u4e0d\u5f53\u5bfc\u81f4\u786c\u4ef6\u635f\u574f\u6216\u7cfb\u7edf\u5d29\u6e83\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u64cd\u4f5cEC\u5bc4\u5b58\u5668\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u901a\u5e38\u9700\u8981\u901a\u8fc7\u7279\u5b9a\u7684\u5e93\u6216\u5de5\u5177\uff0c\u4f8b\u5982\u901a\u8fc7\u4e0e\u786c\u4ef6\u63a5\u53e3\u8fdb\u884c\u4ea4\u4e92\u7684\u6a21\u5757\u3002\u4f7f\u7528<code>ctypes<\/code>\u5e93\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u5e95\u5c42API\uff0c\u6216\u8005\u4f7f\u7528\u5982<code>pywin32<\/code>\u7b49\u5e93\u4e0eWindows\u7cfb\u7edf\u7684API\u8fdb\u884c\u4ea4\u4e92\u3002\u786e\u4fdd\u5728\u4fee\u6539\u5bc4\u5b58\u5668\u4e4b\u524d\u5bf9\u76f8\u5173\u786c\u4ef6\u6709\u5145\u5206\u7684\u4e86\u89e3\uff0c\u4ee5\u907f\u514d\u53ef\u80fd\u7684\u786c\u4ef6\u635f\u574f\u3002<\/p>\n<p><strong>\u4fee\u6539EC\u5bc4\u5b58\u5668\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u65f6\uff0c\u52a1\u5fc5\u5c0f\u5fc3\u8c28\u614e\u3002\u9996\u5148\uff0c\u4e86\u89e3\u5bc4\u5b58\u5668\u7684\u529f\u80fd\u53ca\u5176\u5bf9\u7cfb\u7edf\u7684\u5f71\u54cd\u81f3\u5173\u91cd\u8981\u3002\u9519\u8bef\u7684\u503c\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u7a33\u5b9a\u6216\u786c\u4ef6\u6545\u969c\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u8fdb\u884c\u4fee\u6539\u4e4b\u524d\u5907\u4efd\u76f8\u5173\u6570\u636e\uff0c\u5e76\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u8fdb\u884c\u9a8c\u8bc1\uff0c\u786e\u4fdd\u4e0d\u4f1a\u5f71\u54cd\u5b9e\u9645\u8fd0\u884c\u7684\u7cfb\u7edf\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684Python\u5e93\u53ef\u4ee5\u7528\u4e8e\u64cd\u4f5cEC\u5bc4\u5b58\u5668\uff1f<\/strong><br \/>\u64cd\u4f5cEC\u5bc4\u5b58\u5668\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2aPython\u5e93\u3002\u5176\u4e2d\uff0c<code>pywin32<\/code>\u9002\u7528\u4e8eWindows\u5e73\u53f0\uff0c\u63d0\u4f9b\u4e86\u8bbf\u95ee\u7cfb\u7edf\u8c03\u7528\u7684\u80fd\u529b\u3002\u5bf9\u4e8eLinux\u7528\u6237\uff0c<code>pyudev<\/code>\u6216<code>pyserial<\/code>\u53ef\u4ee5\u7528\u4e8e\u4e0e\u8bbe\u5907\u901a\u4fe1\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u65f6\uff0c\u9700\u8003\u8651\u7cfb\u7edf\u7c7b\u578b\u53ca\u5177\u4f53\u9700\u6c42\uff0c\u786e\u4fdd\u5e93\u7684\u529f\u80fd\u6ee1\u8db3\u4f60\u7684\u64cd\u4f5c\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fee\u6539EC\u5bc4\u5b58\u5668\u7684\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u5229\u7528\u5916\u90e8\u5de5\u5177\u3001\u901a\u8fc7\u4f4e\u7ea7\u786c\u4ef6\u63a5\u53e3\u3001\u4f7f\u7528\u7cfb\u7edf\u5e93\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u5916\u90e8\u5de5\u5177\u5982 [&hellip;]","protected":false},"author":3,"featured_media":1103205,"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\/1103180"}],"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=1103180"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103180\/revisions"}],"predecessor-version":[{"id":1103211,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103180\/revisions\/1103211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103205"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}