Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Can't send data over serialPort

I would like to control my Arduino robot with Node.js and a joystick, but serialport.write doesn't send any data to Arduino. I have tried to use code without a 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);
  });
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The arduino code should look like this:

String data = '';
while(Serial.available() > 0) {
    data = data + Serial.read();
}
Serial.println(data);
if(data == "2") {
     //code
}

But, sorry, I can't see if there is problem in you node.js

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!