nodejs串口通信(基础)
let bodyParser = require('body-parser');
let express = require("express"),
app = express();
let port = 8686;
app.listen(port, () => {
console.log(`server is success,listen on ${port}`);
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static('./static'));
let SerialPort = require('serialport');
let serialPort = new SerialPort('COM5', {
baudRate : 115200,
autoOpen:false
});
app.post("/sendMsg",async (req,res)=>{
let {msg=''} = req.body;
serialPort.write(msg);
});
serialPort.open(function (err) {
console.log('IsOpen:',serialPort.isOpen)
console.log('err:',err)
})
serialPort.on('data',async function (data) {
console.log('data received: '+data)
})
serialPort.on('error',function (error) {
console.log('error: '+error)
})