Reputation: 1089
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
Upvotes: 0
Views: 454
Reputation: 1089
Ok, I found my mistake. Just forgot to also write the "p" to my port.
Upvotes: 1