{"id":38832,"date":"2023-10-03T17:33:34","date_gmt":"2023-10-03T09:33:34","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=38832"},"modified":"2023-10-03T17:33:34","modified_gmt":"2023-10-03T09:33:34","slug":"ldkckfhfsds","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/38832.html","title":{"rendered":"Linux\u6253\u5f00\u4e32\u53e3\u8fd4\u56deFD\u662f\u591a\u5c11\uff1f"},"content":{"rendered":"

  \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5916\u90e8\u8bbe\u5907\u63a5\u53e3\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u5916\u90e8\u8bbe\u5907\uff0c\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528open\u51fd\u6570\u5373\u53ef\u6253\u5f00\u4e32\u53e3\uff0c\u4f46\u662f\uff0c\u6253\u5f00\u4e32\u53e3\u4e4b\u540e\uff0c\u6211\u4eec\u53c8\u5982\u4f55\u77e5\u9053\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\u662f\u591a\u5c11\u5462\uff1f\u672c\u6587\u5c06\u4e3a\u60a8\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u7cfb\u7edf\u4e2d\u6253\u5f00\u4e32\u53e3\uff0c\u4ee5\u53ca\u5982\u4f55\u83b7\u53d6\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\u3002<\/p>\n

Linux\u4e32\u53e3\u64cd\u4f5c\uff1a\u6253\u5f00\u4e32\u53e3<\/h2>\n

  \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528open\u51fd\u6570\u5373\u53ef\u6253\u5f00\u4e32\u53e3\uff0c\u800c\u4e14\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6253\u5f00\u4efb\u610f\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u6253\u5f00\u4e32\u53e3\u3002<\/p>\n

  \u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u4e32\u53e3\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n

  1. \u6253\u5f00\u4e32\u53e3<\/h3>\n

  \u4f7f\u7528open\u51fd\u6570\u6253\u5f00\u4e32\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

fd = open(\"\/dev\/ttyS0\", O_RDWR | O_NOCTTY | O_NDELAY);<\/code><\/pre>\n

  \u8fd9\u91cc\u7684\/dev\/ttyS0\u662f\u4e32\u53e3\u7684\u8bbe\u5907\u6587\u4ef6\uff0cO_RDWR\u662fopen\u51fd\u6570\u7684\u53c2\u6570\uff0c\u8868\u793a\u4ee5\u8bfb\u5199\u65b9\u5f0f\u6253\u5f00\u4e32\u53e3\uff0cO_NOCTTY\u8868\u793a\u4e0d\u5c06\u4e32\u53e3\u8bbe\u7f6e\u4e3a\u63a7\u5236\u7ec8\u7aef\uff0cO_NDELAY\u8868\u793a\u4e0d\u5173\u5fc3\u4e32\u53e3\u7684\u72b6\u6001\uff0c\u53ea\u5173\u5fc3\u6570\u636e\u662f\u5426\u53ef\u8bfb\u3002<\/p>\n

  2. \u83b7\u53d6\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09<\/h3>\n

  \u6253\u5f00\u4e32\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528open\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u83b7\u53d6\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\uff0copen\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c31\u662f\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\uff0c\u6bd4\u5982\uff1a<\/p>\n

fd = open(\"\/dev\/ttyS0\", O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)<\/p>

  {<\/p>

   printf(\"open serial port failed!n\");<\/p>

   return -1;<\/p>

  }<\/p>

  else<\/p>

  {<\/p>

   printf(\"open serial port success!n\");<\/p>

   printf(\"fd = %dn\", fd);<\/p> }<\/code><\/pre>\n

  \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0copen\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c31\u662f\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u83b7\u53d6\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\u3002<\/p>\n

Linux\u4e32\u53e3\u64cd\u4f5c\uff1a\u8bbe\u7f6e\u4e32\u53e3\u5c5e\u6027<\/h2>\n

  \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u5728\u6253\u5f00\u4e32\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528tcsetattr\u51fd\u6570\u6765\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\uff0c\u6bd4\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u3001\u6821\u9a8c\u4f4d\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\uff1a<\/p>\n

\/\/\u8bbe\u7f6e\u4e32\u53e3\u5c5e\u6027

  struct termios options;<\/p>

  tcgetattr(fd, &options);<\/p>

  cfsetispeed(&options, B115200);<\/p>

  cfsetospeed(&options, B115200);<\/p>

  options.c_cflag |= (CLOCAL | CREAD);<\/p>

  options.c_cflag &= ~CSIZE;<\/p>

  options.c_cflag &= ~CRTSCTS;<\/p>

  options.c_cflag |= CS8;<\/p>

  options.c_cflag &= ~CSTOPB;<\/p>

  options.c_iflag |= IGNPAR;<\/p>

  options.c_oflag = 0;<\/p>

  options.c_lflag = 0;<\/p>

  tcflush(fd, TCIFLUSH);<\/p> tcsetattr(fd, TCSANOW, &options);<\/code><\/pre>\n

  \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528tcsetattr\u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\uff0c\u6bd4\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u3001\u6821\u9a8c\u4f4d\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u8981\uff0c\u6765\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\u3002<\/p>\n

\u603b\u7ed3<\/h2>\n

  \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528open\u51fd\u6570\u5373\u53ef\u6253\u5f00\u4e32\u53e3\uff0c\u800c\u4e14\uff0copen\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c31\u662f\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528open\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u83b7\u53d6\u4e32\u53e3\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08FD\uff09\u3002<\/strong>\u53e6\u5916\uff0c\u5728\u6253\u5f00\u4e32\u53e3\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528tcsetattr\u51fd\u6570\u6765\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\uff0c\u6bd4\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u3001\u6821\u9a8c\u4f4d\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u8981\uff0c\u6765\u8bbe\u7f6e\u4e32\u53e3\u7684\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5916\u90e8\u8bbe\u5907\u63a5\u53e3\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u5916\u90e8\u8bbe\u5907\uff0c\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4e32\u53e3\u7684\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528open\u51fd\u6570\u5373\u53ef\u6253\u5f00\u4e32\u53e3\uff0c\u4f46\u662f\uff0c\u6253\u5f00\u4e32\u53e3\u4e4b\u540e\uff0c<\/p>\n","protected":false},"author":1,"featured_media":4132,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[69],"class_list":["post-38832","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-cktx"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38832"}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=38832"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38832\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=38832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=38832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=38832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}