-
Notifications
You must be signed in to change notification settings - Fork 9
/
mb-check.c
101 lines (88 loc) · 2.98 KB
/
mb-check.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
MODBUS Check Data and Addres
By Liyanboy74
https://github.com/liyanboy74/modbus
*/
#include "mb-check.h"
#include "mb-table.h"
#if(MB_MODE==MB_MODE_SLAVE)
mb_error_e mb_check_func(uint8_t func)
{
switch (func)
{
#if MB_ENABLE_FUNC_Read_Coils
case MB_FUNC_Read_Coils:break;
#endif
#if MB_ENABLE_FUNC_Read_Discrete_Inputs
case MB_FUNC_Read_Discrete_Inputs:break;
#endif
#if MB_ENABLE_FUNC_Read_Holding_Registers
case MB_FUNC_Read_Holding_Registers:break;
#endif
#if MB_ENABLE_FUNC_Read_Input_Registers
case MB_FUNC_Read_Input_Registers:break;
#endif
#if MB_ENABLE_FUNC_Write_Single_Coil
case MB_FUNC_Write_Single_Coil:break;
#endif
#if MB_ENABLE_FUNC_Write_Single_Register
case MB_FUNC_Write_Single_Register:break;
#endif
#if MB_ENABLE_FUNC_Write_Multiple_Coils
case MB_FUNC_Write_Multiple_Coils:break;
#endif
#if MB_ENABLE_FUNC_Write_Multiple_Registers
case MB_FUNC_Write_Multiple_Registers:break;
#endif
default:return MB_ERROR_ILLEGAL_FUNCTION;
}
return MB_OK;
}
mb_error_e mb_check_quantity(uint16_t Quantity)
{
if(Quantity>=MB_MIN_QUANTITY&&Quantity<=MB_MAX_QUANTITY)return MB_OK;
return MB_ERROR_ILLEGAL_DATA_VALUE;
}
mb_error_e mb_check_quantity_bit_n_byte(uint16_t Quantity,uint8_t N)
{
uint8_t n;
n=Quantity/8;
if(Quantity%8)n++;
if(n!=N)return MB_ERROR_ILLEGAL_DATA_VALUE;
return MB_OK;
}
mb_error_e mb_check_quantity_reg_n_byte(uint16_t Quantity,uint8_t N)
{
if((Quantity*2)!=N)return MB_ERROR_ILLEGAL_DATA_VALUE;
return MB_OK;
}
mb_error_e mb_check_table_coils_address(uint16_t StartAd,uint16_t Quantity)
{
if(StartAd>=(TBALE_Coils_Size*TABLE_Sel_BitSize))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
if((StartAd+Quantity)>(TBALE_Coils_Size*TABLE_Sel_BitSize))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
return MB_OK;
}
mb_error_e mb_check_table_discretes_input_address(uint16_t StartAd,uint16_t Quantity)
{
if(StartAd>=(TBALE_Discretes_Input_Size*TABLE_Sel_BitSize))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
if((StartAd+Quantity)>(TBALE_Discretes_Input_Size*TABLE_Sel_BitSize))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
return MB_OK;
}
mb_error_e mb_check_table_input_registers_address(uint16_t StartAd,uint16_t Quantity)
{
if(StartAd>=(TBALE_Input_Registers_Size))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
if((StartAd+Quantity)>(TBALE_Input_Registers_Size))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
return MB_OK;
}
mb_error_e mb_check_table_holding_registers_address(uint16_t StartAd,uint16_t Quantity)
{
if(StartAd>=(TABLE_Holding_Registers_Size))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
if((StartAd+Quantity)>(TABLE_Holding_Registers_Size))return MB_ERROR_ILLEGAL_DATA_ADDRESS;
return MB_OK;
}
mb_error_e mb_check_on_off(uint16_t Value)
{
if(Value != MB_COIL_ON && Value != MB_COIL_OFF) return MB_ERROR_ILLEGAL_DATA_VALUE;
return MB_OK;
}
#endif