Skip to content

Add configurable transmit delay via set_transmit_delay() - #31

Open
brocci wants to merge 1 commit into
madleech:masterfrom
brocci:add-transmit-delay-setter
Open

Add configurable transmit delay via set_transmit_delay()#31
brocci wants to merge 1 commit into
madleech:masterfrom
brocci:add-transmit-delay-setter

Conversation

@brocci

@brocci brocci commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Summary

While the v1.7.x library now supports INIT messages and allow for an init handler, it lacks a way to set the transmit delay.

NMRA CMRInet (LCS-9.10.1) specifies a per-character transmission delay derived from the dH/dL bytes in the INIT message. Each unit represents 10 microseconds. Modern hosts set dH/dL to zero; non-zero values are for legacy compatibility with older Host computers.

The library hardcodes delayMicroseconds(50) in transmit() as an RS-485 turnaround delay, regardless of the commanded dH/dL value.

This PR adds a set_transmit_delay() method and a compile-time default via #define TRANSMIT_DELAY_US, allowing sketches to configure the turnaround delay. Sketches can parse dH/dL in an init handler and apply the delay automatically for legacy host compatibility.

Changes

Compile-time default (CMRI.h)

#ifndef TRANSMIT_DELAY_US
// RS-485 turnaround delay before transmitting (microseconds)
// Set to 0 if DE timing is handled externally
#define TRANSMIT_DELAY_US 50
#endif

Runtime override (CMRI.h, CMRI.cpp)

void set_transmit_delay(unsigned int delay_us);

New public method stores the delay in _transmit_delay_us. Applied in transmit() before sending the GET frame:

void CMRI::transmit()
{
    delayMicroseconds(_transmit_delay_us);
    _serial.write(255);
    // ...
}

INIT handler example (examples/init_handler_with_delay/)

Demonstrates parsing dH/dL from the INIT payload and applying the delay:

void on_init(const uint8_t *data, int len)
{
    if (len >= 3)
    {
        unsigned int delay_us = (data[1] * 256 + data[2]) * 10;
        cmri.set_transmit_delay(delay_us);
    }
}

Unit test

Added test_set_transmit_delay to verify the setter compiles and transmit() still produces a valid frame.

Backward compatibility

  • Default TRANSMIT_DELAY_US is 50, matching previous hardcoded behavior
  • set_transmit_delay() is optional -- existing sketches work unchanged
  • Setting delay to 0 disables the wait (for full-duplex or external DE control)

@brocci
brocci force-pushed the add-transmit-delay-setter branch from f98fceb to 45c8e70 Compare August 15, 2026 17:37
@brocci
brocci force-pushed the add-transmit-delay-setter branch from 45c8e70 to 4e23f99 Compare August 15, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant