From a7ee6054b5b4cfe0dc9011d76ee1f119cf4b8fa2 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Fri, 17 Jan 2025 22:26:46 +0100 Subject: [PATCH] Berry 'serial.read()' read only 'n' bytes (#22835) --- CHANGELOG.md | 1 + tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_serial.ino | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dee27a08..8c20391b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. - Command `FileLog 10..14` to enable logging to filesystem using up to 16 log files of 100kB (`#define FILE_LOG_SIZE 100`) - I2S Opus stream and file support for opus/aac (#22795) - I2S command I2sLoop (#22807) +- Berry `serial.read()` read only `n` bytes ### Breaking Changed diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_serial.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_serial.ino index e0efe5ae0..7b0809217 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_serial.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_serial.ino @@ -143,10 +143,18 @@ extern "C" { int32_t b_serial_read(struct bvm *vm) { be_getmember(vm, 1, ".p"); TasmotaSerial * ser = (TasmotaSerial *) be_tocomptr(vm, -1); + int32_t max_lex = -1; // -1 means unlimited + int32_t argc = be_top(vm); // Get the number of arguments + if (argc >= 2 && be_isint(vm, 2)) { + max_lex = be_toint(vm, 2); + } if (ser) { int32_t len = ser->available(); if (len < 0) { len = 0; } if (len > 0) { + if (max_lex >= 0 && len > max_lex) { + len = max_lex; + } // read bytes on stack char * rx_buf = new char[len]; len = ser->read(rx_buf, len);