{"id":141,"date":"2017-03-29T22:48:43","date_gmt":"2017-03-29T14:48:43","guid":{"rendered":"https:\/\/himmel.tech\/wp\/?p=141"},"modified":"2017-03-29T22:48:43","modified_gmt":"2017-03-29T14:48:43","slug":"%e5%bc%80%e5%8f%91%e6%97%a5%e5%bf%97-170329","status":"publish","type":"post","link":"https:\/\/himmel.tech\/wp\/?p=141","title":{"rendered":"\u5f00\u53d1\u65e5\u5fd7 170329"},"content":{"rendered":"<p>\u9065\u63a7\u7c97\u7c97\u7684\u53ef\u4ee5\u8dd1\u901a\u4e86\uff0c\u4eca\u5929\u5c31\u5f00\u59cb\u505a\u81ea\u52a8\u5bfc\u822a\u76f8\u5173\u7684\u4e1c\u897f\u4e86\u3002\u60f3\u4e86\u4e00\u4e0b\uff0c\u4e0d\u5982\u5148\u5904\u7406GPS\u3002\u6211\u7684GPS\u662f\u4e32\u53e3\u7684GPS\uff0c\u56e0\u6b64\u6211\u9700\u8981\u4e00\u4e2a\u51fd\u6570\uff0c\u5927\u6982getline\u7684\u6548\u679c\u5dee\u4e0d\u591a\u3002<\/p>\n<p>\u5982\u679c\u662fSTM32\u7684\u8bdd\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u8c03HAL\u7684\u51fd\u6570\u6765\u8bfb\u8bfb\u5199\u5199\u5c31\u597d\u4e86\uff1b\u53e6\u5916\u542c\u8bf4\u6811\u8393\u6d3e\u4e0a\u7528Python\u4e5f\u633a\u65b9\u4fbf\u7684\u3002\u53ef\u60dc\u8fd9\u6b21\u662f\u6253\u7b97\u7528Raspberry Pi\uff0c\u8fd8\u4e0d\u80fd\u7528Python\uff0c\u53ea\u80fd\u7528C\/C++\u3002<\/p>\n<p>\u5148\u653e\u51fa\u6765\u6211\u7684<a href=\"http:\/\/blog.chinaunix.net\/uid-10747583-id-96094.html\">\u53c2\u8003\u6587\u7ae0<\/a>\uff0c\u57fa\u672c\u5c31\u6309\u7167\u8fd9\u4e2a\u914d\u5c31\u884c\uff0c\u53e6\u5916\u90a3\u4e2acfmakeraw\u51fd\u6570\u6700\u597d\u522b\u7528\u2026\u2026<\/p>\n<p>\u5176\u5b9e\u672c\u6765\u4e5f\u6ca1\u4ec0\u4e48\uff0c\u4e0d\u8fc7\u4ee5\u524dC\u8bed\u8a00\u5b66\u7684\u4e0d\u7262\uff0c\u5bf9stdio\u7684\u7f13\u51b2\u533a\u7406\u89e3\u4e0d\u5230\u4f4d\uff0cprintf\u7684\u65f6\u5019\u6ca1\u6709\\n\uff0c\u5bfc\u81f4\u7f13\u51b2\u533a\u6ca1\u5237\u65b0\u3002\u4ee5\u524d\u77e5\u9053iostream\u7684\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f46printf\u5c31\u6ca1\u5f80\u8fd9\u832c\u60f3\uff0c\u5403\u4e86\u4e2a\u5927\u4e8f\uff0c\u771f\u662f\u811a\u90fd\u8981\u62a0\u70c2\u4e86\u2026\u2026<\/p>\n<p>\u65e2\u7136\u662fC++\uff0c\u90a3\u5c31\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b\u5427\uff08\u7136\u800c\u5e76\u6ca1\u6709\u4ec0\u4e48\u5375\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5176\u5b9e\u53c2\u8003\u7684\u90a3\u7bc7\u6587\u7ae0\u8bb2\u7684\u633a\u597d\uff0c\u6211\u53ea\u662f\u7a0d\u52a0\u6539\u52a8\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;sys\/types.h&gt;\n#include &lt;sys\/stat.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;termios.h&gt;\n#include &lt;stdio.h&gt;\n\n\/* baudrate settings are defined in , which is included by *\/\n\/\/ \u6ce2\u7279\u7387\u7684\u8bbe\u7f6e\u5b9a\u4e49\u5728 . \u5305\u542b\u5728 \u91cc\n#define BAUDRATE B115200 \n\n\/* change this definition for the correct port *\/\n\/\/ \u5b9a\u4e49\u60a8\u6240\u9700\u8981\u7684\u4e32\u53e3\u53f7\n#define MODEMDEVICE \"\/dev\/ttyS1\"\n\n#define _POSIX_SOURCE 1 \/*POSIX compliant source POSIX\u7cfb\u7edf\u517c\u5bb9*\/\n\n#define FALSE 0\n#define TRUE 1\n\nvolatile int STOP=FALSE; \n\nmain() {\nint fd,c, res;\nstruct termios oldtio,newtio;\nchar buf[255];\n\n\/* Open modem device for reading and writing and not as controlling \ntty because we don't want to get killed if linenoise sends CTRL-C.\n\u5f00\u542f\u8bbe\u5907\u7528\u4e8e\u8bfb\u5199\uff0c\u4f46\u662f\u4e0d\u8981\u4ee5\u63a7\u5236 tty \u7684\u6a21\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u5e76\u4e0d\u5e0c\u671b\u5728\u53d1\u9001 Ctrl-C\n\u540e\u7ed3\u675f\u6b64\u8fdb\u7a0b\n*\/\n\nfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); \nif (fd &lt;0) {perror(MODEMDEVICE); exit(-1); }\ntcgetattr(fd,&amp;oldtio); \/* save current serial port settings *\/\n             \/\/ \u50a8\u5b58\u5f53\u524d\u7684\u4e32\u53e3\u8bbe\u7f6e\nbzero(&amp;newtio, sizeof(newtio)); \/* clear struct for new port settings *\/\n                                     \/\/ \u6e05\u7a7a\u65b0\u7684\u4e32\u53e3\u8bbe\u7f6e\u7ed3\u6784\u4f53\n\/* \n BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.\n CRTSCTS : output hardware flow control (only used if the cable has all \n            ecessary lines. See sect. 7 of Serial-HOWTO)\n  CS8 : 8n1 (8bit,no parity,1 stopbit)\n  CLOCAL : local connection, no modem contol\n  CREAD : enable receiving characters\n  BAUDRATE: \u8bbe\u7f6e\u4e32\u53e3\u7684\u4f20\u8f93\u901f\u7387bps, \u4e5f\u53ef\u4ee5\u4f7f\u7528 cfsetispeed \u548c cfsetospeed \u6765\u8bbe\u7f6e\n  CRTSCTS : \u8f93\u51fa\u786c\u4ef6\u6d41\u63a7(\u53ea\u80fd\u5728\u5177\u5b8c\u6574\u7ebf\u8def\u7684\u7f06\u7ebf\u4e0b\u5de5\u4f5c\uff0c\u53c2\u8003 Serial-HOWTO \u7b2c\u4e03\u8282)\n  CS8 : 8n1 (\u6bcf\u4e00\u5e278\u6bd4\u7279\u6570\u636e,\u65e0\u5947\u5076\u6821\u9a8c\u4f4d,1 \u6bd4\u7279\u505c\u6b62\u4f4d)\n  CLOCAL : \u672c\u5730\u8fde\u63a5\uff0c\u65e0\u8c03\u5236\u89e3\u8c03\u5668\u63a7\u5236\n  CREAD : \u5141\u8bb8\u63a5\u6536\u6570\u636e\n*\/\nnewtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;\n\n   \/*\nIGNPAR : ignore bytes with parity errors\nICRNL : map CR to NL (otherwise a CR input on the other computer will not \n       terminate input) otherwise make device raw (no other input processing)\nIGNPAR : \u5ffd\u7565\u5947\u5076\u6821\u9a8c\u51fa\u9519\u7684\u5b57\u8282\nICRNL : \u628a CR \u6620\u50cf\u6210 NL (\u5426\u5219\u4ece\u5176\u5b83\u673a\u5668\u4f20\u6765\u7684 CR \u65e0\u6cd5\u7ec8\u6b62\u8f93\u5165)\u6216\u8005\u5c31\u628a\u8bbe\u5907\u8bbe\n    \u4e3a raw \u72b6\u6001\uff08\u6ca1\u6709\u989d\u5916\u7684\u8f93\u5165\u5904\u7406\uff09\n*\/\nnewtio.c_iflag = 0;\n\n\/*\nRaw output. Raw \u6a21\u5f0f\u8f93\u51fa\n*\/\nnewtio.c_oflag = 0;\n\n\/*\nICANON : enable canonical input\ndisable all echo functionality, and don't send signals to calling program\nICANON : \u542f\u52a8 \u6807\u51c6\u8f93\u51fa\uff0c \u5173\u95ed\u6240\u6709\u56de\u663eecho \u529f\u80fd\uff0c\u4e0d\u5411\u7a0b\u5e8f\u53d1\u9001\u4fe1\u53f7\n*\/\nnewtio.c_lflag = ICANON;\n\n\/* \ninitialize all control characters \ndefault values can be found in \/usr\/include\/termios.h, and \nare given in the comments, but we don't need them here\n\u521d\u59cb\u5316\u6240\u6709\u7684\u63a7\u5236\u5b57\u7b26\uff0c \u9ed8\u8ba4\u503c\u53ef\u4ee5\u5728 \/usr\/include\/termios.h \u627e\u5230\uff0c\n\u5e76\u4e14\u505a\u4e86\u6ce8\u89e3\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u6211\u4eec\u5e76\u4e0d\u9700\u8981\u8003\u8651\u8fd9\u4e9b\n*\/\nnewtio.c_cc[VINTR] = 0; \/* Ctrl-c *\/ \nnewtio.c_cc[VQUIT] = 0; \/* Ctrl-\\ *\/\nnewtio.c_cc[VERASE] = 0; \/* del *\/\nnewtio.c_cc[VKILL] = 0; \/* @ *\/\nnewtio.c_cc[VEOF] = 4; \/* Ctrl-d *\/\nnewtio.c_cc[VTIME] = 0; \/* inter-character timer unused *\/\n                               \/* \u4e0d\u4f7f\u7528\u5b57\u7b26\u95f4\u7684\u8ba1\u65f6\u5668 *\/\nnewtio.c_cc[VMIN] = 1; \/* blocking read until 1 character arrives *\/\n                                  \/* \u963b\u585e\uff0c\u76f4\u5230\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u7b26 *\/\nnewtio.c_cc[VSWTC] = 0; \/* '\\0' *\/\nnewtio.c_cc[VSTART] = 0; \/* Ctrl-q *\/ \nnewtio.c_cc[VSTOP] = 0; \/* Ctrl-s *\/\nnewtio.c_cc[VSUSP] = 0; \/* Ctrl-z *\/\nnewtio.c_cc[VEOL] = 0; \/* '\\0' *\/\nnewtio.c_cc[VREPRINT] = 0; \/* Ctrl-r *\/\nnewtio.c_cc[VDISCARD] = 0; \/* Ctrl-u *\/\nnewtio.c_cc[VWERASE] = 0; \/* Ctrl-w *\/\nnewtio.c_cc[VLNEXT] = 0; \/* Ctrl-v *\/\nnewtio.c_cc[VEOL2] = 0; \/* '\\0' *\/\n\n\/* \nnow clean the modem line and activate the settings for the port\n\u6e05\u7a7a\u6570\u636e\u7ebf\uff0c\u542f\u52a8\u65b0\u7684\u4e32\u53e3\u8bbe\u7f6e\n*\/\ntcflush(fd, TCIFLUSH);\ntcsetattr(fd,TCSANOW,&amp;newtio);\n\n\/*\nterminal settings done, now handle input\nIn this example, inputting a 'z' at the beginning of a line will \nexit the program.\n\u7ec8\u7aef\u8bbe\u7f6e\u5b8c\u6210\uff0c\u73b0\u5728\u5c31\u53ef\u4ee5\u5904\u7406\u6570\u636e\u4e86\n\u5728\u672c\u7a0b\u5e8f\u4e2d\uff0c\u5728\u4e00\u884c\u7684\u5f00\u59cb\u8f93\u5165\u4e00\u4e2a 'z' \u4f1a\u7ec8\u6b62\u8be5\u7a0b\u5e8f \n*\/\nwhile (STOP==FALSE) { \/* loop until we have a terminating condition *\/\n        \/\/ \u5faa\u73af\u76f4\u5230\u6ee1\u8db3\u7ec8\u6b62\u6761\u4ef6\n\/* read blocks program execution until a line terminating character is \ninput, even if more than 255 chars are input. If the number\nof characters read is smaller than the number of chars available,\nsubsequent reads will return the remaining chars. res will be set\nto the actual number of characters actually read \n\u5373\u4f7f\u8f93\u5165\u8d85\u8fc7 255 \u4e2a\u5b57\u8282\uff0c\u8bfb\u53d6\u7684\u7a0b\u5e8f\u6bb5\u8fd8\u662f\u4f1a\u4e00\u76f4\u7b49\u5230\u884c\u7ed3\u675f\u7b26\u51fa\u73b0\u624d\u4f1a\u505c\u6b62\u3002\n   \u5982\u679c\u8bfb\u5230\u7684\u5b57\u7b26\u5c11\u4e8e\u5e94\u521a\u83b7\u5f97\u7684\u5b57\u7b26\u6570\uff0c\u5219\u5269\u4e0b\u7684\u5b57\u7b26\u4e32\u4f1a\u5728\u4e0b\u4e00\u6b21\u8bfb\u53d6\u65f6\u8bfb\u5230\u3002\nres \u7528\u6765\u83b7\u5f97\u6bcf\u6b21\u771f\u6b63\u8bfb\u5230\u7684\u5b57\u8282\u6570 \n*\/\nres = read(fd,buf,255); \nbuf[res]=0; \/* set end of string, so we can printf *\/\n                          \/\/ \u8bbe\u7f6e\u5b57\u7b26\u4e32\u7ed3\u675f\u7b26\uff0c\u4ece\u800c\u53ef\u4ee5\u987a\u5229\u4f7f\u7528 printf\nprintf(\":%s:%d\\n\", buf, res);\nif (buf[0]=='z') STOP=TRUE;\n}\n\/* restore the old port settings \u6062\u590d\u65e7\u7684\u4e32\u53e3\u8bbe\u7f6e *\/\ntcsetattr(fd,TCSANOW,&amp;oldtio);\n}<\/pre>\n<p>\u6700\u5f00\u59cb\u6211\u76f4\u63a5\u7528\u4e86cfmakeraw\u51fd\u6570\uff0c\u7136\u540e\u8bbe\u7f6e\u4e86\u4e00\u4e0bc_cflag\u4e3a115200 8N1\u3002\u901a\u8baf\u5565\u7684\u5168\u90fd\u6b63\u5e38\uff0c\u5c31\u662f\u6bcf\u6b21read\u56de\u6765\uff0c\\r\\n\u5728\u6570\u7ec4\u7684\u6700\u524d\u9762\uff0c\u800c\u4e0d\u662f\u6700\u540e\u2026\u2026\u800c\u4e14\u6bcf\u6b21\u53ea\u80fd\u8bfb32Bytes\u8fdb\u6765\u3002<\/p>\n<p>\u540e\u6765\u5bf9\u6bd4\u4e86\u4e00\u4e0b\u4ee3\u7801\uff0c\u53d1\u73b0termios\u7684c_lflag\u4e0a\u6ca1\u7ed9ICANON\uff0c\u5de5\u4f5c\u5728\u4e86\u539f\u59cb\u6a21\u5f0f\uff0c\u628a\u8fd9\u4e2a\u6807\u5fd7\u52a0\u4e0a\u540e\uff0c\u4ee5\u6807\u51c6\u6a21\u5f0f\u5de5\u4f5c\u540e\uff0c\u4e00\u6b21\u5c31\u80fd\u8bfbN\u591a\u5b57\u8282\uff0c\\r\\n\u4e5f\u5728\u6570\u7ec4\u7684\u6700\u540e\u9762\u4e86\u3002<\/p>\n<p>\u8fd9\u624d\u662f\u6211\u60f3\u8981\u7684\u561b\uff0c\u4e00\u4e2a\u7c7b\u4f3cgetline\u7684\u51fd\u6570xD\uff0c\u5c31\u8fd9\u73a9\u610f\u6298\u817e\u4e86\u4e00\u5929\u554a\uff08\u6454\uff01\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9065\u63a7\u7c97\u7c97\u7684\u53ef\u4ee5\u8dd1\u901a\u4e86\uff0c\u4eca\u5929\u5c31\u5f00\u59cb\u505a\u81ea\u52a8\u5bfc\u822a\u76f8\u5173\u7684\u4e1c\u897f\u4e86\u3002\u60f3\u4e86\u4e00\u4e0b\uff0c\u4e0d\u5982\u5148\u5904\u7406GPS\u3002\u6211\u7684GPS\u662f\u4e32\u53e3\u7684GPS\uff0c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/himmel.tech\/wp\/?p=141\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u5f00\u53d1\u65e5\u5fd7 170329\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-development-diary"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p83CFH-2h","_links":{"self":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=141"}],"version-history":[{"count":2,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=\/wp\/v2\/posts\/141\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himmel.tech\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}