You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js SerialPort Readline解析器为何无法正常工作?

Fixing Truncated/Inconsistent Serial Data Read with Node.js SerialPort

Hey there, let's sort out this serial reading issue you're facing! The problem you're seeing—where some data gets truncated (like that final <Buffer 52...>) and the output feels inconsistent—happens because the SerialPort module's default data event fires whenever it receives a chunk of bytes, not necessarily a complete line of data from your Arduino.

When your Arduino sends Serial.print('R'); Serial.println(1);, it's sending the string R1\r\n (the println adds a carriage return \r and newline \n). But SerialPort might split this into multiple chunks if the data arrives in smaller batches, or bundle multiple lines into one chunk. That's why you're seeing partial buffers and raw byte outputs instead of clean, complete lines.

The Easy Fix: Use the Readline Parser

The SerialPort library has a built-in Readline parser that handles line-based data perfectly. It automatically splits incoming data using your specified delimiter (in this case, \r\n), so you only get complete lines in your data event.

Here's how to set it up:

  1. First, install the required packages:
npm install serialport @serialport/parser-readline
  1. Then write your Node.js code like this:
const { SerialPort } = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline');

// Replace with your Arduino's serial port (e.g., COM3 on Windows, /dev/tty.usbmodem14101 on macOS)
const port = new SerialPort({ path: '/dev/tty.usbmodem14101', baudRate: 9600 });

// Create the parser, using \r\n as the delimiter (matches Arduino's println)
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }));

// Listen for complete lines of data
parser.on('data', (line) => {
  console.log('Received complete line:', line);
  // You can now process the line directly—e.g., check if it's 'R1' or 'R2'
  if (line === 'R1') {
    console.log('Got command R1');
  } else if (line === 'R2') {
    console.log('Got command R2');
  }
});

// Handle port errors
port.on('error', (err) => {
  console.error('Serial port error:', err.message);
});

Why This Works

The ReadlineParser buffers incoming bytes until it hits the \r\n delimiter, then emits the entire line as a string. No more raw buffers, no more truncated data—just clean, complete lines matching what your Arduino sends.

Manual Buffer Handling (If You Prefer)

If you don't want to use the parser, you can manually buffer incoming data and split it yourself. Here's a quick example:

const { SerialPort } = require('serialport');
const port = new SerialPort({ path: '/dev/tty.usbmodem14101', baudRate: 9600 });

let buffer = '';

port.on('data', (data) => {
  // Convert the buffer to a string and append to our buffer
  buffer += data.toString();
  // Split the buffer into lines using \r\n
  const lines = buffer.split('\r\n');
  // The last element might be an incomplete line, so keep it in the buffer
  buffer = lines.pop();
  // Process each complete line
  lines.forEach(line => {
    console.log('Received line:', line);
    // Handle R1/R2 logic here
  });
});

This does the same job as the parser, but requires you to manage the buffer yourself. The official parser is more robust and easier to maintain, though.

Quick Check for Your Arduino Code

Just to confirm: your Arduino code uses Serial.println(), which automatically appends \r\n—that's exactly the delimiter we're using in the parser, so it should match perfectly. If you ever switch to a different delimiter (like just \n), make sure to update the parser's delimiter option accordingly.

内容的提问来源于stack exchange,提问作者George Profenza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:50:08