项目录结构
node项目
1) 安装serialport
npm install serialport
2)编写index.js
1 const SerialPort = require('serialport');
2 var senddata = [0x02];//串口索要发送的数据源
3 var port = new SerialPort('COM3');//连接串口COM3
4 port.on('open', function () {//开启串口
5 writeport();
6 })
7 setInterval(function () {
8 writeport();
9 }, 5000)
10 function writeport () {
11 port.write(senddata, function (err) {//node与串口通讯
12 if (err) {
13 return console.log('Error on write: ', err.message);
14 }
15 console.log('send: ' + senddata);
16 });
17 }
3)执行代码运行与串口通信
node index.js
目前前端做与串口通讯需要单独开启node服务,网页不可单独调用serialport