DTL_C_DEFINE (dtl)
Last changed: -71.87.24.210

.
Summary
TODO - a short description

C# Signature:

[DllImport("dtl.dll", SetLastError=true)]
static extern public int DTL_C_DEFINE(ref int name_id, StringBuilder def);

VB Signature:

Declare Function DTL_C_DEFINE Lib "dtl.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Dtl.dll functions are not thread safe. Multiple instances need to be in separate processes.

Tips & Tricks:

Please add some!

Sample Code:

    /// <summary>
    /// Class dtl has all the p/invoke routines and
    /// error constants for the Rockwell DTL C sdk
    /// interface.
    /// </summary>
    public static class Dtl
    {
    /// <summary>
    /// Close Dtl driver
    /// </summary>
    /// <param name="driverId">specifies driver to close</param>
    /// <param name="timeOut">time out in milliseconds</param>
    /// <returns>dtl status</returns>
    public static int DtlDriverClose(int driverId, int timeOut)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_DRIVER_CLOSE(driverId, timeOut));
    }

    /// <summary>
    /// Uninitialize Dtl driver
    /// </summary>
    /// <param name="driverId">driver to uninitialize</param>
    public static void DtlUninit(int driverId)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        UnsafeNativeMethods.DTL_UNINIT(driverId);
    }

    /// <summary>
    /// Initialize Dtl driver
    /// </summary>
    /// <param name="tableSize">number of entries in the definition table</param>
    /// <returns>dtl status</returns>
    public static int DtlInit(int tableSize)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_INIT(tableSize));
    }

    /// <summary>
    /// Returns message associated dtl status
    /// </summary>
    /// <param name="dtlSts">dtl status to lookup</param>
    /// <param name="msg">message corresponding to dtl status </param>
    /// <param name="buffSize">size of buffer that will hold message</param>
    public static void DtlErrorS(int dtlSts, StringBuilder msg, int buffSize)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        UnsafeNativeMethods.DTL_ERROR_S(dtlSts, msg, buffSize);
    }

    /// <summary>
    /// Open Dtl Driver
    /// </summary>
    /// <param name="driverId">driver id to assign to this driver</param>
    /// <param name="driverName">name of the driver </param>
    /// <param name="timeOut">timeout in milliseconds</param>
    /// <returns>dtl status</returns>
    public static int DtlDriverOpen(int driverId, string driverName, int timeOut)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_DRIVER_OPEN(driverId, driverName, timeOut));
    }

    /// <summary>
    /// Get version of dtl dll
    /// </summary>
    /// <param name="versionText">string to hold returned text</param>
    /// <param name="maxCharsToReturn">size of string to hold version text</param>
    /// <returns>dtl status</returns>
    public static int DtlVersion(StringBuilder versionText, int maxCharsToReturn)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_VERSION(versionText, maxCharsToReturn));
    }

    /// <summary>
    /// Define a tag
    /// </summary>
    /// <param name="nameId">name id assigned to the tag</param>
    /// <param name="defString">string the defines the tag and its data</param>
    /// <returns>dtl status</returns>
    public static int DtlCDefine(ref int nameId, StringBuilder defString)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_C_DEFINE(ref nameId, defString));
    }

    /// <summary>
    /// Undefine a dtl tag
    /// </summary>
    /// <param name="nameId">name id of the tag</param>
    /// <returns>dtl status</returns>
    public static int DtlUndef(int nameId)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_UNDEF(nameId));
    }

    /// <summary>
    /// Read data from a dtl tag
    /// </summary>
    /// <param name="nameId">name id of the tag</param>
    /// <param name="readData">byte buffer to hold the data</param>
    /// <param name="ioStat">io status of the read operation</param>
    /// <param name="timeOut">timeout in milliseconds</param>
    /// <returns>dtl status</returns>
    public static int DtlReadW(int nameId, byte[] readData, ref uint ioStat, int timeOut)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_READ_W(nameId, readData, ref ioStat, timeOut));
    }

    /// <summary>
    /// Write to a dtl tag
    /// </summary>
    /// <param name="nameId">name id of the tag</param>
    /// <param name="writeData">data buffer to write from</param>
    /// <param name="ioStat">io status of the write operation</param>
    /// <param name="timeOut">timeout in milliseconds</param>
    /// <returns>dtl status</returns>
    public static int DtlWriteW(int nameId, byte[] writeData, ref uint ioStat, int timeOut)
    {
        new UIPermission(UIPermissionWindow.AllWindows).Demand();
        return (UnsafeNativeMethods.DTL_WRITE_W(nameId, writeData, ref ioStat, timeOut));
    }

    /// <summary>
    /// Contains unsafe dtl dll calls
    /// </summary>
    [SuppressUnmanagedCodeSecurityAttribute]
    internal static class UnsafeNativeMethods
    {
        [DllImport("Dtl32.dll")]
        static extern public int DTL_DRIVER_CLOSE(int driver_id, int timeout);

        [DllImport("Dtl32.dll")]
        static extern public void DTL_UNINIT(int driver_id);

        [DllImport("Dtl32.dll")]
        static extern public int DTL_INIT(int table_size);

        [DllImport("Dtl32.dll")]
        static extern public void DTL_ERROR_S(int iostat, StringBuilder msg, int buffsz);

        [DllImport("Dtl32.dll")]
        static extern public int DTL_DRIVER_OPEN(int driver_id, string szDriverName, int timeout);

        [DllImport("DTL32.DLL")]
        static extern public int DTL_VERSION(StringBuilder sb, int maxChars);

        [DllImport("DTL32.DLL")]
        static extern public int DTL_C_DEFINE(ref int name_id, StringBuilder def);

        [DllImport("DTL32.DLL")]
        static extern public int DTL_UNDEF(int name_id);

        [DllImport("DTL32.DLL")]
        static extern public int DTL_READ_W(int name_id, byte[] data, ref uint iostat, int timeout);

        [DllImport("DTL32.DLL")]
        static extern public int DTL_WRITE_W(int name_id, byte[] data, ref uint iostat, int timeout);
    }
    }

    bool DtlStatusOk(string dtlFunction, int dtlSts, ref string errorText, string address = null, int ioStat = 0, bool logIt = false)
    {
        var msg = new StringBuilder(256);

        if (dtlSts != DtlSuccess)
        {
        if (ioStat == 0)
        {
            ioStat = dtlSts;
        }

        Dtl.DtlErrorS(ioStat, msg, msg.MaxCapacity);

        if (address != null)
        {
            errorText = dtlFunction + ": " + address + ' ' + msg;
        }

        else
        {
            errorText = dtlFunction + ": " + msg;
        }

        if (logIt)
        {
            _logger.Warn(LoggerEvent.RockwellPlcError, msg.ToString());
        }

        return (false);
        }

        return (true);
    }

    public void DefineData(string address, int numberOfBytes, bool isWriteable)
    {
        var definition = new StringBuilder(256);
        string slot = " ";
        string msgTxt = " ";
        int hops = 0;
        bool ok = true;

        if (numberOfBytes <= MaxTagSize)
        {
        _address = address;
        _sizeOfTag = numberOfBytes;
        _elements = _sizeOfTag / 2;
        _writeable = isWriteable;
        _defined = false;

        _type = "WORD";

        _access = _writeable ? "MODIFY" : "READ";

        switch (_plcType)
        {
            case 5:

            _procType = "PLC5";
            _portId = "AB:LOCAL";
            _wordSize = 2;

            switch (_driverMethod)
            {
                case 2:
                slot = Convert.ToString(_nodeAddress, 8); // octal
                break;

                case 3:
                slot = _ipAddress;
                _portId = string.Format("AB:ASA/L:{0:d}", _nodeAddress);
                break;

                default:
                slot = string.Format("{0:d}", _nodeAddress);
                break;
            }

            break;

            case 500:

            _procType = "SLC500";
            _portId = "AB:LOCAL";
            _wordSize = 2;

            switch (_driverMethod)
            {
                case 2:
                slot = Convert.ToString(_nodeAddress, 8); // octal
                break;

                default:
                slot = Convert.ToString(_nodeAddress);
                break;

            }

            break;

            case 5000:

            _procType = "PLC5";
            _portId = "AB:CIP";
            _wordSize = 4;

            switch (_driverMethod)
            {
                case 1:
                slot = string.Format("{0:d}.{1:d}", 1, _nodeAddress);
                break;

                case 2:
                string decip = ConvertIp(_ipAddress, ref hops);
                slot = string.Format("{0:d}.{1:d}.{2}.{3:d}.{4:d}", 16, hops, decip, 1, _nodeAddress);
                break;

                default:
                ok = false;
                break;
            }

            break;

            default:

            _procType = "NOTDEFINED";
            _portId = "NOTDEFINED";
            ok = false;

            break;
        }

        if (ok)
        {
            definition.Append(string.Format("${0},{1:d},{2},{3},{4},{5},{6},{7:d}",
                            _address,
                            _elements,
                            _type,
                            _access,
                            _portId,
                            slot,
                            _procType,
                            _driverId));

            _maxWordNo = (_sizeOfTag/_wordSize) - 1;
            _nameId = 0;
            int dtlSts = Dtl.DtlCDefine(ref _nameId, definition);

            if (DtlStatusOk("DtlCDefine", dtlSts, ref msgTxt, "", 0, true))
            {
            _defined = true;
            }

            else
            {
            _logger.Error(LoggerEvent.RockwellPlcError, msgTxt);
            throw new PlcException();
            }
        }

        else
        {
            string msg = string.Format("{0}", "Cannot define data, check configuration");
            _logger.Error(LoggerEvent.RockwellPlcError, msg);
            throw new PlcException();    
        }
        }

        else
        {
        string msg = string.Format("{0}", "Data file size passed is too large");
        _logger.Error(LoggerEvent.RockwellPlcError, msg);
        throw new PlcException();
        }
    }

Documentation