From b71b799566dee8aa821547aa76a4755cfa8ba94b Mon Sep 17 00:00:00 2001 From: Razvalyaev Date: Wed, 5 Nov 2025 18:18:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D1=80=D0=BE=D1=82=D0=BE=D0=BA=D0=BE=D0=BB=20TCP?= =?UTF-8?q?,=20=D0=BD=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inc/modbus_core.h | 26 ++++++++++++++++++++------ Src/modbus_master.c | 2 +- __modbus_config.h | 1 + 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Inc/modbus_core.h b/Inc/modbus_core.h index cf11c46..3935fd6 100644 --- a/Inc/modbus_core.h +++ b/Inc/modbus_core.h @@ -40,16 +40,24 @@ ////////////////////---MODBUS MESSAGE DEFINES---///////////////////// //-------------DEFINES FOR STRUCTURE---------------- /* defines for structure of modbus message */ -#define MbAddr_SIZE 1 ///< size of (MbAddr) -#define Func_Code_SIZE 1 ///< size of (Func_Code) -#define Addr_SIZE 2 ///< size of (Addr) -#define Qnt_SIZE 2 ///< size of (Qnt) -#define ByteCnt_SIZE 1 ///< size of (ByteCnt) +#define ID_Transaction_size 2 ///< size of (ID_Transaction) +#define ID_Protocol_size 2 ///< size of (ID_Protocol) +#define PDU_Len_size 2 ///< size of (PDU Length) +#define MbAddr_SIZE 1 ///< size of (Slave Addr) +#define Func_Code_SIZE 1 ///< size of (Function Code) +#define Addr_SIZE 2 ///< size of (Address) +#define Qnt_SIZE 2 ///< size of (Quantity) +#define ByteCnt_SIZE 1 ///< size of (Byte Count) #define DATA_SIZE 125 ///< maximum number of data: DWORD (NOT MESSAGE SIZE) #define CRC_SIZE 2 ///< size of (MB_CRC) in bytes -/** @brief Size of whole message */ +#ifndef MODBUS_PROTOCOL_TCP +/** @brief Size of whole RTU message */ #define INFO_SIZE_MAX (MbAddr_SIZE+Func_Code_SIZE+Addr_SIZE+Qnt_SIZE+ByteCnt_SIZE) +#else +/** @brief Size of whole TCP message */ +#define INFO_SIZE_MAX (ID_Transaction_size+ID_Protocol_size+PDU_Len_size+MbAddr_SIZE+Func_Code_SIZE+Addr_SIZE+Qnt_SIZE) +#endif /** @brief Size of first part of message that will be received first receive info part of message, than defines size of rest message*/ @@ -161,6 +169,12 @@ typedef struct /** @brief Structure for modbus messsage */ typedef struct // RS_MsgTypeDef { +#ifdef MODBUS_PROTOCOL_TCP + uint16_t ID_Transaction; ///< Modbus TCP: ID Transaction + uint16_t ID_Prot; ///< Modbus TCP: ID Protocol + uint16_t PDU_Length; ///< Modbus TCP: PDU_Length +#endif + uint8_t MbAddr; ///< Modbus Slave Address MB_FunctonTypeDef Func_Code; ///< Modbus Function Code MB_DevIdMsgTypeDef DevId; ///< Read Device Identifications Header struct diff --git a/Src/modbus_master.c b/Src/modbus_master.c index a6d5709..c6b6d2c 100644 --- a/Src/modbus_master.c +++ b/Src/modbus_master.c @@ -93,7 +93,7 @@ int MB_RespGet_CoilState(RS_MsgTypeDef *modbus_msg, uint16_t coil_addr, int *coi if(bit_index < 8) *coil_state = (modbus_msg->DATA[data_index] >> (bit_index+8)) & 0x01; else - *coil_state = (modbus_msg->DATA[data_index] >> bit_index) & 0x01; + *coil_state = ((modbus_msg->DATA[data_index]&0xFF) >> bit_index) & 0x01; return 1; diff --git a/__modbus_config.h b/__modbus_config.h index 53aec6f..ae66ba2 100644 --- a/__modbus_config.h +++ b/__modbus_config.h @@ -51,6 +51,7 @@ #define MODBUS_ENABLE_DEVICE_IDENTIFICATIONS ///< Включить обработку идентификаторы устройства #define MODBUS_ENABLE_DIAGNOSTICS ///< Включить обработку диагностики модбас +#define MODBUS_PROTOCOL_TCP ///< Включить TCP-протокол, иначе - RTU /** * @brief Поменять комманды 0x03 и 0x04 местами (для LabView терминалки от двигателей)