シリアルデバイスと通信するサーバーがあります。シリアルポートをコード内で直接設定すると、期待どおりに動作します。しかし、新しいシリアルポートオブジェクトを作成するための関数を介して設定を渡すと、パーサーは機能しません。動作しませんシリアルポートパーサーが機能で設定されていないと動作しません
// 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キャラクタ)、それはちょうど待っている。