{"id":38830,"date":"2023-10-03T16:56:56","date_gmt":"2023-10-03T08:56:56","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=38830"},"modified":"2023-10-03T16:56:56","modified_gmt":"2023-10-03T08:56:56","slug":"sylsxck","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/38830.html","title":{"rendered":"\u4f7f\u7528Linux\u5b9e\u73b0\u4e32\u53e3"},"content":{"rendered":"

  Linux\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u3002\u4e32\u53e3\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u5916\u90e8\u8bbe\u5907\uff0c\u4f8b\u5982\u6253\u5370\u673a\u3001\u7f51\u7edc\u8bbe\u5907\u7b49\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u6570\u636e\u4ea4\u6362\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Linux\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u3002<\/p>\n

\u4e32\u53e3\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n

  \u4e32\u53e3\uff08Serial Port\uff09\u662f\u4e00\u79cd\u6570\u636e\u4f20\u8f93\u7684\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u5916\u90e8\u8bbe\u5907\uff0c\u4f8b\u5982\u6253\u5370\u673a\u3001\u7f51\u7edc\u8bbe\u5907\u7b49\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u6570\u636e\u4ea4\u6362\u3002\u4e32\u53e3\u901a\u4fe1\u5206\u4e3a\u4e24\u79cd\uff1a\u4e32\u884c\u901a\u4fe1\u548c\u5e76\u884c\u901a\u4fe1\u3002\u4e32\u884c\u901a\u4fe1\u662f\u6307\u4e00\u6b21\u53ea\u4f20\u8f93\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\uff0c\u800c\u5e76\u884c\u901a\u4fe1\u5219\u662f\u6307\u4e00\u6b21\u53ef\u4ee5\u4f20\u8f93\u591a\u4e2a\u5b57\u8282\u7684\u6570\u636e\u3002<\/p>\n

  \u4e32\u53e3\u7684\u786c\u4ef6\u8fde\u63a5<\/h3>\n

  \u4e32\u53e3\u7684\u786c\u4ef6\u8fde\u63a5\u5305\u62ec\u4e24\u90e8\u5206\uff1a\u4e00\u662f\u7ebf\u7f06\uff0c\u4e8c\u662f\u5916\u90e8\u8bbe\u5907\u3002\u7ebf\u7f06\u662f\u7528\u6765\u8fde\u63a5\u7535\u8111\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\uff0c\u5b83\u7684\u7c7b\u578b\u53ef\u4ee5\u662fRS-232\u3001RS-422\u6216\u8005RS-485\u3002\u5916\u90e8\u8bbe\u5907\u662f\u6307\u8981\u901a\u8fc7\u4e32\u53e3\u8fde\u63a5\u7684\u5916\u90e8\u8bbe\u5907\uff0c\u4f8b\u5982\u6253\u5370\u673a\u3001\u7f51\u7edc\u8bbe\u5907\u7b49\u3002<\/p>\n

  \u4e32\u53e3\u7684\u8f6f\u4ef6\u8bbe\u7f6e<\/h3>\n

  \u4e32\u53e3\u7684\u8f6f\u4ef6\u8bbe\u7f6e\u5305\u62ec\u4e24\u90e8\u5206\uff1a\u4e00\u662f\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4e8c\u662f\u4e32\u53e3\u7a0b\u5e8f\u3002\u9a71\u52a8\u7a0b\u5e8f\u662f\u6307\u7528\u6765\u63a7\u5236\u4e32\u53e3\u7684\u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u63a7\u5236\u4e32\u53e3\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u7b49\u3002\u4e32\u53e3\u7a0b\u5e8f\u662f\u6307\u7528\u6765\u7f16\u5199\u4e32\u53e3\u901a\u4fe1\u7a0b\u5e8f\u7684\u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n

\u4f7f\u7528Linux\u5b9e\u73b0\u4e32\u53e3<\/h2>\n

  \u4f7f\u7528Linux\u5b9e\u73b0\u4e32\u53e3\u9700\u8981\u5148\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f\uff0c\u7136\u540e\u518d\u7f16\u5199\u4e32\u53e3\u7a0b\u5e8f\u3002<\/p>\n

  \u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f<\/h3>\n

  Linux\u7cfb\u7edf\u81ea\u5e26\u7684\u4e32\u53e3\u9a71\u52a8\u7a0b\u5e8f\u4e3atty\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u4e32\u53e3\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u7b49\u3002\u8981\u5b89\u88c5tty\u9a71\u52a8\u7a0b\u5e8f\uff0c\u9700\u8981\u6253\u5f00\u7ec8\u7aef\uff0c\u7136\u540e\u8f93\u5165\uff1a<\/p>\n

  sudo apt-get install tty<\/p>\n

  \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\uff1a<\/p>\n

  ls \/dev\/tty*<\/p>\n

  \u7f16\u5199\u4e32\u53e3\u7a0b\u5e8f<\/h3>\n

  \u8981\u7f16\u5199\u4e32\u53e3\u7a0b\u5e8f\uff0c\u9700\u8981\u4f7f\u7528C\u8bed\u8a00\u6216\u8005\u5176\u4ed6\u8bed\u8a00\uff0c\u4f8b\u5982Python\u3001Java\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u7b80\u5355\u7684\u4e32\u53e3\u7a0b\u5e8f\uff1a<\/p>\n

#include 

  #include <\/p>

  #include <\/p>

  int main(int argc, char *argv[])<\/p>

  {<\/p>

   int fd;<\/p>

   struct termios options;<\/p>

   \/\/ \u6253\u5f00\u4e32\u53e3<\/p>

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

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

   {<\/p>

  perror(\"open serial port error!n\");<\/p>

  return -1;<\/p>

   }<\/p>

   \/\/ \u8bbe\u7f6e\u4e32\u53e3\u53c2\u6570<\/p>

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

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

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

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

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

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

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

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

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

   \/\/ \u5173\u95ed\u4e32\u53e3<\/p>

   close(fd);<\/p>

   return 0;<\/p>

  }<\/p><\/code><\/pre>\n

  \u4e0a\u9762\u7684\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4e32\u53e3\u7a0b\u5e8f\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u3001\u8bbe\u7f6e\u548c\u5173\u95ed\u4e32\u53e3\u3002<\/p>\n

\u603b\u7ed3<\/h2>\n

  \u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Linux\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\uff0c\u5b83\u9700\u8981\u5148\u5b89\u88c5\u9a71\u52a8\u7a0b\u5e8f\uff0c\u7136\u540e\u518d\u7f16\u5199\u4e32\u53e3\u7a0b\u5e8f\u3002<\/strong>Linux\u7cfb\u7edf\u81ea\u5e26\u7684\u4e32\u53e3\u9a71\u52a8\u7a0b\u5e8f\u4e3atty\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u4e32\u53e3\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u7b49\u3002\u8981\u7f16\u5199\u4e32\u53e3\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528C\u8bed\u8a00\u6216\u8005\u5176\u4ed6\u8bed\u8a00\uff0c\u4f8b\u5982Python\u3001Java\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Linux\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u3002\u4e32\u53e3\u53ef\u4ee5\u7528\u6765\u8fde\u63a5\u5916\u90e8\u8bbe\u5907\uff0c\u4f8b\u5982\u6253\u5370\u673a\u3001\u7f51\u7edc\u8bbe\u5907\u7b49\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u6570\u636e\u4ea4\u6362\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f<\/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-38830","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\/38830","targetHints":{"allow":["GET"]}}],"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=38830"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38830\/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=38830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=38830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=38830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}