{"id":39127,"date":"2023-10-04T23:54:27","date_gmt":"2023-10-04T15:54:27","guid":{"rendered":"\/\/www.viralrail.com\/?p=39127"},"modified":"2023-10-04T23:54:27","modified_gmt":"2023-10-04T15:54:27","slug":"syskzmfs","status":"publish","type":"post","link":"\/\/www.viralrail.com\/blog\/39127.html","title":{"rendered":"\u4f7f\u7528serialport\u5e93\u600e\u4e48\u53d1\u9001"},"content":{"rendered":"
Serialport\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5728Node.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u64cd\u4f5c\u4e32\u884c\u7aef\u53e3\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u901a\u4fe1\uff0c\u4ece\u800c\u66f4\u597d\u5730\u63a7\u5236\u548c\u64cd\u7eb5\u8bbe\u5907\u3002\u672c\u6587\u5c06\u6df1\u5165\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528serialport\u5e93\u53d1\u9001\u6570\u636e\uff0c\u4ee5\u53ca\u5982\u4f55\u8ba9Node.js\u5e94\u7528\u7a0b\u5e8f\u66f4\u597d\u5730\u63a7\u5236\u5916\u90e8\u8bbe\u5907\u3002<\/p>\n
\u8981\u4f7f\u7528serialport\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528npm\u5b89\u88c5\uff1a<\/p>\n
“`<\/p>\n
npm install serialport<\/p>\n
“`<\/p>\n
\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5728Node.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528serialport\u5e93\u4e86\u3002<\/p>\n
\u5728\u4f7f\u7528serialport\u5e93\u53d1\u9001\u6570\u636e\u4e4b\u524d\uff0c\u9700\u8981\u5148\u67e5\u627e\u53ef\u7528\u7684\u4e32\u884c\u7aef\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u67e5\u627e\u53ef\u7528\u7684\u4e32\u884c\u7aef\u53e3\uff1a<\/p>\n
“`<\/p>\n
const SerialPort = require(‘serialport');<\/p>\n
SerialPort.list().then(ports => {<\/p>\n
ports.forEach(port => {<\/p>\n
console.log(port.comName);<\/p>\n
});<\/p>\n
});<\/p>\n
“`<\/p>\n
\u5f53\u67e5\u627e\u5230\u53ef\u7528\u7684\u4e32\u884c\u7aef\u53e3\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fde\u63a5\u4e32\u884c\u7aef\u53e3\uff1a<\/p>\n
“`<\/p>\n
const SerialPort = require(‘serialport');<\/p>\n
const port = new SerialPort(‘\/dev\/ttyUSB0', {<\/p>\n
baudRate: 9600<\/p>\n
});<\/p>\n
port.on(‘open', () => {<\/p>\n
console.log(‘\u4e32\u884c\u7aef\u53e3\u5df2\u8fde\u63a5');<\/p>\n
});<\/p>\n
“`<\/p>\n
\u5f53\u8fde\u63a5\u4e0a\u4e32\u884c\u7aef\u53e3\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53d1\u9001\u6570\u636e\uff1a<\/p>\n
“`<\/p>\n
port.write(‘Hello World!', (err) => {<\/p>\n
if (err) {<\/p>\n
return console.log(‘Error on write: ‘, err.message);<\/p>\n
}<\/p>\n
console.log(‘\u6570\u636e\u53d1\u9001\u6210\u529f');<\/p>\n
});<\/p>\n
“`<\/p>\n
\u672c\u6587\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528serialport\u5e93\u53d1\u9001\u6570\u636e\uff0c\u4ee5\u53ca\u5982\u4f55\u8ba9Node.js\u5e94\u7528\u7a0b\u5e8f\u66f4\u597d\u5730\u63a7\u5236\u5916\u90e8\u8bbe\u5907\u3002\u9996\u5148\uff0c\u4f7f\u7528npm\u5b89\u88c5serialport\u5e93\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u67e5\u627e\u53ef\u7528\u7684\u4e32\u884c\u7aef\u53e3\uff0c\u63a5\u7740\u8fde\u63a5\u4e32\u884c\u7aef\u53e3\uff0c\u6700\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u53d1\u9001\u6570\u636e\u4e86\u3002\u4f7f\u7528serialport\u5e93\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u901a\u4fe1\uff0c\u4ece\u800c\u66f4\u597d\u5730\u63a7\u5236\u548c\u64cd\u7eb5\u8bbe\u5907\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Serialport\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u5728Node.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u64cd\u4f5c\u4e32\u884c\u7aef\u53e3\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5b9e\u73b0\u4e0e\u5916\u90e8\u8bbe\u5907\u7684\u901a\u4fe1\uff0c\u4ece\u800c\u66f4\u597d\u5730\u63a7\u5236\u548c\u64cd\u7eb5\u8bbe\u5907\u3002\u672c\u6587\u5c06\u6df1\u5165\u8ba8\u8bba\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":[305],"class_list":["post-39127","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-dzjs"],"_links":{"self":[{"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/posts\/39127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/comments?post=39127"}],"version-history":[{"count":0,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/posts\/39127\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/media\/4132"}],"wp:attachment":[{"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/media?parent=39127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/categories?post=39127"},{"taxonomy":"post_tag","embeddable":true,"href":"\/\/www.viralrail.com\/wp-json\/wp\/v2\/tags?post=39127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}