Skip to content

Commit

Permalink
add modbus function
Browse files Browse the repository at this point in the history
  • Loading branch information
epcbtech committed Jan 9, 2023
1 parent 07b8552 commit a1906d3
Show file tree
Hide file tree
Showing 15 changed files with 623 additions and 7 deletions.
4 changes: 1 addition & 3 deletions application/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ IF_NETWORK_NRF24_OPTION = -UIF_NETWORK_NRF24_EN

# Task compile option.
TASK_ZIGBEE_OPTION = -UTASK_ZIGBEE_EN
TASK_MBMASTER_OPTION = -UTASK_MBMASTER_EN
TASK_MBMASTER_OPTION = -DTASK_MBMASTER_EN

# Include sources file
include sources/ak/Makefile.mk
Expand Down Expand Up @@ -120,8 +120,6 @@ IF_OPTION += $(IF_LINK_OPTION) \
# App option
TASK_OPTION += $(TASK_ZIGBEE_OPTION)
TASK_OPTION += $(TASK_MBMASTER_OPTION)
TASK_OPTION += $(TASK_BUZZER_OPTION)
TASK_OPTION += $(TASK_NRF24_OPTION)

# Console log compile option
CONSOLE_OPTION += \
Expand Down
1 change: 1 addition & 0 deletions application/sources/app/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SOURCES_CPP += sources/app/app.cpp
SOURCES_CPP += sources/app/app_data.cpp
SOURCES_CPP += sources/app/app_non_clear_ram.cpp
SOURCES_CPP += sources/app/app_bsp.cpp
SOURCES_CPP += sources/app/app_modbus_pull.cpp
SOURCES_CPP += sources/app/shell.cpp

