DTL_WRITE_W (dtl)
Last changed: -71.87.24.210

.
Summary
TODO - a short description

C# Signature:

[DllImport("dtl.dll", SetLastError=true)]
static extern public int DTL_WRITE_W(int name_id, byte[] data, ref uint iostat, int timeout);

VB Signature:

Declare Function DTL_WRITE_W 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 override bool WriteToPlc()
    {
        bool ok = true;
        uint writeIoStat = 0;
        uint readIoStat = 0;
        int dtlSts = DtlSuccess;
        string msg = " ";
        var readBackData = new byte[_rawData.Length];
        var currentData = new byte[_rawData.Length];
        bool readBackOk = false;
        bool currentDataWasRead = false;
        bool done = false;

        Array.Copy(_rawData, readBackData, readBackData.Length);
        Array.Clear(currentData, 0, currentData.Length);

        if (_defined)
        {
        if (_writeable)
        {

            for (int i = 0; i < RetryMax; i++)
            {
            dtlSts = Dtl.DtlReadW(_nameId, currentData, ref readIoStat, TimeoutInMs);

            if (DtlStatusOk("DtlReadW", dtlSts, ref msg, "", 0, true))
            {
                currentDataWasRead = true;
                break;
            }
            }

            if (currentDataWasRead)
            {
            bool writeNeeded;

            if (_disableChangeMask)
            {
                Array.Copy(_rawData, currentData, currentData.Length);
                writeNeeded = true;
            }

            else
            {
                writeNeeded = MoveWithMask(_rawData, _changeMask, ref currentData );
            }

            if (writeNeeded)
            {
                for (int i = 0; i < RetryMax && !done; i++)
                {
                dtlSts = Dtl.DtlWriteW(_nameId, currentData, ref writeIoStat, TimeoutInMs);

                if (DtlStatusOk("DtlWriteW", dtlSts, ref msg, "", 0, true))
                {
                    dtlSts = Dtl.DtlReadW(_nameId, readBackData, ref readIoStat, TimeoutInMs);

                    if (DtlStatusOk("DtlReadW", dtlSts, ref msg, "", 0, true))
                    {
                    readBackOk = true;

                    for (int j = 0; j < _rawData.Length && readBackOk; j++)
                    {
                        if (currentData[j] != readBackData[j])
                        {
                        readBackOk = false;
                        }
                    }

                    if (readBackOk)
                    {
                        done = true;
                    }
                    }
                }
                }

                if (DtlStatusOk("DtlReadW", dtlSts, ref msg, "", 0, true))
                {
                if (readBackOk)
                {
                    DataCurrent = false;
                }

                else
                {
                    ok = false;
                }
                }

                else
                {
                ok = false;
                }
            }
            }
        }

        else
        {
            msg = string.Format("{0}", "Attempt to write to read only data file");
            _logger.Error(LoggerEvent.RockwellPlcError, msg);
            throw new PlcException();
        }

        if (ok)
        {
            _changesPending = false;
            Array.Clear(_changeMask, 0, _changeMask.Length);
        }
        }

        else
        {
        msg = string.Format("{0}", "Dtl data file has not been defined");
        _logger.Error(LoggerEvent.RockwellPlcError, msg);
        throw new PlcException();
        }

        return ok;
    }

Documentation
DTL_WRITE_W on MSDN