{"id":561,"date":"2017-02-18T19:43:32","date_gmt":"2017-02-18T11:43:32","guid":{"rendered":"http:\/\/192.168.1.20\/?p=561"},"modified":"2017-10-23T11:57:21","modified_gmt":"2017-10-23T03:57:21","slug":"python-serial{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}e6{d4daabd1ba368d9860c36387ddb72bbda14f8f9559d69d60a952800885260c03}a8{47dd41ec6bdcbd9a214c783a3735392b82ea6d1d918378c5a5","status":"publish","type":"post","link":"http:\/\/alextop.top\/?p=561","title":{"rendered":"Python serial\u6a21\u5757\u6742\u8c08"},"content":{"rendered":"<p>\u5148\u6765\u8bf4\u8bf4\u5b98\u65b9\u6587\u6863\n<\/p>\n<p><a href=\"http:\/\/pythonhosted.org\/pyserial\/shortintro.html#opening-serial-ports\">http:\/\/pythonhosted.org\/pyserial\/shortintro.html#opening-serial-ports<\/a>\n\t<\/p>\n<p style=\"text-align: justify; background: white\"><span style=\"color:black; font-size:12pt\"><span style=\"font-family:Arial\">Open port at &#8220;9600,8,N,1&#8221;, no timeout:<\/span><span style=\"font-family:\u5b8b\u4f53\">\uff08<\/span><span style=\"font-family:Arial\">8<\/span><span style=\"font-family:\u5b8b\u4f53\">\uff0c<\/span><span style=\"font-family:Arial\">n<\/span><span style=\"font-family:\u5b8b\u4f53\">\uff0c<\/span><span style=\"font-family:Arial\">1<\/span><span style=\"font-family:\u5b8b\u4f53\">\u662f<\/span><span style=\"font-family:Arial\"><br \/>\n\t\t\t<\/span><span style=\"font-family:\u5b8b\u4f53\">\u5b57\u8282\u6570<\/span><span style=\"font-family:Arial\">8  <\/span><span style=\"font-family:\u5b8b\u4f53\">\u5947\u5076\u6821\u9a8c\u65e0<\/span><span style=\"font-family:Arial\"><br \/>\n\t\t\t<\/span><span style=\"font-family:\u5b8b\u4f53\">\u505c\u6b62\u4f4d<\/span><span style=\"font-family:Arial\">1<\/span><span style=\"font-family:\u5b8b\u4f53\">\uff09<\/span><span style=\"font-family:Arial\"><br \/>\n\t\t\t<\/span><\/span><\/p>\n<p style=\"background: #eeffcc\"><span style=\"color:#c65d09; font-family:\u5b8b\u4f53; font-size:12pt\"><strong>&gt;&gt;&gt; <span style=\"color:#007020\">import<\/span><\/strong><span style=\"color:#333333\"><br \/>\n\t\t\t\t<span style=\"color:#0e84b5\"><strong>serial<\/strong><span style=\"color:#333333\"><br \/>\n\t\t\t\t\t<\/span><\/span><\/span><\/span><\/p>\n<p style=\"background: #eeffcc\"><span style=\"color:#c65d09; font-family:\u5b8b\u4f53; font-size:12pt\"><strong>&gt;&gt;&gt; <\/strong><span style=\"color:#333333\">ser <span style=\"color:#666666\">=<span style=\"color:#333333\"> serial<span style=\"color:#666666\">.<span style=\"color:#333333\">Serial(<span style=\"color:#4070a0\">&#8216;\/dev\/ttyUSB0&#8217;<span style=\"color:#333333\">)  <span style=\"color:#408090\"><em># open serial port<\/em><span style=\"color:#333333\"><br \/>\n\t\t\t\t\t\t\t\t\t\t\t<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p style=\"background: #eeffcc\"><span style=\"color:#c65d09; font-family:\u5b8b\u4f53; font-size:12pt\"><strong>&gt;&gt;&gt; <span style=\"color:#007020\">print<\/span><\/strong><span style=\"color:#333333\">(ser<span style=\"color:#666666\">.<span style=\"color:#333333\">name)         <span style=\"color:#408090\"><em># check which port was really used<\/em><span style=\"color:#333333\"><br \/>\n\t\t\t\t\t\t\t<\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p style=\"background: #eeffcc\"><span style=\"color:#c65d09; font-family:\u5b8b\u4f53; font-size:12pt\"><strong>&gt;&gt;&gt; <\/strong><span style=\"color:#333333\">ser<span style=\"color:#666666\">.<span style=\"color:#333333\">write(b<span style=\"color:#4070a0\">&#8216;hello&#8217;<span style=\"color:#333333\">)     <span style=\"color:#408090\"><em># write a string<\/em><span style=\"color:#333333\"><br \/>\n\t\t\t\t\t\t\t\t\t<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p style=\"background: #eeffcc\"><span style=\"color:#c65d09; font-family:\u5b8b\u4f53; font-size:12pt\"><strong>&gt;&gt;&gt; <\/strong><span style=\"color:#333333\">ser<span style=\"color:#666666\">.<span style=\"color:#333333\">close()             <span style=\"color:#408090\"><em># close port<\/em><span style=\"color:#333333\"><br \/>\n\t\t\t\t\t\t\t<\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p>\n\u00a0<\/p>\n<p>\u793a\u4f8b\n<\/p>\n<p>import serial\n<\/p>\n<p>ser = serial.Serial( #\u4e0b\u9762\u8fd9\u4e9b\u53c2\u6570\u6839\u636e\u60c5\u51b5\u4fee\u6539\n<\/p>\n<p>\u00a0\u00a0port=&#8217;\/dev\/tty***&#8217;,\n<\/p>\n<p>\u00a0\u00a0baudrate=9600,\n<\/p>\n<p>\u00a0\u00a0parity=serial.PARITY_ODD,\n<\/p>\n<p>\u00a0\u00a0stopbits=serial.STOPBITS_TWO,\n<\/p>\n<p>\u00a0\u00a0bytesize=serial.SEVENBITS\n<\/p>\n<p>)\n<\/p>\n<p>\n\u00a0<\/p>\n<p>\u89e3\u91ca\uff1a\n<\/p>\n<p>port:\u5982COM1,COM2,COM3,COM4&#8230;&#8230;\u5982\u679cport\u8bbe\u7f6e\u4e3a0\u5bf9\u5e94\u7684\u4e3aCOM1\n<\/p>\n<p>baudrate\uff1a\u6ce2\u7279\u7387\n<\/p>\n<p>bytesize:\u6570\u636e\u4f4d\n<\/p>\n<p>stopbits:\u505c\u6b62\u4f4d\n<\/p>\n<p>timeout:\u8d85\u65f6\u65f6\u95f4\n<\/p>\n<ul>\n<li>timeout\u00a0=\u00a0None: \u957f\u65f6\u95f4\u7b49\u5f85\n<\/li>\n<li>timeout\u00a0=\u00a00: \u4e0d\u963b\u585e\u5f62\u5f0f (\u8bfb\u5b8c\u4e4b\u540e\u5c31\u8fd4\u56de)\n<\/li>\n<li>timeout\u00a0=\u00a0x: x\u79d2\u540e\u8d85\u65f6 (float allowed)\n<\/li>\n<\/ul>\n<p>\n\u00a0<\/p>\n<p>\n\u00a0<\/p>\n<p>\n\u00a0<\/p>\n<p>\n\u00a0<\/p>\n<p>\u5173\u4e8e16\u8fdb\u5236\u6570\u8f6c\u6362acs\u7801\u7684\u6587\u7ae0\uff1ahttp:\/\/blog.csdn.net\/xhao014\/article\/details\/7640568<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u6765\u8bf4\u8bf4\u5b98\u65b9\u6587\u6863 http:\/&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,10,15],"tags":[],"class_list":["post-561","post","type-post","status-publish","format-standard","hentry","category-python","category-jishu","category-reapberry"],"_links":{"self":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/561"}],"collection":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=561"}],"version-history":[{"count":1,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions"}],"predecessor-version":[{"id":998,"href":"http:\/\/alextop.top\/index.php?rest_route=\/wp\/v2\/posts\/561\/revisions\/998"}],"wp:attachment":[{"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/alextop.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}