0

I'm trying to get data from a serial port. I've got a working python script, but I need the code in node.js.

Here ist the python script:

import serial
import time
import io

ser = serial.Serial('COM3')

ser.timeout=1

while(True):

try:
    ser.write(b'M00');
    ser.write(b'p');
    s0 = ser.read(26)

    print(s0)

except KeyboardInterrupt:
    ser.close()
    break

except:
    pass

Now I tried to rewrite this code in javascript and came up with this code:

const SerialPort = require('serialport');

SerialPort.list((err, ports) => {
  console.log(ports)                
})

var myPort = new SerialPort('COM3', {
   baudRate:9600
})

myPort.on('open', onOpen);
myPort.on('data', onData);
myPort.write('M01');

function onOpen(){
  console.log("Open connection");
}

function onData(data){
  console.log("on Data " + data);
}

while(true){
  console.log(myPort.read())
}

Unfortunatley I can't get it to work. My guess is, that maybe I have to pass binary code to my port. Do you know a sollution? thanks

1 Answer 1

1

Ok, I found my mistake. Just forgot to also write the "p" to my port.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.