Abnormal reading of serial port data
Kimmyzhao opened this issue · comments
Kimmyzhao commented
SerialPort Version
12.0.0
Node Version
v20.10.0
Electron Version
21.4.4
Platform
MacOS 14.4
Architecture
x64
Hardware or chipset of serialport
No response
What steps will reproduce the bug?
When I read data through the serial port, the data I read from the serial port is abnormal。
'use strict';
const {Controller} = require('ee-core');
const {SerialPort} = require('serialport');
/**
* 串口
* @class
*/
class SerialPortController extends Controller {
constructor(ctx) {
super(ctx);
}
/**
* 获取串口
*/
async getSerialPortList() {
//主线程获取串口列表
return await SerialPort.list();
}
async readSerialPortData(args, event) {
const {path, baudRate} = args;
console.log('path is %s, baudRate is %d', path, baudRate)
const port = new SerialPort({
path: path,
baudRate: baudRate,
dataBits: 8, // 数据位
parity: 'none', // 奇偶校验
stopBits: 1, // 停止位
flowControl: true,
autoOpen: true
}, function (err) {
if (err) {
return console.log('Error: ', err.message)
}
})
port.on('data', data => {
console.log('Data:', data)
event.reply(`controller.serialPort.getSerialPortData`, {data});
})
}
}
SerialPortController.toString = () => '[class SerialPortController]';
module.exports = SerialPortController;
What happens?
when i use port.on('data', data =>{console.log(data)})
, the output is
Data: <Buffer 53 54 2c>
Data: <Buffer 4e 54 2c 2b 20 20 31 33 35 2e 31 20 67 0d 0a>
Actually, the expected output is
Data: <Buffer 53 54 2c 4e 54 2c 2b 20 20 31 33 35 2e 31 20 67 0d 0a>
What should have happened?
When I use a serial port assistant tool to read electronic scale data, the output is shown in the figure
How can I adjust the code to read the same content as the serial assistant
Additional information
No response