MODBUS Communications Driver
Calling the Driver from C is straightforward. Each MODBUS command has its own function call within the Driver Dynamic Link Library.
To be able to call the Driver, you must add its import library to your C or C++ project. The import library is called MBDRVDLL.LIB. This file tells the C linker which functions are contained in the Driver DLL and how to find them.
In Microsoft Visual C++, you can add the import library by selecting the “Project / Add to Project / Files” command, changing the “Files of type” selection to “Library files (.lib),” and then browsing to MBDRVDLL.LIB.
You must also include the Driver DLL header file to be able to call Driver functions. This header file, called MBDRVDLL.H, declares all the functions in the DLL. This is normally done with a directive like:
at the top of the C file.
The above steps will make the Driver DLL functions callable from within your program.
A typical C Driver call
Most MODBUS Driver calls follow the same general form. Calls that cause a MODBUS command to be sent will include a MODBUS address and one or more parameters:
status = MB_ReadOutputStatus(Address, Start, Count, Values);
This command will read the status of digital outputs (coils) on the MODBUS device. The Address parameter specifies the MODBUS device’s address on the network (a number from 1 to 255). The Start parameter indicates the first coil to be read, and the Count parameter tells the Driver how many coils to read. Lastly, the Values parameter is an array that will contain the coil states retrieved from the MODBUS device.
Most MODBUS Driver DLL commands return a status code (assigned to the variable status in he above example). This will be zero if the function completed successfully or nonzero in the event of an error. Error codes can be interpreted from the table below, or you can get a text description using the MB_ErrorString function.
June 6, 1998