Skip to content

Instantly share code, notes, and snippets.

@wsd1
Created March 22, 2024 08:36
Show Gist options
  • Save wsd1/bf4ff3b61a07eff6a493942608f3835f to your computer and use it in GitHub Desktop.
Save wsd1/bf4ff3b61a07eff6a493942608f3835f to your computer and use it in GitHub Desktop.
//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