2016-09-15 13 views
0

シリアルデバイスと通信するサーバーがあります。シリアルポートをコード内で直接設定すると、期待どおりに動作します。しかし、新しいシリアルポートオブジェクトを作成するための関数を介して設定を渡すと、パーサーは機能しません。動作しませんシリアルポートパーサーが機能で設定されていないと動作しません

// serial port initialization: 
var serialport = require('serialport'), // include the serialport library 
SerialPort = serialport.SerialPort, // make a local instance of serial 
portName = process.argv[2], // get the port name from the command line 
portConfig = { 
    baudrate : 9600, 
    databits : 8, 
    parity : 'none', 
    stopBits : 1, 
    buffersize : 4096, 
    parser : serialport.parsers.readline('\n') 
}; 
console.log(portConfig); 

// open the serial port: 
var myPort = new SerialPort(portName, portConfig); 
console.log(myPort); 

コード:

動作するコード

そして、関数に渡されたデータオブジェクト(これはハードのパラメータで、今のコード化されている私たちが仕事を知っています) :

function configureSerialPort(){ 
var portBundle = [{ 
    baudrate: 9600, 
    databits: 8, 
    parity: 'none', 
    stopBits: 1, 
    buffersize: 4096, 
}, 
{ 
    portName: 'com21' 
}]; 
socket.send(JSON.stringify(portBundle)); 
} 

ウェブサイト上のボタンからの入力を使用して設定されたポートmyPort 、ソケットの上に読み込まれている:

// this function runs if there's input from the client: 
socket.on('message', function (data) { 
    console.log("Client request received."); 
    console.log("SerialSend: " + data); 
    //check to see if the port is configured, if not, run configuration 
    if (typeof myPort === 'undefined') { 
     myPort = SetSerialPortConfig(data); 
     //prevents a write to the port with configuration data. 
     return false; 
    } 
    myPort.write(data); // send the data to the serial device 
}); 

私たちは、設定変数の任意のセットに渡すことができるようにWebページを必要とするので、私は関数法の作業をする必要があります。いずれにしてもconsole.log(myPort);を実行すると、ポートは同一であるように見えるので、パーサーが動作しない理由はわかりません。私は視覚的にデータがLED Tx & RxのRS-485コンバータのライトを介して送信されていることを視覚的に見ることができるので、デバイスのポートがデータを送受信していることがわかりますが、パーサーはEOLキャラクタ)、それはちょうど待っている。

答えて

0

同じようなことが発生しましたが、baudratestopbitsなどのすべての可能性のある整数型にparseInt()を使用しました。

portConfig = { 
    baudrate : parseInt(portBundle[0].baudrate), 
    databits : parseInt(portBundle[0].databits), 
    parity : portBundle[0].parity, 
    stopBits : parseInt(portBundle[0].stopBits), 
    buffersize : parseInt(portBundle[0].buffersize), 
関連する問題