我正在使用带有socket.io的nodejs serialport,但是在初始化Serial Port后收到SetCommState错误。 javascript代码:

var http = require("http");
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var Serialport = require('serialport');

var server = http.createServer(function(request, response){
    var path = url.parse(request.url).pathname;

    switch(path){
        case '/':
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write('hello world');
            response.end();
            break;
        case '/socket.html':
            fs.readFile(__dirname + path, function(error, data){
                if (error){
                    response.writeHead(404);
                    response.write("opps this doesn't exist - 404");
                    response.end();
                }
                else{
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf8");
                    response.end();
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("opps this doesn't exist - 404");
            response.end();
            break;
    }
});

server.listen(8001);


io.listen(server);
var listener = io.listen(server);
listener.sockets.on('connection', function(socket){
    //send data to client
    SerialPort = Serialport.SerialPort;
    serialport = new SerialPort("COM12",{
    baudRate: 9600,
    // look for return and newline at the end of each data packet:
    parser: Serialport.parsers.readline("\n")
     });
    serialport.on('open', function(){
      console.log('Serial Port Opend');
      serialport.on('data', function(data){

            socket.emit('date', {'date': data, 'value': Math.random() });
        });
      });
    serialport.on('error', function(error) {
       console.log('The error: '+error);

    });


    socket.on('client_data', function(data){
        process.stdout.write(data.letter);
      });

});


在命令提示符下运行代码时,我得到的输出为:

The error: Error: SetCommState: Unknown error code 31


我该如何解决这个问题?

最佳答案

对于任何其他来此查找此错误的人,错误31表示Windows没有该产品的驱动程序。我使用它与朋友的Mac上的Arduino通信,因此当我切换到其他Arduino时,它可以工作。

https://www.lifewire.com/how-to-fix-code-31-errors-2623184

关于javascript - nodejs串行端口setCommState,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35977468/

10-09 21:59