Created
March 22, 2024 08:36
-
-
Save wsd1/bf4ff3b61a07eff6a493942608f3835f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//npm i serialport | |
const SerialPort = require('serialport').SerialPort; | |
var port = new SerialPort({ | |
path: '/dev/ttyUSB0', | |
baudRate: 9600, //波特率 | |
dataBits: 8, //数据位 | |
parity: 'none', //奇偶校验 | |
stopBits: 1, //停止位 | |
flowControl: false | |
}, false); // this is the openImmediately flag [default is true] | |
//指令监听 | |
port.on('open', function () { | |
// port.write('main screen turn on ', function (err) { | |
// if (err) { | |
// return console.log('Error on write: ', err.message); | |
// } | |
// console.log('send success'); | |
// }); | |
port.on('data', function (data) { | |
console.log('data received: ' + data); | |
}); | |
}); | |
//打开错误将会发出一个错误事件 | |
port.on('error', function (err) { | |
console.log('Error: ', err.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment