Добавление флага write в MB_DefineRegistersAddress
И рефакторинг дефайнов размеров сообщения
This commit is contained in:
@@ -65,6 +65,9 @@ CREATE_DEPRECATED_ALIAS(MB_EXTENDED_IDENTIFICATIONS, RID_EXTENDED_IDENTIFICATIO
|
|||||||
CREATE_DEPRECATED_ALIAS(MB_SPEDIFIC_IDENTIFICATIONS, RID_SPEDIFIC_IDENTIFICATIONS, MB_ReadDevId);
|
CREATE_DEPRECATED_ALIAS(MB_SPEDIFIC_IDENTIFICATIONS, RID_SPEDIFIC_IDENTIFICATIONS, MB_ReadDevId);
|
||||||
|
|
||||||
|
|
||||||
|
#define MbAddr DeviceAddr
|
||||||
|
|
||||||
|
|
||||||
/** MODBUS_LEGACY_API
|
/** MODBUS_LEGACY_API
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -41,31 +41,31 @@
|
|||||||
////////////////////---MODBUS MESSAGE DEFINES---/////////////////////
|
////////////////////---MODBUS MESSAGE DEFINES---/////////////////////
|
||||||
//-------------DEFINES FOR STRUCTURE----------------
|
//-------------DEFINES FOR STRUCTURE----------------
|
||||||
/* defines for structure of modbus message */
|
/* defines for structure of modbus message */
|
||||||
#define TransactionID_size 2 ///< size of (Transaction ID)
|
#define MbTransactionID_size 2 ///< size of (Transaction ID)
|
||||||
#define ProtocolID_size 2 ///< size of (Protocol ID)
|
#define MbProtocolID_size 2 ///< size of (Protocol ID)
|
||||||
#define PDULength_size 2 ///< size of (PDU Length)
|
#define MbPDULength_size 2 ///< size of (PDU Length)
|
||||||
#define MbAddr_SIZE 1 ///< size of (Slave Addr)
|
#define MbDeviceAddr_size 1 ///< size of (Slave Addr)
|
||||||
#define Func_Code_SIZE 1 ///< size of (Function Code)
|
#define MbFuncCode_size 1 ///< size of (Function Code)
|
||||||
#define Addr_SIZE 2 ///< size of (Address)
|
#define MbAddr_size 2 ///< size of (Address)
|
||||||
#define Qnt_SIZE 2 ///< size of (Quantity)
|
#define MbQnt_size 2 ///< size of (Quantity)
|
||||||
#define ByteCnt_SIZE 1 ///< size of (Byte Count)
|
#define MbByteCnt_size 1 ///< size of (Byte Count)
|
||||||
#define DATA_SIZE 125 ///< maximum number of data: DWORD (NOT MESSAGE SIZE)
|
#define MbData_size 125 ///< maximum number of data: DWORD (NOT MESSAGE SIZE)
|
||||||
#define CRC_SIZE 2 ///< size of (MbCRC) in bytes
|
#define MbCRC_size 2 ///< size of (MbCRC) in bytes
|
||||||
|
|
||||||
#ifndef MODBUS_PROTOCOL_TCP
|
#ifndef MODBUS_PROTOCOL_TCP
|
||||||
/** @brief Size of whole RTU message */
|
/** @brief Size of whole RTU message */
|
||||||
#define INFO_SIZE_MAX (MbAddr_SIZE+Func_Code_SIZE+Addr_SIZE+Qnt_SIZE+ByteCnt_SIZE)
|
#define RS_INFO_SIZE_MAX (MbDeviceAddr_size+MbFuncCode_size+MbAddr_size+MbQnt_size+MbByteCnt_size)
|
||||||
#else
|
#else
|
||||||
/** @brief Size of whole TCP message */
|
/** @brief Size of whole TCP message */
|
||||||
#define INFO_SIZE_MAX (TransactionID_size+ProtocolID_size+PDULength_size+MbAddr_SIZE+Func_Code_SIZE+Addr_SIZE+Qnt_SIZE)
|
#define RS_INFO_SIZE_MAX (MbTransactionID_size+MbProtocolID_size+MbPDULength_size+MbDeviceAddr_size+MbFuncCode_size+MbAddr_size+MbQnt_size)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** @brief Size of first part of message that will be received
|
/** @brief Size of first part of message that will be received
|
||||||
first receive info part of message, than defines size of rest message*/
|
first receive info part of message, than defines size of rest message*/
|
||||||
#define RX_FIRST_PART_SIZE INFO_SIZE_MAX
|
#define RS_RX_FIRST_PART_SIZE RS_INFO_SIZE_MAX
|
||||||
|
|
||||||
/** @brief Size of buffer: max size of whole message */
|
/** @brief Size of buffer: max size of whole message */
|
||||||
#define MSG_SIZE_MAX (INFO_SIZE_MAX + DATA_SIZE*2 + CRC_SIZE) // max possible size of message
|
#define RS_MSG_SIZE_MAX (RS_INFO_SIZE_MAX + MbData_size*2 + MbCRC_size) // max possible size of message
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Enum for modbus exception codes
|
* @brief Enum for modbus exception codes
|
||||||
@@ -129,7 +129,9 @@ typedef enum //MB_FunctonTypeDef
|
|||||||
FC_ERR_R_DEVICE_INFO = FC_R_DEVICE_ID + FC_ERR_VALUES_START, ///< Ошибка чтения информации об устройстве
|
FC_ERR_R_DEVICE_INFO = FC_R_DEVICE_ID + FC_ERR_VALUES_START, ///< Ошибка чтения информации об устройстве
|
||||||
}MB_FunctonTypeDef;
|
}MB_FunctonTypeDef;
|
||||||
|
|
||||||
/** @brief Enum for MEI func codes */
|
/**
|
||||||
|
* @brief Enum for MEI func codes
|
||||||
|
*/
|
||||||
typedef enum //MB_FunctonTypeDef
|
typedef enum //MB_FunctonTypeDef
|
||||||
{
|
{
|
||||||
MEI_DEVICE_IDENTIFICATIONS = 0x0E,
|
MEI_DEVICE_IDENTIFICATIONS = 0x0E,
|
||||||
@@ -180,14 +182,14 @@ typedef struct // RS_MsgTypeDef
|
|||||||
uint16_t PDULength; ///< Modbus TCP: PDU Length
|
uint16_t PDULength; ///< Modbus TCP: PDU Length
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint8_t MbAddr; ///< Modbus Slave Address
|
uint8_t DeviceAddr; ///< Modbus Slave Address
|
||||||
MB_FunctonTypeDef FuncCode; ///< Modbus Function Code
|
MB_FunctonTypeDef FuncCode; ///< Modbus Function Code
|
||||||
MB_DevIdMsgTypeDef DevId; ///< Read Device Identifications Header struct
|
MB_DevIdMsgTypeDef DevId; ///< Read Device Identifications Header struct
|
||||||
uint16_t Addr; ///< Modbus Address of data
|
uint16_t Addr; ///< Modbus Address of data
|
||||||
uint16_t Qnt; ///< Quantity of modbus data
|
uint16_t Qnt; ///< Quantity of modbus data
|
||||||
uint8_t ByteCnt; ///< Quantity of bytes of data in message to transmit/receive
|
uint8_t ByteCnt; ///< Quantity of bytes of data in message to transmit/receive
|
||||||
|
|
||||||
uint16_t MbData[DATA_SIZE]; ///< Modbus Data
|
uint16_t MbData[MbData_size]; ///< Modbus Data
|
||||||
MB_ExceptionTypeDef Except_Code; ///< Exception Code for the command
|
MB_ExceptionTypeDef Except_Code; ///< Exception Code for the command
|
||||||
|
|
||||||
uint16_t MbCRC; ///< Modbus CRC
|
uint16_t MbCRC; ///< Modbus CRC
|
||||||
@@ -298,7 +300,7 @@ extern RS_MsgTypeDef MODBUS_MSG;
|
|||||||
/* Check is address valid for certain array */
|
/* Check is address valid for certain array */
|
||||||
MB_ExceptionTypeDef MB_Check_Address_For_Arr(uint16_t Addr, uint16_t Qnt, uint16_t R_ARR_ADDR, uint16_t R_ARR_NUMB);
|
MB_ExceptionTypeDef MB_Check_Address_For_Arr(uint16_t Addr, uint16_t Qnt, uint16_t R_ARR_ADDR, uint16_t R_ARR_NUMB);
|
||||||
/* Define Address Origin for Input/Holding Registers */
|
/* Define Address Origin for Input/Holding Registers */
|
||||||
MB_ExceptionTypeDef MB_DefineRegistersAddress(uint16_t **pRegs, uint16_t Addr, uint16_t Qnt, uint8_t RegisterType);
|
MB_ExceptionTypeDef MB_DefineRegistersAddress(uint16_t **pRegs, uint16_t Addr, uint16_t Qnt, uint8_t RegisterType, uint8_t WriteFlag);
|
||||||
/* Define Address Origin for coils */
|
/* Define Address Origin for coils */
|
||||||
MB_ExceptionTypeDef MB_DefineCoilsAddress(uint16_t **pCoils, uint16_t Addr, uint16_t Qnt, uint16_t *start_shift, uint8_t WriteFlag);
|
MB_ExceptionTypeDef MB_DefineCoilsAddress(uint16_t **pCoils, uint16_t Addr, uint16_t Qnt, uint16_t *start_shift, uint8_t WriteFlag);
|
||||||
/** MODBUS_CMD_PROCESS_FUNCTIONS
|
/** MODBUS_CMD_PROCESS_FUNCTIONS
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
4. Инициализировать через RS_Init() и запустить прием RS_Receive_IT()
|
4. Инициализировать через RS_Init() и запустить прием RS_Receive_IT()
|
||||||
|
|
||||||
@section features Особенности:
|
@section features Особенности:
|
||||||
- Буфер: RS_Buffer[MSG_SIZE_MAX] Общий для приема/передачи
|
- Буфер: RS_Buffer[RS_MSG_SIZE_MAX] Общий для приема/передачи
|
||||||
- Состояния: отслеживается через флаги в RS_HandleTypeDef
|
- Состояния: отслеживается через флаги в RS_HandleTypeDef
|
||||||
- Таймауты: контролируют максимальное время ожидания фрейма
|
- Таймауты: контролируют максимальное время ожидания фрейма
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
@@ -36,8 +36,8 @@
|
|||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
////////////////////////////---DEFINES---////////////////////////////
|
////////////////////////////---DEFINES---////////////////////////////
|
||||||
/* Check that all defines required by RS are defined */
|
/* Check that all defines required by RS are defined */
|
||||||
#ifndef MSG_SIZE_MAX
|
#ifndef RS_MSG_SIZE_MAX
|
||||||
#error Define MSG_SIZE_MAX (Maximum size of message). This is necessary to create buffer for UART.
|
#error Define RS_MSG_SIZE_MAX (Maximum size of message). This is necessary to create buffer for UART.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,7 +45,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/* Clear message-uart buffer */
|
/* Clear message-uart buffer */
|
||||||
#define RS_Clear_Buff(_buff_) for(int i=0; i<MSG_SIZE_MAX;i++) _buff_[i] = NULL
|
#define RS_Clear_Buff(_buff_) for(int i=0; i<RS_MSG_SIZE_MAX;i++) _buff_[i] = NULL
|
||||||
|
|
||||||
/* Set/Reset flags */
|
/* Set/Reset flags */
|
||||||
#define RS_Set_Free(_hRS_) _hRS_->f.RS_Busy = 0
|
#define RS_Set_Free(_hRS_) _hRS_->f.RS_Busy = 0
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ void MB_WriteObjectsToMessage(RS_MsgTypeDef *modbus_msg, unsigned maxidofobj)
|
|||||||
|
|
||||||
/* Define number of object in one message */
|
/* Define number of object in one message */
|
||||||
unsigned lastobjid = 0;
|
unsigned lastobjid = 0;
|
||||||
for(int i = 0; i < DATA_SIZE*2;)
|
for(int i = 0; i < MbData_size*2;)
|
||||||
{
|
{
|
||||||
/* Если объект за пределами допутимого - выходим из цикла */
|
/* Если объект за пределами допутимого - выходим из цикла */
|
||||||
if(objidtmp >= 0xFF + MODBUS_NUMB_OF_USEROBJECTS)
|
if(objidtmp >= 0xFF + MODBUS_NUMB_OF_USEROBJECTS)
|
||||||
@@ -60,7 +60,7 @@ void MB_WriteObjectsToMessage(RS_MsgTypeDef *modbus_msg, unsigned maxidofobj)
|
|||||||
i += 2;
|
i += 2;
|
||||||
i += obj[objidtmp].length;
|
i += obj[objidtmp].length;
|
||||||
/* Если все еще помещается в массив переходим на следующий объект */
|
/* Если все еще помещается в массив переходим на следующий объект */
|
||||||
if(i < DATA_SIZE*2)
|
if(i < MbData_size*2)
|
||||||
{
|
{
|
||||||
objidtmp++;
|
objidtmp++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ MB_ExceptionTypeDef MB_Holding_Write_Global(uint16_t Addr, uint16_t WriteVal)
|
|||||||
uint16_t *pHoldRegs;
|
uint16_t *pHoldRegs;
|
||||||
|
|
||||||
//------------WRITE COIL-------------
|
//------------WRITE COIL-------------
|
||||||
Exception = MB_DefineRegistersAddress(&pHoldRegs, Addr, 1, RegisterType_Holding);
|
Exception = MB_DefineRegistersAddress(&pHoldRegs, Addr, 1, RegisterType_Holding, 1);
|
||||||
if(Exception == ET_NO_ERRORS)
|
if(Exception == ET_NO_ERRORS)
|
||||||
{
|
{
|
||||||
*(pHoldRegs) = WriteVal;
|
*(pHoldRegs) = WriteVal;
|
||||||
@@ -66,7 +66,7 @@ uint16_t MB_Holding_Read_Global(uint16_t Addr, MB_ExceptionTypeDef *Exception)
|
|||||||
uint16_t *pHoldRegs;
|
uint16_t *pHoldRegs;
|
||||||
|
|
||||||
//------------READ COIL--------------
|
//------------READ COIL--------------
|
||||||
Exception_tmp = MB_DefineRegistersAddress(&pHoldRegs, Addr, 1, RegisterType_Holding);
|
Exception_tmp = MB_DefineRegistersAddress(&pHoldRegs, Addr, 1, RegisterType_Holding, 0);
|
||||||
|
|
||||||
if(Exception) // if exception is not given to func fill it
|
if(Exception) // if exception is not given to func fill it
|
||||||
*Exception = Exception_tmp;
|
*Exception = Exception_tmp;
|
||||||
@@ -94,7 +94,7 @@ uint8_t MB_Process_Read_Hold_Regs(RS_MsgTypeDef *modbus_msg)
|
|||||||
//---------CHECK FOR ERRORS----------
|
//---------CHECK FOR ERRORS----------
|
||||||
// get origin address for data
|
// get origin address for data
|
||||||
uint16_t *pHoldRegs;
|
uint16_t *pHoldRegs;
|
||||||
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Holding); // определение адреса регистров
|
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Holding, 0); // определение адреса регистров
|
||||||
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ uint8_t MB_Process_Write_Single_Reg(RS_MsgTypeDef *modbus_msg)
|
|||||||
{
|
{
|
||||||
// get origin address for data
|
// get origin address for data
|
||||||
uint16_t *pHoldRegs;
|
uint16_t *pHoldRegs;
|
||||||
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, 1, RegisterType_Holding); // определение адреса регистров
|
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, 1, RegisterType_Holding, 1); // определение адреса регистров
|
||||||
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ uint8_t MB_Process_Write_Miltuple_Regs(RS_MsgTypeDef *modbus_msg)
|
|||||||
}
|
}
|
||||||
// get origin address for data
|
// get origin address for data
|
||||||
uint16_t *pHoldRegs;
|
uint16_t *pHoldRegs;
|
||||||
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Holding); // определение адреса регистров
|
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pHoldRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Holding, 1); // определение адреса регистров
|
||||||
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ MB_ExceptionTypeDef MB_Input_Write_Global(uint16_t Addr, uint16_t WriteVal)
|
|||||||
uint16_t *pInRegs;
|
uint16_t *pInRegs;
|
||||||
|
|
||||||
//------------WRITE COIL-------------
|
//------------WRITE COIL-------------
|
||||||
Exception = MB_DefineRegistersAddress(&pInRegs, Addr, 1, RegisterType_Input);
|
Exception = MB_DefineRegistersAddress(&pInRegs, Addr, 1, RegisterType_Input, 1);
|
||||||
if(Exception == ET_NO_ERRORS)
|
if(Exception == ET_NO_ERRORS)
|
||||||
{
|
{
|
||||||
*(pInRegs) = WriteVal;
|
*(pInRegs) = WriteVal;
|
||||||
@@ -62,7 +62,7 @@ uint16_t MB_Input_Read_Global(uint16_t Addr, MB_ExceptionTypeDef *Exception)
|
|||||||
uint16_t *pInRegs;
|
uint16_t *pInRegs;
|
||||||
|
|
||||||
//------------READ COIL--------------
|
//------------READ COIL--------------
|
||||||
Exception_tmp = MB_DefineRegistersAddress(&pInRegs, Addr, 1, RegisterType_Input);
|
Exception_tmp = MB_DefineRegistersAddress(&pInRegs, Addr, 1, RegisterType_Input, 0);
|
||||||
|
|
||||||
if(Exception) // if exception is not given to func fill it
|
if(Exception) // if exception is not given to func fill it
|
||||||
*Exception = Exception_tmp;
|
*Exception = Exception_tmp;
|
||||||
@@ -89,7 +89,7 @@ uint8_t MB_Process_Read_Input_Regs(RS_MsgTypeDef *modbus_msg)
|
|||||||
//---------CHECK FOR ERRORS----------
|
//---------CHECK FOR ERRORS----------
|
||||||
// get origin address for data
|
// get origin address for data
|
||||||
uint16_t *pInRegs;
|
uint16_t *pInRegs;
|
||||||
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pInRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Input); // определение адреса регистров
|
modbus_msg->Except_Code = MB_DefineRegistersAddress(&pInRegs, modbus_msg->Addr, modbus_msg->Qnt, RegisterType_Input, 0); // определение адреса регистров
|
||||||
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
if(modbus_msg->Except_Code != ET_NO_ERRORS)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|||||||
@@ -374,7 +374,7 @@ static int MB_Define_Size_of_Function(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mb_func_size = RX_FIRST_PART_SIZE + mb_func_size; // size of whole message
|
mb_func_size = RS_RX_FIRST_PART_SIZE + mb_func_size; // size of whole message
|
||||||
return mb_func_size;
|
return mb_func_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -392,7 +392,7 @@ RS_StatusTypeDef MB_Master_Collect_Message(RS_HandleTypeDef *hmodbus, RS_MsgType
|
|||||||
//------INFO ABOUT DATA/MESSAGE------
|
//------INFO ABOUT DATA/MESSAGE------
|
||||||
//-----------[first bytes]-----------
|
//-----------[first bytes]-----------
|
||||||
// set ID of slave device
|
// set ID of slave device
|
||||||
modbus_uart_buff[ind++] = modbus_msg->MbAddr;
|
modbus_uart_buff[ind++] = modbus_msg->DeviceAddr;
|
||||||
|
|
||||||
// set function code
|
// set function code
|
||||||
modbus_uart_buff[ind++] = modbus_msg->FuncCode;
|
modbus_uart_buff[ind++] = modbus_msg->FuncCode;
|
||||||
@@ -472,7 +472,7 @@ RS_StatusTypeDef MB_Master_Parse_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeDe
|
|||||||
int expected_size = 0;
|
int expected_size = 0;
|
||||||
|
|
||||||
// get ID of slave device
|
// get ID of slave device
|
||||||
modbus_msg->MbAddr = modbus_uart_buff[ind++];
|
modbus_msg->DeviceAddr = modbus_uart_buff[ind++];
|
||||||
|
|
||||||
// get function code (check if error response)
|
// get function code (check if error response)
|
||||||
modbus_msg->FuncCode = modbus_uart_buff[ind++];
|
modbus_msg->FuncCode = modbus_uart_buff[ind++];
|
||||||
@@ -633,7 +633,7 @@ RS_MsgTypeDef MB_REQUEST_WRITE_MULTIPLE_COILS(uint8_t slave_addr, uint16_t start
|
|||||||
|
|
||||||
// Copy coil data to message MbData array
|
// Copy coil data to message MbData array
|
||||||
for(int i = 0; i < byte_count; i++) {
|
for(int i = 0; i < byte_count; i++) {
|
||||||
if(i < DATA_SIZE) {
|
if(i < MbData_size) {
|
||||||
msg.MbData[i] = coils_data[i];
|
msg.MbData[i] = coils_data[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -649,7 +649,7 @@ RS_MsgTypeDef MB_REQUEST_WRITE_MULTIPLE_REGS(uint8_t slave_addr, uint16_t start_
|
|||||||
msg.ByteCnt = quantity * 2; // Each register is 2 bytes
|
msg.ByteCnt = quantity * 2; // Each register is 2 bytes
|
||||||
|
|
||||||
// Copy register data to message MbData array
|
// Copy register data to message MbData array
|
||||||
for(int i = 0; i < quantity && i < DATA_SIZE; i++) {
|
for(int i = 0; i < quantity && i < MbData_size; i++) {
|
||||||
msg.MbData[i] = regs_data[i];
|
msg.MbData[i] = regs_data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ RS_StatusTypeDef MB_Slave_Response(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef *mod
|
|||||||
RS_Reset_TX_Flags(hmodbus); // reset flag for correct transmit
|
RS_Reset_TX_Flags(hmodbus); // reset flag for correct transmit
|
||||||
|
|
||||||
MB_Diagnostics_BusMessageCnt();
|
MB_Diagnostics_BusMessageCnt();
|
||||||
if(hmodbus->ID == 0 || modbus_msg->MbAddr == 0)
|
if(hmodbus->ID == 0 || modbus_msg->DeviceAddr == 0)
|
||||||
{
|
{
|
||||||
MB_Diagnostics_SlaveNoResponseCnt(); // <-- Устройство не отвечает на широковещательные сообщения
|
MB_Diagnostics_SlaveNoResponseCnt(); // <-- Устройство не отвечает на широковещательные сообщения
|
||||||
hmodbus->RS_STATUS = RS_SKIP;
|
hmodbus->RS_STATUS = RS_SKIP;
|
||||||
@@ -185,7 +185,7 @@ RS_StatusTypeDef MB_Slave_Collect_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeD
|
|||||||
#endif
|
#endif
|
||||||
//-----------[first bytes]-----------
|
//-----------[first bytes]-----------
|
||||||
// set ID of message/user
|
// set ID of message/user
|
||||||
modbus_uart_buff[ind++] = modbus_msg->MbAddr;
|
modbus_uart_buff[ind++] = modbus_msg->DeviceAddr;
|
||||||
|
|
||||||
// set dat or err response
|
// set dat or err response
|
||||||
modbus_uart_buff[ind++] = modbus_msg->FuncCode;
|
modbus_uart_buff[ind++] = modbus_msg->FuncCode;
|
||||||
@@ -204,7 +204,7 @@ RS_StatusTypeDef MB_Slave_Collect_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeD
|
|||||||
modbus_uart_buff[ind++] = modbus_msg->DevId.NextObjId;
|
modbus_uart_buff[ind++] = modbus_msg->DevId.NextObjId;
|
||||||
modbus_uart_buff[ind++] = modbus_msg->DevId.NumbOfObj;
|
modbus_uart_buff[ind++] = modbus_msg->DevId.NumbOfObj;
|
||||||
|
|
||||||
if (modbus_msg->ByteCnt > DATA_SIZE*2) // if ByteCnt less than DATA_SIZE
|
if (modbus_msg->ByteCnt > MbData_size*2) // if ByteCnt less than MbData_size
|
||||||
{
|
{
|
||||||
TrackerCnt_Err(hmodbus->rs_err);
|
TrackerCnt_Err(hmodbus->rs_err);
|
||||||
return RS_COLLECT_MSG_ERR;
|
return RS_COLLECT_MSG_ERR;
|
||||||
@@ -235,7 +235,7 @@ RS_StatusTypeDef MB_Slave_Collect_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeD
|
|||||||
else // modbus data header
|
else // modbus data header
|
||||||
{
|
{
|
||||||
// set size of received data
|
// set size of received data
|
||||||
if (modbus_msg->ByteCnt <= DATA_SIZE*2) // if ByteCnt less than DATA_SIZE
|
if (modbus_msg->ByteCnt <= MbData_size*2) // if ByteCnt less than MbData_size
|
||||||
modbus_uart_buff[ind++] = modbus_msg->ByteCnt;
|
modbus_uart_buff[ind++] = modbus_msg->ByteCnt;
|
||||||
else // otherwise return data_size err
|
else // otherwise return data_size err
|
||||||
{
|
{
|
||||||
@@ -314,7 +314,7 @@ static int MB_Define_Size_of_Function(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef *
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mb_func_size = RX_FIRST_PART_SIZE + mb_func_size; // size of whole message
|
mb_func_size = RS_RX_FIRST_PART_SIZE + mb_func_size; // size of whole message
|
||||||
return mb_func_size;
|
return mb_func_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,12 +348,12 @@ RS_StatusTypeDef MB_Slave_Parse_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef
|
|||||||
// get ID of message/user
|
// get ID of message/user
|
||||||
if(modbus_uart_buff[ind] != hmodbus->ID)
|
if(modbus_uart_buff[ind] != hmodbus->ID)
|
||||||
{
|
{
|
||||||
modbus_msg->MbAddr = 0;
|
modbus_msg->DeviceAddr = 0;
|
||||||
ind++;
|
ind++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
modbus_msg->MbAddr = modbus_uart_buff[ind++];
|
modbus_msg->DeviceAddr = modbus_uart_buff[ind++];
|
||||||
}
|
}
|
||||||
|
|
||||||
// get func code
|
// get func code
|
||||||
@@ -361,7 +361,7 @@ RS_StatusTypeDef MB_Slave_Parse_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef
|
|||||||
if(modbus_msg->FuncCode & FC_ERR_VALUES_START) // явная херня
|
if(modbus_msg->FuncCode & FC_ERR_VALUES_START) // явная херня
|
||||||
{
|
{
|
||||||
MB_Diagnostics_SlaveNAKCnt();
|
MB_Diagnostics_SlaveNAKCnt();
|
||||||
modbus_msg->MbAddr = 0;
|
modbus_msg->DeviceAddr = 0;
|
||||||
return RS_SKIP;
|
return RS_SKIP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,7 +424,7 @@ RS_StatusTypeDef MB_Slave_Parse_Message(RS_HandleTypeDef *hmodbus, RS_MsgTypeDef
|
|||||||
if (modbus_msg->ByteCnt != 0)
|
if (modbus_msg->ByteCnt != 0)
|
||||||
{
|
{
|
||||||
//check that data size is correct
|
//check that data size is correct
|
||||||
if (modbus_msg->ByteCnt > DATA_SIZE*2)
|
if (modbus_msg->ByteCnt > MbData_size*2)
|
||||||
{
|
{
|
||||||
TrackerCnt_Err(hmodbus->rs_err);
|
TrackerCnt_Err(hmodbus->rs_err);
|
||||||
modbus_msg->FuncCode |= FC_ERR_VALUES_START;
|
modbus_msg->FuncCode |= FC_ERR_VALUES_START;
|
||||||
|
|||||||
@@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
@section arch Архитектура:
|
@section arch Архитектура:
|
||||||
В режиме слейв:
|
В режиме слейв:
|
||||||
- Инициализация приема с сообщения с максимальным размером MSG_SIZE_MAX
|
- Инициализация приема с сообщения с максимальным размером RS_MSG_SIZE_MAX
|
||||||
- При срабатывании прерывания IDLE - обработка полученного сообщения
|
- При срабатывании прерывания IDLE - обработка полученного сообщения
|
||||||
В режиме мастер (пока не реализовано):
|
В режиме мастер (пока не реализовано):
|
||||||
- Отправка запроса и переход в режим приема сообщения с максимальным размером MSG_SIZE_MAX
|
- Отправка запроса и переход в режим приема сообщения с максимальным размером RS_MSG_SIZE_MAX
|
||||||
- При срабатывании прерывания IDLE - обработка полученного ответа
|
- При срабатывании прерывания IDLE - обработка полученного ответа
|
||||||
|
|
||||||
@section ithandler Необходимые обработчики:
|
@section ithandler Необходимые обработчики:
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
#include "rs_message.h"
|
#include "rs_message.h"
|
||||||
#include "modbus_diag.h"
|
#include "modbus_diag.h"
|
||||||
|
|
||||||
uint8_t RS_Buffer[MSG_SIZE_MAX]; // uart buffer
|
uint8_t RS_Buffer[RS_MSG_SIZE_MAX]; // uart buffer
|
||||||
|
|
||||||
extern void RS_UART_Init(void);
|
extern void RS_UART_Init(void);
|
||||||
extern void RS_UART_DeInit(UART_HandleTypeDef *huart);
|
extern void RS_UART_DeInit(UART_HandleTypeDef *huart);
|
||||||
@@ -61,7 +61,7 @@ RS_StatusTypeDef RS_Receive_IT(RS_HandleTypeDef *hRS, RS_MsgTypeDef *RS_msg)
|
|||||||
|
|
||||||
// start receiving
|
// start receiving
|
||||||
__HAL_UART_ENABLE_IT(hRS->huart, UART_IT_IDLE);
|
__HAL_UART_ENABLE_IT(hRS->huart, UART_IT_IDLE);
|
||||||
uart_res = HAL_UART_Receive_IT(hRS->huart, &hRS->pBufferPtr[hRS->RS_Message_Size], MSG_SIZE_MAX); // receive until ByteCnt+1 byte,
|
uart_res = HAL_UART_Receive_IT(hRS->huart, &hRS->pBufferPtr[hRS->RS_Message_Size], RS_MSG_SIZE_MAX); // receive until ByteCnt+1 byte,
|
||||||
// then in Callback restart receive for rest bytes
|
// then in Callback restart receive for rest bytes
|
||||||
|
|
||||||
// if receive isnt started - abort RS
|
// if receive isnt started - abort RS
|
||||||
@@ -445,7 +445,7 @@ void RS_TIM_Handler(RS_HandleTypeDef *hRS)
|
|||||||
hRS->RS_STATUS = RS_TIMEOUT;
|
hRS->RS_STATUS = RS_TIMEOUT;
|
||||||
|
|
||||||
if(hRS->sRS_Mode < RS_MASTER_MODE_START)
|
if(hRS->sRS_Mode < RS_MASTER_MODE_START)
|
||||||
if(hRS->pMessagePtr->MbAddr == hRS->ID) // ошибка если таймаут по нашему сообщению
|
if(hRS->pMessagePtr->DeviceAddr == hRS->ID) // ошибка если таймаут по нашему сообщению
|
||||||
TrackerCnt_Err(hRS->rs_err);
|
TrackerCnt_Err(hRS->rs_err);
|
||||||
|
|
||||||
if(hRS->sRS_Mode >= RS_MASTER_MODE_START)
|
if(hRS->sRS_Mode >= RS_MASTER_MODE_START)
|
||||||
|
|||||||
@@ -65,10 +65,10 @@ MB_ExceptionTypeDef MB_Check_Address_For_Arr(uint16_t Addr, uint16_t Qnt, uint16
|
|||||||
* @details Определение адреса начального регистра.
|
* @details Определение адреса начального регистра.
|
||||||
* @note WriteFlag пока не используется.
|
* @note WriteFlag пока не используется.
|
||||||
*/
|
*/
|
||||||
MB_ExceptionTypeDef MB_DefineRegistersAddress(uint16_t **pRegs, uint16_t Addr, uint16_t Qnt, uint8_t RegisterType)
|
MB_ExceptionTypeDef MB_DefineRegistersAddress(uint16_t **pRegs, uint16_t Addr, uint16_t Qnt, uint8_t RegisterType, uint8_t WriteFlag)
|
||||||
{
|
{
|
||||||
/* check quantity error */
|
/* check quantity error */
|
||||||
if (Qnt > DATA_SIZE)
|
if (Qnt > MbData_size)
|
||||||
{
|
{
|
||||||
return ET_ILLEGAL_DATA_VALUE; // return exception code
|
return ET_ILLEGAL_DATA_VALUE; // return exception code
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user