[DllImport("dtl.dll", SetLastError=true)]
static extern public int DTL_INIT(int table_size);
Declare Function DTL_INIT 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);
}
}
}
protected void OpenDriver(string driverName, int timeoutFactor)
{
int dtlSts;
string msg = " ";
//Look for drivername in list
bool driverAlreadyOpened = _driverLookupList.TryGetValue(_driverName, out _driverId);
//Initialize DTL
if (!_dtlInitialized)
{
dtlSts = Dtl.DtlInit(TableSize);
if (DtlStatusOk("DtlInit", dtlSts, ref msg, " "))
{
_dtlInitialized = true;
}
else
{
_logger.Error(LoggerEvent.RockwellPlcError, msg);
throw new PlcException();
}
}
//Open driver
if (!driverAlreadyOpened)
{
if (_dtlInitialized)
{
_driverId = _nextDriverId;
dtlSts = Dtl.DtlDriverOpen(_driverId, _driverName, _timeoutFactor * 3);
if (DtlStatusOk("DtlDriverOpen", dtlSts, ref msg, " ", 0, true))
{
_driverLookupList[_driverName] = _driverId;
int highestId = -1;
foreach (KeyValuePair<string, int> id in _driverLookupList)
{
if (id.Value > highestId)
{
_nextDriverId = id.Value + 1;
highestId = id.Value;
}
}
}
else
{
_logger.Error(LoggerEvent.RockwellPlcError, msg);
throw new PlcException();
}
}
}
}