-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
623 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ®Val); | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.