Skip to content

UART splits packets on every 0xFF byte. #11132

Closed
@hitecSmartHome

Description

@hitecSmartHome

Board

ESP32-Wrover-E

Device Description

  • PSRAM
  • FLASH
  • SPI

Hardware Configuration

Version

latest master (checkout manually)

IDE Name

VsCode

Operating System

Windows 10

Flash frequency

80

PSRAM enabled

yes

Upload speed

115200

Description

When the ESP32 receives a frame which has 0xFF in it, it will break the packet into multiple packets.
We measured with oscilloscope and there is no byte timeout, the bytes are continous.
So a single continous packet which has 3pcs 0xFF byte in it, it will be split into 3 different packets.

Sketch

#define MBUS_BAUD 115200
#define MBUS_RX 35
#define MBUS_TX 32
#define MBUS_RTS 33

#define MBUS_RX_TIMEOUT 1 // Can't set it higher if UART_MODE_RS485_HALF_DUPLEX
#define MAX_MBUS_DATA_LENGTH 256
#define MAX_RX_BUFFER_SIZE 256

void Modbus::init() {
    Serial1.setRxBufferSize(MAX_RX_BUFFER_SIZE);
    Serial1.setTxBufferSize(MAX_MBUS_DATA_LENGTH);
    Serial1.begin(MBUS_BAUD, SERIAL_8N1, MBUS_RX, MBUS_TX);
    Serial1.setPins(-1, -1, -1, MBUS_RTS);
    Serial1.setMode(UART_MODE_RS485_HALF_DUPLEX);
    Serial1.setRxTimeout(MBUS_RX_TIMEOUT);

    Serial1.onReceive(
        std::bind(&Modbus::handlePacket, this),
        PACKET_TRIGGER_ONLY_ON_TIMEOUT
    );
    Serial1.onReceiveError(
        std::bind(&Modbus::handleReceiveError, this, std::placeholders::_1)
    );
}

void Modbus::handlePacket() {
    int available = Serial1.available();

    uint8_t rawPacket[available] = {0};
    int read = Serial1.readBytes(rawPacket, available);

    utils.printRawPacket("UART packet: ", rawPacket, read);
}

void Utils::printRawPacket(const char* title, uint8_t* data, int length) {
    printf("%s: ", title);
    for (int i = 0; i < length; i++) {
        printf("0x%02x ", data[i]);
    }
    printf("\n");
}

Expected single packet:

0x04 0x03 0x40 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 0x00 0x00 0x1b 0x00 0x00 0x00 0x0b 0x00 0x01 0x00 0x1b 0x00 0x00 0x00 0x35 0x00 0x00 0x00 0x1c 0x00 0x00 0x00 0x1f 0x00 0x00 0x00 0x1f 0x00 0x00 0x00 0x27 0x00 0x00 0x00 0x25 0x00 0x00 0x00 0x83 0x00 0x00 0x00 0x58 0x00 0x00 0x94 0x20 0x00 0x04 0xff 0x3d 0xff 0xff 0xeb 0x35

Received packets instead.

0x04 0x03 0x40

0xff 0xff 0xff

0xff 0xff 0xff

0xff 0x00 0x00 0x00 0x1b 0x00 0x00 0x00 0x0b 0x00 0x01 0x00 0x1b 0x00 0x00 0x00 0x35 0x00 0x00 0x00 0x1c 0x00 0x00 0x00 0x1f 0x00 0x00 0x00 0x1f 0x00 0x00 0x00 0x27 0x00 0x00 0x00 0x25 0x00 0x00 0x00 0x83 0x00 0x00 0x00 0x58 0x00 0x00 0x94 0x20 0x00 0x04

0xff 0x3d 0xff 0xff 0xeb 0x35

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions