버티면할수있다
버티면할수있다

Reputation: 39

Node JS SerialPort How can i change parameters in port?

I'm using node.js SerialPort in electron for raspberry pi 4. https://serialport.io/docs/guide-cli#serialport-terminal

I wanna change parameters portname, dataBits, stopBits, parity etc... in port. But I couldn't find a way to do that.

Only one thing I know baudrate change is port.update() but I wanna know something else.

here is renderer.js

var portname,
    baudrate,
    dataBits,
    stopBits,
    parity;

const serialport = require('serialport');
const port = new serialport('COM4', {
  // baudRate: baudrate,
  baudRate: 9600,
  dataBits: 7,
  stopBits: 2,
  parity: "none",
  lock: false,
});


document.getElementById('openBtn').addEventListener('click', () => {
  // port.update is well done!
  // but how can I change something else?
  port.update({baudRate: baudrate}, err => {
    if(err) console.log('err!');
    else console.log(baudrate);
  })
})

// When index.html is loading this function is auto loading
// for change port's parameters
// this parameters are coming from setting.html
function getSerialInfo() {
  if (localStorage.getItem('serialInfo')) {
    var message = localStorage.getItem('serialInfo');
    var serialInfo = JSON.parse(message);
    portname = stringify(serialInfo.portname);
    baudrate = parseInt(serialInfo.baudrate);
    dataBits = serialInfo.databits
  }
}

Upvotes: 3

Views: 2321

Answers (2)

Ismael Soschinski
Ismael Soschinski

Reputation: 475

In the latest version of serialport the constructor has changed. It is now

new SerialPort({ path: '/dev/port', baudRate: 9600, dataBits: ... })

Upvotes: 0

버티면할수있다
버티면할수있다

Reputation: 39

I solved this problem using global declaration and generate new serialport when button is clicked Reference this blog https://it-jm.tistory.com/34

Upvotes: 1

Related Questions