Friday, 28 April 2017

Node.js - Can't sent data over serialPort

i would like to controll my arduino robot with node.js and joystick but serialport.Write doesnt work. I have try to use code without joystick and it works but only with one serial.write.


Is there a bug in my code?

Arduino code:

 String data = Serial.readString();
 Serial.println(data);
if(data=="2")
{
//motor1
}

Node.js

var hid = require('node-hid');
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort('COM3', {
    baudrate: 9600
});
    serialPort.on("open", function() {
    console.log('open');

    function sentData(data) {
        console.log(data);
        if (data == 0)
            setTimeout(function() {
                serialPort.write('1')
            }, 2000);
        else if (data > 999)
            setTimeout(function() {
                serialPort.write('2')
            }, 2000);
    }
    var device = new hid.HID(1133, 49685);
    device.on('data', function(buf) {

        var ch = buf.toString('hex').match(/.{1,2}/g).map(function(c) {
            return parseInt(c, 16);
        });

        var position = ((ch[2] & 0x0f) << 6) + ((ch[1] & 0xfc) >> 2);
        position = parseInt(position);
        sentData(position);

    });
});



via TheUrban

No comments:

Post a Comment