使用节点js从客户端PC Web浏览器与串行端口通信

时间:2018-07-19 14:19:37

标签: node.js serial-port

我有一个用户要求来标识客户端Web浏览器的串行端口。在VS 2017中使用Node js。

app.js代码:

'use strict';
var debug = require('debug');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

app.set('port', process.env.PORT || 3000);

index.pug代码:

extends layout

block content
  h1= title
  p Welcome to #{title}


    var express = require('express'),
    server = require('http').createServer(app);
    io = require('socket.io').listen(server);
    serialPort = require('serialport');

    var portName  = '/ACPI/PNP0501/1';

var readData = ''; 

var sp = new serialPort(portName, {
    baudRate : 115200,
    dataBits : 8,
    parity : 'none',
    stopBits : 1,
}); 

sp.open(function (error) {
    if ( error ) {
        console.log('failed to open: ' + error);
    } else {
        console.log('serial port opened');
        serialport_opened = true;

        // get data from connected device via serial port
        serialPort.on('data', function(data) {
            // get buffered data and parse it to an utf-8 string
            data = data.toString('utf-8');
            // you could for example, send this data now to the the client via socket.io
            // io.emit('emit_data', data);
        });

        serialPort.on('error', function(data) {
            console.log('Error: ' + data);
        })
    }
});

当我运行index.pug时,我收到以下意外的文本错误消息,请任何人帮助:

C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ views \ index.pug:22:1 20 |平价:“无”,21 | stopBits:1,> 22 | }); -------- ^ 23 | 24 | sp.open(function(error){25 | if(error){意外文本“});” 错误:C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ views \ index.pug:22:1     20 |平价:“无”,     21 | stopBits:1,

  

22 | });   -------- ^       23 |       24 | sp.open(函数(错误){       25 |如果(错误){

意外的文字“}); ”     在makeError(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-error \ index.js:32:13)     在Lexer.error(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:59:15)     在Lexer.fail(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:1441:10)     在Lexer.advance(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:1501:15)     在Lexer.callLexerFunction(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:1456:23)     在Lexer.getTokens(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:1512:12)     在lex(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-lexer \ index.js:12:42)     在Object.lex(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug \ lib \ index.js:99:27)     在Function.loadString [作为字符串]处(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug-load \ index.js:44:24)     在compileBody(C:\ Users \ IWPC002 \ source \ repos \ ExpressApp1 \ ExpressApp1 \ node_modules \ pug \ lib \ index.js:86:18)

0 个答案:

没有答案