Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abnormal reading of serial port data #2764

Open
Kimmyzhao opened this issue Mar 22, 2024 · 0 comments
Open

Abnormal reading of serial port data #2764

Kimmyzhao opened this issue Mar 22, 2024 · 0 comments

Comments

@Kimmyzhao
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant