[DllImport("dtl.dll", SetLastError=true)]
static extern public int DTL_READ_W(int name_id, byte[] data, ref uint iostat, int timeout);
Declare Function DTL_READ_W Lib "dtl.dll" (TODO) As TODO
None.
Do you know one? Please contribute it!
None.
Please add some!
namespace MyNamespace
{
/// <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 ReadFromPlc()
{
uint ioStat = 0;
bool done = false;
int dtlSts = DtlSuccess;
string msg = " ";
Array.Clear(_rawData, 0, _rawData.Length);
if (_defined)
{
for (int i = 0; i < RetryMax && !done; i++)
{
dtlSts = Dtl.DtlReadW(_nameId, _rawData, ref ioStat, TimeoutInMs);
if (DtlStatusOk("DtlReadW", dtlSts, ref msg, "", 0, true))
{
done = true;
}
}
}
else
{
msg = string.Format("{0}", "Dtl data file has not been defined");
_logger.Error(LoggerEvent.RockwellPlcError, msg);
throw new PlcException();
}
if (DtlStatusOk("DtlReadW", dtlSts, ref msg, "", 0, true))
{
DataCurrent = true;
return true;
}
return false;
}