简体   繁体   中英

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 } }

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM