node 读取串口

app.get('/serial',(req,res)=>{
    res.setHeader('Content-Type', 'text/palin; charset=utf-8');

    var SerialPort = require('serialport')
    //Opening a Port
    var serialPort = new SerialPort('COM2', {
        baudRate : 9600,
        autoOpen:false,
        encoding:"utf-8",
        dataBits: 8,


    })
    //连接串口
    serialPort.open(function (err) {
        console.log('IsOpen:',serialPort.isOpen)
    })
    //指令监听
    
    serialPort.on('data',function (data) {
        console.log('data received: '+data)
        if(data){
            compilehandle(data)
        }
    })

    //获取端口列表
    SerialPort.list().then(ports=>{
        console.log(ports);
        // ports.forEach(function(port) {
        //  console.log(port.comName);
        //  console.log(port.pnpId);
        //  console.log(port.manufacturer);
        // });
        res.send(ports)
    })
    
    function compilehandle(data){


    }

})

你可能感兴趣的:(node 读取串口)