SOURCES_CPP += sources/app/task_shell.cpp
Expand Down
8 changes: 6 additions & 2 deletions application/sources/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ enum {
*/
/*****************************************************************************/
/* define timer */
#define AC_DISPLAY_INITIAL_INTERVAL (300)
#define AC_DISPLAY_INITIAL_INTERVAL (100)
#define AC_DISPLAY_STARTUP_INTERVAL (2000)
#define AC_DISPLAY_LOGO_INTERVAL (10000)
#define AC_DISPLAY_SHOW_IDLE_BALL_MOVING_UPDATE_INTERAL (150)
#define AC_DISPLAY_SHOW_MERRY_CHRISTMAS_SNOW_MOVING_UPDATE_INTERAL (150)
#define AC_DISPLAY_SHOW_MERRY_CHRISTMAS_SLEEP_INTERVAL (15000)
#define AC_DISPLAY_SHOW_MODBUS_PULL_INTERVAL (1500)
#define AC_DISPLAY_SHOW_MODBUS_PULL_SLEEP_INTERVAL (30000)

/* define signal */
enum {
Expand All @@ -150,7 +152,9 @@ enum {
AC_DISPLAY_SHOW_FW_UPDATE,
AC_DISPLAY_SHOW_FW_UPDATE_ERR,
AC_DISPLAY_SHOW_MERRY_CHRISTMAS_SNOW_MOVING_UPDATE,
AC_DISPLAY_SHOW_MERRY_CHRISTMAS_SLEEP
AC_DISPLAY_SHOW_MERRY_CHRISTMAS_SLEEP,
AC_DISPLAY_SHOW_MODBUS_PULL_UPDATE,
AC_DISPLAY_SHOW_MODBUS_PULL_SLEEP
};

/*****************************************************************************/
Expand Down
161 changes: 161 additions & 0 deletions application/sources/app/app_modbus_pull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* kernel include */
#include "ak.h"
#include "message.h"
#include "timer.h"
#include "fsm.h"

/* driver include */
#include "led.h"
#include "button.h"
#include "flash.h"

/* app include */
#include "app.h"
#include "app_dbg.h"
#include "app_bsp.h"
#include "app_flash.h"
#include "app_data.h"
#include "app_non_clear_ram.h"
#include "app_modbus_pull.h"

/* sys include */
#include "sys_io.h"
#include "sys_ctrl.h"


/*----------------------------------------------------------------------------*
* Device name: Cảm biến nhiệt độ, độ ẩm RS485 Modbus RTU ES35-SW (SHT35)
* Product link: https://epcb.vn/products/cam-bien-nhiet-do-do-am-rs485-modbus-rtu-es35-sw
* Note:
*----------------------------------------------------------------------------*/
static MB_DevRegStruct_t Es35SW_Registers[] = {
{ 0 , REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_REGISTERS, 0.1, (const int8_t*)"*C", INT16U10, true , (const int8_t*)"Temperature value"},
{ 1 , REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_REGISTERS, 0.1, (const int8_t*)"RH", INT16U10, true , (const int8_t*)"Humidity value" },
{ 100, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_REGISTERS, 1, (const int8_t*)"NA", INT16U10, false, (const int8_t*)"Device address" },
{ 101, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_REGISTERS, 1, (const int8_t*)"NA", INT16U10, false, (const int8_t*)"Device baudrate" },
};

MB_DeviceStruct_t MB_ES35SW_TH_Sensor = {
.tId = 2,
.tBaud = 9600,
.listRegDevice = Es35SW_Registers,
.listRegAmount = sizeof(Es35SW_Registers) / sizeof(Es35SW_Registers[0])
};

/*----------------------------------------------------------------------------*
* Device name: Relay 4 kênh IO giao tiếp RS485/RS232 công nghiệp LH-IO-01
* Product link: https://epcb.vn/products/relay-4-kenh-dau-ra-io-giao-tiep-rs485-rs232-cong-nghiep-lh-io-01
* Note:
*----------------------------------------------------------------------------*/
static MB_DevRegStruct_t MB_LHIO404_IO_Device_Registers[] = {
{ 1, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_COILS , 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Ouput 2 status"},
{ 2, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_COILS , 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Ouput 3 status"},
{ 0, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_COILS , 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Ouput 1 status"},
{ 3, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_COILS , 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Ouput 4 status"},
{ 0, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_DISCRETE_INPUT, 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Input 1 status"},
{ 1, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_DISCRETE_INPUT, 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Input 2 status"},
{ 2, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_DISCRETE_INPUT, 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Input 3 status"},
{ 3, REG_VAL_DEFAULT, MODBUS_FUNCTION_READ_DISCRETE_INPUT, 1, (const int8_t*)"NA", INT8U, true, (const int8_t*)"Input 4 status"},
};

MB_DeviceStruct_t MB_LHIO404_IO_Device = {
.tId = 3,
.tBaud = 9600,
.listRegDevice = MB_LHIO404_IO_Device_Registers,
.listRegAmount = sizeof(MB_LHIO404_IO_Device_Registers) / sizeof(MB_LHIO404_IO_Device_Registers[0])
};

/* Private functions prototypes -----------------------------------------------*/
static eMBErrorCode appMBMasterRead(UCHAR slAddr, uint8_t funCode, USHORT addReg, USHORT *buf);
static eMBErrorCode appMBMasterWrite(UCHAR slAddr, uint8_t funCode, USHORT addReg, USHORT val);

/* Function implementation ---------------------------------------------------*/
void updateDataModbusDevice(MB_DeviceStruct_t *mbDevice) {
uint8_t retryCount = 0;
USHORT regVal;
USHORT regAddr;
uint8_t funCode;

for (uint16_t regIndex = 0; regIndex < mbDevice->listRegAmount; ++regIndex) {
funCode = mbDevice->listRegDevice[regIndex].funcCode;
regAddr = mbDevice->listRegDevice[regIndex].regAddress;
regVal = 0;

eMBErrorCode errCode = appMBMasterRead(mbDevice->tId, funCode, regAddr, &regVal);

if (errCode != MB_ENOERR) {
mbDevice->listRegDevice[regIndex].regValue = REG_VAL_DEFAULT;
++retryCount;
}
else {
mbDevice->listRegDevice[regIndex].regValue = regVal;
}

if (retryCount > MB_READ_FAILED_RETRY_MAX) {
break;
}
}
}

eMBErrorCode appMBMasterRead(UCHAR slAddr, uint8_t funCode, USHORT addReg, USHORT *buf) {
eMBErrorCode errCodeRet = MB_ENOERR;

switch (funCode) {
case MODBUS_FUNCTION_READ_COILS: {
errCodeRet = eMBMReadCoils(xMBMMaster, slAddr, addReg, 1, (UBYTE*)buf);
}
break;

case MODBUS_FUNCTION_READ_DISCRETE_INPUT: {
errCodeRet = eMBMReadDiscreteInputs(xMBMMaster, slAddr, addReg, 1, (UBYTE*)buf);
}
break;

case MODBUS_FUNCTION_READ_REGISTERS: {
errCodeRet = eMBMReadHoldingRegisters(xMBMMaster, slAddr, addReg, 1, buf);
}
break;

case MODBUS_FUNCTION_READ_INPUT_REGISTER: {
errCodeRet = eMBMReadInputRegisters(xMBMMaster, slAddr, addReg, 1, buf);
}
break;

default:
errCodeRet = MB_EINVAL;
break;
}

return errCodeRet;
}

eMBErrorCode appMBMasterWrite(UCHAR slAddr, uint8_t funCode, USHORT addReg, USHORT val) {
eMBErrorCode errCodeRet = MB_ENOERR;

switch (funCode) {
case MODBUS_FUNCTION_WRITE_REGISTER: {
errCodeRet = eMBMWriteSingleRegister(xMBMMaster, slAddr, addReg, val);
}
break;

case MODBUS_FUNCTION_WRITE_COIL: {
errCodeRet = eMBMWriteSingleCoil(xMBMMaster, slAddr, addReg, val);
}

case MODBUS_FUNCTION_WRITE_MULTIPLE_COILS: {

}
break;

case MODBUS_FUNCTION_WRITE_MULTIPLE_REGISTERS: {

}
break;

default:
errCodeRet = MB_EINVAL;
break;
}

return errCodeRet;
}
94 changes: 94 additions & 0 deletions application/sources/app/app_modbus_pull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#ifndef __APP_MODBUS_PULL_H
#define __APP_MODBUS_PULL_H

#include <stdint.h>

#include "port.h"

#include "mbport.h"
#include "mbm.h"
#include "mbtypes.h"
#include "mbportlayer.h"

/*----------------------------------------------------------------------------*
* DECLARE: Common definitions
* Note:
*----------------------------------------------------------------------------*/
#define REG_VAL_DEFAULT (0xFFFF)
#define MB_READ_FAILED_RETRY_MAX ( 3 )

/* Enumarics -----------------------------------------------------------------*/
enum eMB_FuncCode {
MODBUS_FUNCTION_NONE = 0,
MODBUS_FUNCTION_READ_COILS = 1,
MODBUS_FUNCTION_READ_DISCRETE_INPUT = 2,
MODBUS_FUNCTION_READ_REGISTERS = 3,
MODBUS_FUNCTION_READ_INPUT_REGISTER = 4,
MODBUS_FUNCTION_WRITE_COIL = 5,
MODBUS_FUNCTION_WRITE_REGISTER = 6,
MODBUS_FUNCTION_WRITE_MULTIPLE_COILS = 15,
MODBUS_FUNCTION_WRITE_MULTIPLE_REGISTERS = 16
};

enum eMB_DataType {
UTF8 = 1, /* Alphanumeric */
INT16, /* Signed Integer, 16 bits */
INT16U, /* Unsigned Integer, 16 bits */
INT32L, /* Signed Integer, 32 bits, Little Endian */
INT32UL, /* Unsigned Integer, 32 bits, Little Endian */
INT64L, /* Signed Integer, 64 bits, Little Endian */
FLOAT32, /* Floating Point, 32 bits */
FLOAT64, /* Floating Point, 64 bits */
BITMAP,
DATETIME, /* DateTime */
DATE, /* Date */
TIME, /* Time */
PORTAL,
FQ_FP_PF, /* Four Quadrant Floating Point Power Factor */
INT16U10, /* Unsigned Integer, 16 bits */
INT16U256, /* Unsigned Integer, 16 bits */
INT32B, /* Signed Integer, 32 bits, Big Endian */
INT32UB, /* Unsigned Integer, 32 bits, Big Endian */
INT64UL, /* Signed Integer, 64 bits, Little Endian */
INT64B, /* Signed Integer, 64 bits, Big Endian */
INT64UB, /* Unsigned Integer, 64 bits, Big Endian */
INT16U100, /* Unsigned Integer, 16 bits */
INT16Ux10, /* Unsigned Integer, 16 bits */
INT16Ux100, /* Unsigned Integer, 16 bits */
INT16100, /* Signed Integer, 16 bits */
INT1610, /* Signed Integer, 16 bits */
INT8U, /* Unsigned Integer, 8 bits */
INT8U10, /* Unsigned Integer, 8 bits */
INT8U40, /* Unsigned Integer, 8 bits */
INT16UL10, /* Unsigned Integer, 16 bits, Little Endian */
INT32UL3600000,
INT16U1000
};

/* Typedef -------------------------------------------------------------------*/
typedef struct RegisterDeviceStructure {
USHORT regAddress;
ULONG regValue;
uint8_t funcCode;
double ratio;
const int8_t* unit;
uint8_t typeData;
bool isView;
const int8_t* regDescribe;
} __AK_PACKETED MB_DevRegStruct_t;

typedef struct ModbusDeviceStructure {
uint8_t tId;
ULONG tBaud;
MB_DevRegStruct_t *listRegDevice;
uint16_t listRegAmount;
} __AK_PACKETED MB_DeviceStruct_t;

/* Extern variables ----------------------------------------------------------*/
extern MB_DeviceStruct_t MB_ES35SW_TH_Sensor; /* https://epcb.vn/products/cam-bien-nhiet-do-do-am-rs485-modbus-rtu-es35-sw */
extern MB_DeviceStruct_t MB_LHIO404_IO_Device; /* https://epcb.vn/products/relay-4-kenh-dau-ra-io-giao-tiep-rs485-rs232-cong-nghiep-lh-io-01 */

/* Function prototypes -------------------------------------------------------*/
extern void updateDataModbusDevice(MB_DeviceStruct_t *mbDevice);

#endif
2 changes: 2 additions & 0 deletions application/sources/app/screens/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ CPPFLAGS += -I./sources/app/screens
VPATH += sources/app/screens

# CPP source files
SOURCES_CPP += sources/app/screens/scr_es35sw_th_sensor.cpp
SOURCES_CPP += sources/app/screens/scr_idle.cpp
SOURCES_CPP += sources/app/screens/scr_info.cpp
SOURCES_CPP += sources/app/screens/scr_lhio404_io_device.cpp
SOURCES_CPP += sources/app/screens/scr_noen.cpp
SOURCES_CPP += sources/app/screens/scr_startup.cpp
Loading

0 comments on commit a1906d3

Please sign in to comment.