esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

MODBUS-RTU.MAC
See other macros in category: All
Description:
    Macro functions to access slave MODBUS devices in RTU mode, which uses binary communication as opposed to ASCII in the file modbus.mac.
Documentation:
    DESCRIPTION
    This macro set provides functions to access remote devices using MODBUS protocol over serial lines using the binary (called RTU) mode.

    The following MODBUS functions are implemented and have been tested with a Eurotherm 2408 temperature controller. Further functions will have to be added as new instruments come along:
      • Function code | Function
      • 01 or 02      | Read n bits
      • 03 or 04      | Read n words
      • 05            | Write a bit
      • 06            | Write a word
      • 07            | Fast Read of Status
      • 08            | Loopback
      • 16            | Write n words
    The remote devices must behave as MODBUS slave nodes and have to be register with the modb_rtu_addnode() macro function before being accessed. This function returns a node number that must be used as the first parameter of the all the macro functions used to exchange data with the remote devices.
    Many of the input/output functions use an array to transfer data. This array can be a data array or an associative array. In the case of using a data array, the user is responsible of providing an array of the correct size to hold the data.

    When an error happens during the execution of a MODBUS function, the corresponding error is stored in the global variable MODB_ERR if it exists and the macro function returns the negative value -MODB_ERR. An error message is printed on the output terminal unless MODB_ERR is previously set to -1. If the function completes succesfully, MODB_ERR is set to 0.

    The error codes are the following:
      • 1001 - Bad device or interface
      • 1002 - Bad address
      • 1003 - Bad data parameters
      • 1004 - Not a registered slave node
      • 1005 - No answer from slave node
      • 1006 - Bad answer from slave node
      • 1007 - Function mismatch

        EXAMPLE
        myslave   = modb_rtu_addnode(3, 47)
        modb_rtu_read_status(myslave, data)
        This reads the status of the slave with MODBUS address 47 that is connected to the spec serial interface 3.
        $Revision: 1.5 $, $Date: 2009/03/11 13:06:06 $

Macros:
    modb_rtu_addnode
    Usage: modb_rtu_addnode (<device>, <maddr> [, <name>])
    Tries to connect to a MODBUS slave node. If the node is found, the macro function appends it to the internal list and returns a unique node number that must be used to access the slave with this macro set.
    The parameter <device> must be either the number of a serial line interface or an ESRF device name. The parameter <maddr> is required and must correspond to the MODBUS address of the node.
    If there is an error, the macro returns -MODB_ERR, and the node is not added to the internal list.


    modb_rtu_searchnode
    Usage: modb_rtu_searchnode (<device> [, <name>])
    Tries to find a MODBUS slave node. The macro will run through the MODBUS addresses starting with 1 going to 255. By the nature of things, higher addresses will take longer to be found. Once the node is found, the macro function appends it to the internal list and returns a unique node number that must be used to access the slave with this macro set.
    The parameter <device> must be either the number of a serial line interface or an ESRF device name.
    If there is an error, the macro returns -MODB_ERR, and the node is not added to the internal list.


    modb_rtu_searchparameters
    Usage: modb_rtu_searchparameters (<device>)
    Tries to work out the serial line parameters to communicate with a MODBUS slave node. The macro will run through the possible serial line parameters, which are 9600, 19200, 4800, 2400 and 1200 baud. At each speed, it will try to communicate at 8 data bits, then 7 data bits with even, then odd parity. By the nature of things, things are going to be slow. Once a communication is established, the macro will offer the result in printing.
    The parameter <device> must be either the number of a serial line interface or an ESRF device name.

    modb_rtu_read_status
    Usage: modb_rtu_read_status (<node>, <data>)
    This macro executes the function 7 "FAST READ OF STATUS" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). The 8-bit status is returned in the first element of the array <data>.
    If an error happens, the macro returns -MODB_ERR (see error codes), otherwise 1.


    modb_rtu_read_diagnostics
    Usage: modb_rtu_read_diagnostics (<node>, <subcode>, <dfield>, <data>)
    This macro executes the function 8 "DIAGNOSTIC LOOPBACK" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns the number of words actually read from the slave and stored in the first positions of the array <data>. Each valid position of <data> contains a 16-bit word. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_read_n_bits
    Usage: modb_rtu_read_n_bits (<node>, <address of first bit>, <number of bits>, <data>)
    This macro executes the function 1 or 2 "Read n bits" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns the number of bits actually read from the slave and stored in the first positions of the array <data>. Each valid position of <data> contains ONE bit of the answer. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_write_a_bit
    Usage: modb_rtu_write_a_bit (<node>, <address of bit>, <value of bit>, <data>)
    This macro executes the function 5 "write a bit" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns 1 If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_read_n_words
    Usage: modb_rtu_read_n_words (<node>, <address of first word>, <number of words>, <data>)
    This macro executes the function 3 or 4 "Read n words" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns the number of words actually read from the slave and stored in the first positions of the array <data>. Each valid position of <data> contains ONE bit of the answer. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_write_a_word
    Usage: modb_rtu_write_a_word (<node>, <address of word>, <value of word>, <data>)
    This macro executes the function 6 "Write a word" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns 1. Each valid position of <data> contains ONE bit of the answer. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_write_n_words
    Usage: modb_rtu_write_n_words (<node>, <address of first word>, <number of words>, <data>)
    This macro executes the function 16 "WRITE N WORDS" in a slave MODBUS node. The slave node is selected by the parameter <node> that must be a valid value previously returned by the macro function modb_rtu_addnode(). If no error happens, the macro function returns the number of words actually written to the slave. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_CRC
    Usage: modb_rtu_CRC (len)
    This macro calculates the CRC over the present MODBFRAME. Lenght is the argument. If an error happens, the macro returns -MODB_ERR (see error codes).


    modb_rtu_debug
    Usage: modb_rtu_debug


