MB_DIRd()
is called when a Modbus master sends a Function Code 2 command. MB_DIRd()
read the value of a single discrete input. MB_DIRd()
should only be called by µC/Modbus.
Prototype
CPU_BOOLEAN MB_DIRd (CPU_INT16U di,
CPU_INT16U
*
perr)
Arguments
di
Is the discrete input number that you want to read and can be a number between 0 and 65535 (depending on your product). It is up to you to decide which discrete input is assigned to what variable in your product.
perr
Is a pointer to a variable that will contain an error code based on the outcome of the call. Your code thus needs to return one of the following error codes:
MODBUS_ERR_NONE
if the discrete input number you specified is a valid discrete input and you are able to have code access the value of this discrete input.
MODBUS_ERR_RANGE
if the discrete input number passed as an argument is not a valid discrete input number for your product.
Returned Value
MB_DIRd()
returns the current value of the specified discrete input (TRUE
or FALSE
). If an invalid discrete input number is specified, you should return FALSE
.
Notes / Warnings
Code is enabled when MODBUS_CFG_FC02_EN
is set to DEF_ENABLED
in your product’s mb_cfg.h
file.
Called By:
MBS_FC02_DIRd()
in mbs_core.c
Example
In this example, our product has 19 discrete inputs. 16 of these discrete are placed in AppDITbl[]
by your application. The other three discrete inputs actually represent the status of three switches that your application reads and places the status into the following variables: AppSwStart
, AppSwStop
and AppSwReset
. A pressed switch is indicated by a TRUE
and a released switch is represented by a FALSE
.
Your systems Engineer decided to assign Modbus discrete input numbers 100, 101 and 102 to the three switches and the other discrete inputs to 103 through 118.
Code Block | ||
---|---|---|
| ||
CPU_BOOLEAN AppDITbl[16];
CPU_BOOLEAN AppSwStart;
CPU_BOOLEAN AppSwStop;
CPU_BOOLEAN AppSwReset;
CPU_BOOLEAN MB_DIRd (CPU_INT16U di, CPU_INT16U *perr)
{
*perr = MODBUS_ERR_NONE;
switch (di) {
case 100:
return (AppSwStart);
case 101:
return (AppSwStop);
case 102:
return (AppSwReset);
case 103:
case 104:
case 105:
case 106:
case 107:
case 108:
case 109:
case 110:
case 111:
case 112:
case 113:
case 114:
case 115:
case 116:
case 117:
case 118:
return (AppDITbl[di - 103]);
default:
*perr = MODBUS_ERR_RANGE;
return (FALSE);
}
} |