MB_FileWr()
MB_FileWr()
is called when a Modbus master sends a Function Code 21 command. MB_FileWr()
writes a single integer value to a file. As mentionned in the Modbus specifications, a file is an organization of records. Each file can contain up to 10,000 records (addressed from 0 to 9999). You must 'map' the File/Record/Ix to the actual application's corresponding data. MB_FileWr()
should only be called by µC/Modbus.
Prototype
void MB_FileWr (CPU_INT16U file_nbr,
CPU_INT16U record_nbr,
CPU_INT16U ix,
CPU_INT08U record_len,
CPU_INT16U val,
CPU_INT16U *
perr)
Arguments
file_nbr
Is the number of the desired file.
record_nbr
Is the desired record within the file, a number between 0 and 9999.
ix
Is the desired entry in the specified record.
record_len
Is the total length of the record.
val
Is the value to write to the file/record.
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
the specified file/record/entry is valid and your code is returning its current value.
MODBUS_ERR_FILE
if the specified file_nbr
is not a valid file number in your product.
MODBUS_ERR_RECORD
if the specified record_nbr
is not a valid record number in the specified file.
MODBUS_ERR_IX
if the specified ix
is not a valid index into the specified record.
Returned Value
None.
Notes / Warnings
Code is enabled when MODBUS_FC21_EN
is set to DEF_ENABLED
in your product’s mb_cfg.h
file.
Called By:
MBS_FC21_FileWr()
in mbs_core.c
Example
In this example, we have two ‘files’ that we implemented as an array of 16-bit integers.
#define APP_MAX_FILES 2 #define APP_FILE_MAX_RECORDS 10 #define APP_FILE_MAX_VALUES 100 CPU_INT16U AppFile[APP_MAX_FILES][APP_FILE_MAX_RECORDS][APP_FILE_MAX_VALUES]; CPU_INT16U MB_FileWr (CPU_INT16U file_nbr, CPU_INT16U record_nbr, CPU_INT16U ix, CPU_INT08U record_len, CPU_INT16U val, CPU_INT16U *perr) { *perr = MODBUS_ERR_NONE; if (file_nbr >= APP_MAX_FILES) { *perr = MODBUS_ERR_FILE; return; } if (record_nbr >= APP_FILE_MAX_RECORDS) { *perr = MODBUS_ERR_RECORD; return; } if (ix >= APP_FILE_MAX_VALUES) { *perr = MODBUS_ERR_IX; return; } CPU_CRITICAL_ENTER(); AppFile[file_nbr][record_nbr][ix] = val; CPU_CRITICAL_EXIT(); }