{"id":38887,"date":"2023-09-30T14:13:20","date_gmt":"2023-09-30T06:13:20","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=38887"},"modified":"2023-09-30T14:13:20","modified_gmt":"2023-09-30T06:13:20","slug":"dfzcktxl","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/38887.html","title":{"rendered":"DotNet\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b"},"content":{"rendered":"
\u4e32\u53e3\u901a\u4fe1\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0e\u53e6\u4e00\u53f0\u8ba1\u7b97\u673a\u6216\u5176\u5b83\u5916\u90e8\u8bbe\u5907\u8fde\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u4f20\u8f93\u3002DotNet\u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecdDotNet\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n
DotNet\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\u662f\u4e00\u79cd\u5c01\u88c5\u4e86\u4e32\u53e3\u901a\u4fe1\u529f\u80fd\u7684\u7c7b\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u529f\u80fd\uff0c\u53ef\u4ee5\u8282\u7701\u5f00\u53d1\u65f6\u95f4\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u7c7b\uff0c\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u66f4\u65b9\u4fbf\u7684\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u529f\u80fd\uff0c\u6bd4\u5982SerialPort\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u4e32\u53e3\u7684\u5f00\u542f\u3001\u5173\u95ed\u3001\u53d1\u9001\u3001\u63a5\u6536\u7b49\u529f\u80fd\uff0c\u800cStream\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u6d41\u7684\u8bfb\u53d6\u548c\u5199\u5165\u7b49\u529f\u80fd\u3002<\/p>\n
\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e32\u53e3\u5bf9\u8c61\uff0c\u4f7f\u7528SerialPort\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e32\u53e3\u5bf9\u8c61\uff0c\u5e76\u4f20\u5165\u4e32\u53e3\u53f7\uff0c\u6ce2\u7279\u7387\uff0c\u6570\u636e\u4f4d\uff0c\u505c\u6b62\u4f4d\uff0c\u6821\u9a8c\u4f4d\u7b49\u53c2\u6570\uff1a<\/p>\n
SerialPort serialPort = new SerialPort(\"COM1\", 9600, Parity.None, 8, StopBits.One);<\/code><\/pre>\n 2. \u6253\u5f00\u4e32\u53e3<\/h3>\n
\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6253\u5f00\u4e32\u53e3\uff0c\u4f7f\u7528SerialPort\u7c7b\u7684Open()\u65b9\u6cd5\u53ef\u4ee5\u6253\u5f00\u4e32\u53e3\uff1a<\/p>\n
serialPort.Open();<\/code><\/pre>\n 3. \u53d1\u9001\u6570\u636e<\/h3>\n
\u4f7f\u7528SerialPort\u7c7b\u7684Write()\u65b9\u6cd5\u53ef\u4ee5\u53d1\u9001\u6570\u636e\uff1a<\/p>\n
serialPort.Write(\"Hello World!\");<\/code><\/pre>\n 4. \u63a5\u6536\u6570\u636e<\/h3>\n
\u4f7f\u7528SerialPort\u7c7b\u7684Read()\u65b9\u6cd5\u53ef\u4ee5\u63a5\u6536\u6570\u636e\uff1a<\/p>\n
string data = serialPort.Read();<\/code><\/pre>\n 5. \u5173\u95ed\u4e32\u53e3<\/h3>\n
\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5173\u95ed\u4e32\u53e3\uff0c\u4f7f\u7528SerialPort\u7c7b\u7684Close()\u65b9\u6cd5\u53ef\u4ee5\u5173\u95ed\u4e32\u53e3\uff1a<\/p>\n
serialPort.Close();<\/code><\/pre>\n\u4e09\u3001\u5e38\u89c1\u95ee\u9898<\/h2>\n 1. \u4e32\u53e3\u53f7\u4e0d\u5b58\u5728<\/h3>\n
\u5982\u679c\u60a8\u6307\u5b9a\u7684\u4e32\u53e3\u53f7\u4e0d\u5b58\u5728\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u6ca1\u6709\u5b89\u88c5\u5bf9\u5e94\u7684\u4e32\u53e3\u8bbe\u5907\uff0c\u6216\u8005\u662f\u60a8\u6240\u6307\u5b9a\u7684\u4e32\u53e3\u53f7\u4e0d\u6b63\u786e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SerialPort\u7c7b\u7684GetPortNames()\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u8ba1\u7b97\u673a\u4e0a\u53ef\u7528\u7684\u4e32\u53e3\u53f7\u5217\u8868\uff1a<\/p>\n
string[] ports = SerialPort.GetPortNames();<\/code><\/pre>\n 2. \u4e32\u53e3\u6253\u5f00\u5931\u8d25<\/h3>\n
\u5982\u679c\u60a8\u4f7f\u7528SerialPort\u7c7b\u7684Open()\u65b9\u6cd5\u6253\u5f00\u4e32\u53e3\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u7ecf\u6709\u5176\u4ed6\u7a0b\u5e8f\u5360\u7528\u4e86\u8be5\u4e32\u53e3\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SerialPort\u7c7b\u7684IsOpen\u5c5e\u6027\u6765\u68c0\u67e5\u4e32\u53e3\u662f\u5426\u5df2\u7ecf\u88ab\u6253\u5f00\uff1a<\/p>\n
if (serialPort.IsOpen) {<\/p>
\/\/\u4e32\u53e3\u5df2\u7ecf\u88ab\u6253\u5f00<\/p> }<\/code><\/pre>\n
3. \u6570\u636e\u63a5\u6536\u5931\u8d25<\/h3>\n
\u5982\u679c\u60a8\u4f7f\u7528SerialPort\u7c7b\u7684Read()\u65b9\u6cd5\u63a5\u6536\u6570\u636e\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\u6ca1\u6709\u5b89\u88c5\u5bf9\u5e94\u7684\u4e32\u53e3\u8bbe\u5907\uff0c\u6216\u8005\u662f\u60a8\u6240\u6307\u5b9a\u7684\u4e32\u53e3\u53f7\u4e0d\u6b63\u786e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SerialPort\u7c7b\u7684BytesToRead\u5c5e\u6027\u6765\u68c0\u67e5\u4e32\u53e3\u7f13\u51b2\u533a\u4e2d\u662f\u5426\u6709\u6570\u636e\uff1a<\/p>\n
if (serialPort.BytesToRead > 0) {<\/p>
\/\/\u4e32\u53e3\u7f13\u51b2\u533a\u4e2d\u6709\u6570\u636e<\/p> }<\/code><\/pre>\n
\u603b\u7ed3<\/h2>\n
\u672c\u6587\u4ecb\u7ecd\u4e86DotNet\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/strong>DotNet\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e32\u53e3\u901a\u4fe1\u529f\u80fd\uff0c\u53ef\u4ee5\u8282\u7701\u5f00\u53d1\u65f6\u95f4\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e32\u53e3\u901a\u4fe1\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0e\u53e6\u4e00\u53f0\u8ba1\u7b97\u673a\u6216\u5176\u5b83\u5916\u90e8\u8bbe\u5907\u8fde\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u4f20\u8f93\u3002DotNet\u6846\u67b6\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c01\u88c5\u4e32\u53e3\u901a\u4fe1\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u5b9e\u73b0\u4e32\u53e3<\/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-38887","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\/38887","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=38887"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/38887\/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=38887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=38887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=38887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}