{"id":10021063,"date":"2025-11-04T17:02:53","date_gmt":"2025-11-04T09:02:53","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021063"},"modified":"2025-11-04T17:02:53","modified_gmt":"2025-11-04T09:02:53","slug":"python%e9%80%9a%e8%bf%87tcp%e8%af%bb%e5%8f%96modbus%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021063","title":{"rendered":"Python\u901a\u8fc7TCP\u8bfb\u53d6ModBus\u7684\u6570\u636e"},"content":{"rendered":"<p>\u4e70\u4e86\u4e2a485\/\u4e32\u53e3\u8f6cTCP\u7684\u8f6c\u63a5\u6a21\u5757\uff0c\u80fd\u591f\u628a485 modbus\u7684\u6570\u636e\u76f4\u63a5\u8f6c\u6362\u4e3aTCP\u6570\u636e\uff08\u7c7b\u4f3c\u4e32\u53e3\u670d\u52a1\u5668\uff09\uff0c\u76f4\u63a5\u8bfb485\u7684\u6570\u636e\u53ef\u4ee5\u7528modbus_tk\u7684RtuMaster\uff0c\u8bfbmodbus_tcp\u7684\u6570\u636e\u4e5f\u53ef\u4ee5\u7528TcpMaster\uff0c\u4f46\u662f\u76f4\u63a5\u628a485\u8f6ctcp\u7684\u5c31\u4e0d\u77e5\u9053\u7528\u54ea\u4e2a\u4e86\uff0c\u56e0\u4e3a\u6570\u636e\u5e27\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n<p>\u53ef\u4ee5\u5148\u5b89\u88c5\u76f8\u5173\u7684\u5305<\/p>\n<p>pip install\u00a0modbus_tk\u00a0modbus_tcprtu<\/p>\n<p>\u7136\u540e\u8fd9\u6837<\/p>\n<pre><code class=\"Python\">import modbus_tk.defines as cst\nfrom modbus_tcprtu import TcpRtuMaster\n\n# create a master\nmaster = TcpRtuMaster(host='127.0.0.1', port=9001)\nmaster.set_timeout(2)\nfor i in range(10):\n    # read data from modbus\n    # just use as modbus_tk.Master\n    print(i, master.execute(1, cst.READ_HOLDING_REGISTERS, 0, i + 1))<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047367773\">https:\/\/segmentfault.com\/a\/1190000047367773<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e70\u4e86\u4e2a485\/\u4e32\u53e3\u8f6cTCP\u7684\u8f6c\u63a5\u6a21\u5757\uff0c\u80fd\u591f\u628a485 modbus\u7684\u6570\u636e\u76f4\u63a5\u8f6c\u6362\u4e3aTCP\u6570\u636e\uff08\u7c7b\u4f3c\u4e32\u53e3\u670d\u52a1\u5668\uff09\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021063","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021063"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021063\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}