serialport / node-serialport

Access serial ports with JavaScript. Linux, OSX and Windows. Welcome your robotic JavaScript overlords. Better yet, program them!

Home Page:https://serialport.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Abnormal reading of serial port data

Kimmyzhao opened this issue · comments

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