Skip to content

File syn_modbus_tcp.h

FileList > proto > syn_modbus_tcp.h

Go to the source code of this file

Modbus TCP ADU (MBAP) framing layer. More...

  • #include "../common/syn_defs.h"
  • #include "syn_modbus.h"
  • #include "syn_modbus_master.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_MBAP_Header
Modbus TCP MBAP Header structure.

Public Functions

Type Name
bool syn_mbap_decode_header (const uint8_t * buf, SYN_MBAP_Header * hdr)
Decode an MBAP header from a byte buffer.
void syn_mbap_encode_header (const SYN_MBAP_Header * hdr, uint8_t * buf)
Encode an MBAP header into a byte buffer.
SYN_Status syn_modbus_tcp_build_client_adu (uint16_t transaction_id, uint8_t unit_id, const uint8_t * pdu, uint16_t pdu_len, uint8_t * req_adu, uint16_t req_max, uint16_t * req_len)
Build a Modbus TCP Master request ADU.
bool syn_modbus_tcp_process_slave (SYN_Modbus * mb, const uint8_t * req_adu, uint16_t req_len, uint8_t * resp_adu, uint16_t resp_max, uint16_t * resp_len)
Process an incoming Modbus TCP ADU frame on a Modbus Slave instance.

Macros

Type Name
define SYN_MBAP_HEADER_LEN 7
define SYN_MB_TCP_MAX_ADU 260

Detailed Description

Implements Modbus TCP MBAP (Modbus Application Protocol) header framing over TCP transport sockets (syn_transport_tcp).

MBAP Header Structure (7 bytes): * Transaction ID (2 bytes, big-endian) * Protocol ID (2 bytes, big-endian, 0x0000 = Modbus) * Length (2 bytes, big-endian, length of remaining bytes) * Unit ID (1 byte, target slave/server address)

Followed by standard Modbus PDU (Function Code + Data). Note that Modbus TCP does NOT include RTU CRC-16.

Public Functions Documentation

function syn_mbap_decode_header

Decode an MBAP header from a byte buffer.

bool syn_mbap_decode_header (
    const uint8_t * buf,
    SYN_MBAP_Header * hdr
) 

Parameters:

  • buf Input byte buffer (must be at least 7 bytes).
  • hdr Pointer to destination MBAP header structure.

Returns:

true if valid Modbus protocol ID (0x0000).


function syn_mbap_encode_header

Encode an MBAP header into a byte buffer.

void syn_mbap_encode_header (
    const SYN_MBAP_Header * hdr,
    uint8_t * buf
) 

Parameters:

  • hdr Pointer to MBAP header structure.
  • buf Output byte buffer (must be at least 7 bytes).

function syn_modbus_tcp_build_client_adu

Build a Modbus TCP Master request ADU.

SYN_Status syn_modbus_tcp_build_client_adu (
    uint16_t transaction_id,
    uint8_t unit_id,
    const uint8_t * pdu,
    uint16_t pdu_len,
    uint8_t * req_adu,
    uint16_t req_max,
    uint16_t * req_len
) 

Parameters:

  • transaction_id Transaction sequence number.
  • unit_id Target Unit ID (slave address).
  • pdu Raw PDU bytes (Function Code + Data).
  • pdu_len PDU length in bytes.
  • req_adu Output buffer for complete Modbus TCP ADU.
  • req_max Output buffer capacity.
  • req_len Out: Total ADU length (7 + pdu_len).

Returns:

SYN_OK on success, SYN_NO_MEMORY if buffer too small.


function syn_modbus_tcp_process_slave

Process an incoming Modbus TCP ADU frame on a Modbus Slave instance.

bool syn_modbus_tcp_process_slave (
    SYN_Modbus * mb,
    const uint8_t * req_adu,
    uint16_t req_len,
    uint8_t * resp_adu,
    uint16_t resp_max,
    uint16_t * resp_len
) 

Takes a full Modbus TCP ADU (MBAP header + PDU), processes the PDU using the Modbus slave engine, and builds the corresponding Modbus TCP response ADU.

Parameters:

  • mb Modbus RTU/PDU slave instance.
  • req_adu Input Modbus TCP request ADU buffer.
  • req_len Length of request ADU in bytes.
  • resp_adu Output Modbus TCP response ADU buffer.
  • resp_max Capacity of response ADU buffer.
  • resp_len Out: Length of generated response ADU in bytes.

Returns:

true if response ADU generated, false if no response (or invalid ADU).


Macro Definition Documentation

define SYN_MBAP_HEADER_LEN

#define SYN_MBAP_HEADER_LEN `7`

MBAP header length in bytes


define SYN_MB_TCP_MAX_ADU

#define SYN_MB_TCP_MAX_ADU `260`

Max Modbus TCP ADU (7 MBAP + 253 PDU)



The documentation for this class was generated from the following file src/syntropic/proto/syn_modbus_tcp.h