Internal Macros:
    modb_rtu__read
    Usage: modb_rtu__read (<node>, <daddr>, <nitems>, <data>, <function>)
    Executes any read data function in a slave MODBUS node.


    modb_rtu__float
    Usage: modb_rtu__float (<data>, <nitems>)
    Extracts <nitems> from the binary frame in MODBFRAME and stores them as float values in the array <data>.
    Tested only for function modb_rtu_read_n_words().
    The 2408 can be put into a floating point mode (read cell 12550) which then displays values with variable number of floating point numbers. To avoid getting in trouble with that, the protocol allows to read 4 bytes from adresses over 0x8000. Thus, the cell 2 reads from
    2 x 2 + 8000h = 8004h = 32772 decimal.


    modb_rtu__data
    Usage: modb_rtu__data (<data>, <nitems>)
    Extracts <nitems> data from the binary frame in MODBFRAME and stores them in the array <data>.


    modb_rtu__checkpar
    Usage: modb_rtu__checkpar (<nitems>, <data>)
    Checks that nitems is in the (1, 0xFFFF) range and that data is an array of the right capacity.


    modb_rtu__frame
    Usage: modb_rtu__frame (<node>, <funct>, <word1>, <word2>, <data>)
    Builds a modbus frame from the input parameters <funct>, <word1>, <word2> and <data>. The frame is stored in the global byte array MODBFRAME[] starting from the position 1.


    modb_rtu__put
    Usage: modb_rtu__put (<node>)
    Sends a message to a slave node and gets the answer back. The message is constructed from a valid modbus frame previously stored in the byte array MODBFRAME[]. The answer from the slave is stored also in MODBFRAME[], overwritting the initial content. The return value is the length of the answer frame or -MODB_ERR in case of error.


    modb_rtu__get
    Usage: modb_rtu__get(node, funct)


    modb_rtu__getlow
    Usage: modb_rtu__getlow(node)


    __modb_rtu__getdata
    Usage: __modb_rtu__getdata(node, funct)


    modb_rtu__chkstr
    Usage: modb_rtu__chkstr (<str>)
    Store the answer from the slave into MODBFRAME[].
    If the macro detects a MODBUS exception or a CRC error returns -MODB_ERR, otherwise returns the length of the answer frame.


    modb_rtu__chkanswer
    Usage: modb_rtu__chkanswer ()
    Checks the received frame for a MODBUS exception and returns the error code. If no exception, it returns the length of the answer frame.


Filename: modbus-rtu.mac
Author: P. Fajardo, (Original 5/01). RTU version based on PF modbus.mac: H. Witsch March 2008 $Revision: 1.5 $ / $Date: 2009/03/11 13:06:06 $
Last mod.: 11/03/2009 14:06 by witsch