Skip to content

Hardware serial sometimes doesn't send every data. #6642

Closed
@zekageri

Description

@zekageri

Board

esp32-wrover-e

Device Description

Custom board, using ESP32-Wrover-E ( 16mb flash 8mb external ram )

Hardware Configuration

Ethernet -> ETH_LAN_8720 chip

RTC Module -> DS3231 chip ( i2c )

Transistor for restarting the device -> 2n2222 or similar. ( GPIO 12 )

Two hardware serial UART:

  • Debug Serial 0 : RX: GPIO 3 | TX: GPIO 1
  • Custom Modbus Serial 1 : RX: GPIO 34 | TX: GPIO 15

Version

latest master (checkout manually)

IDE Name

Platform IO

Operating System

Windows 10

Flash frequency

80Mhz

PSRAM enabled

yes

Upload speed

115200

Description

Serial1 sometimes does not send out all the bytes and i got a frame timeout in my communication because the modules that my esp communicates doesnt understand the broken message. Happens mostly on web page load or file upload via HTTP.

Communication runs on an available core in a separate task with 115200 baud rate, tries to write as soon as possible.

Sketch

#define MBUS_BAUD 115200
#define MBUS_RX 34  //   6.  PIN
#define MBUS_TX 15  //   23. PIN

void mBusSetup(){
    pinSetup();
    Serial1.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
}

void mSystem::pinSetup(){
    pinMode(MBUS_TX_EN, OUTPUT);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::startWrite(){
    mBusLock();
    //Serial1.flush();
    digitalWrite(MBUS_TX_EN, HIGH);
}

void mSystem::endWrite(){
    Serial1.flush();          // does not help
    //delayMicroseconds(200); // Does not help. Dont want magic delays
    digitalWrite(MBUS_TX_EN, LOW); // TX_ENABLE pin pulled low too soon i assume.
    mBusUnLock();
}

void mSystem::rawWrite(uint8_t * data, int length){
    int crc16 = getCRC_16(data, length);
    startWrite();
    Serial1.write( data, length );
    Serial1.write( lowByte(crc16) );
    Serial1.write( highByte(crc16) );
    endWrite();
}

#define MBUS_DISCOVER_LISTEN_TIMEOUT 150 // Modules will respond back in maximum of 15ms.

void writeReadDevice(int index){
    // This function handles which device address to write and assmebles the writable data and calls
    // rawWrite. If rawWrite returns, this function waits for the response data.
    uint8_t exampleData[] = {0xFF, 0x03, 0x00, 0x06, 0x00, 0x03};
    uint8_t responseData[20]; // Maximum response data is 10-13.
    rawWrite(exampleData, sizeof(exampleData));

    // wait for the response data. The modules will respond back if the data is understandable.
    long startMs = millis();
    while( !Serial1.available() ){
        vTaskDelay(1);
        if( millis() - startMs >= MBUS_DISCOVER_LISTEN_TIMEOUT ){
            Serial.println("FRAME TIMEOUT!");
            return;
        }
    }

    int respDataCount = 0;
    while( Serial1.available() > 0 ){
        responseData[respDataCount] = Serial1.read();
        respDataCount++;
    }
}

void commLoop(){
    writeReadDevice(deviceIndex);
    deviceIndex++; // Used for communicating with different modules
    if( deviceIndex > deviceCounter ){deviceIndex = 0;}
}

void mBusLoopTask(void* parameter){
    mBusSetup();
    for(;;){
        commLoop();
        vTaskDelay(3); // The problem occours even faster if this delay is smaller.
    }
}

Debug Message

No debug message just broken data on Serial.

Other Steps to Reproduce

Try to write frequently on serial1.

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

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions