Skip to content

File syn_modbus_tcp.c

FileList > proto > syn_modbus_tcp.c

Go to the source code of this file

Modbus TCP ADU (MBAP) framing implementation.

  • #include "../util/syn_assert.h"
  • #include "../util/syn_pack.h"
  • #include "syn_modbus_tcp.h"
  • #include <string.h>

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.

Public Static Functions

Type Name
uint16_t get_response_pdu_len (const uint8_t * buf)

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).


Public Static Functions Documentation

function get_response_pdu_len

static uint16_t get_response_pdu_len (
    const uint8_t * buf
) 


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