using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
///I-8084W version 0.5.0.3, 2011/07/05
///I-8084W version 0.5.0.4, 2011/07/19
///Add pac_i8084W_GetFirmwareVersion
///Fix bug for 0.5.0.3 wrong dll import declaration
///[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetLowPassFilterUs")]
namespace pac8084WNet
{
public class pac8084W
{
private class ICPDAS
{
// for I/O version management
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_GetLibVersion")]
public static extern Int16 pac_i8084W_GetLibVersion();
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_GetFirmwareVersion")]
public static extern Int16 pac_i8084W_GetFirmwareVersion(int slot, ref Int16 firmware);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_GetLibDate")]
public static extern void pac_i8084W_GetLibDate(byte[] libDate);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_InitDriver")]
public static extern Int16 pac_i8084W_InitDriver(int Slot);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetChannelMode")]
public static extern Int16 pac_i8084W_SetChannelMode(int Slot, int Channel, Int16 Mode);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadCntABPhase")]
public static extern Int16 pac_i8084W_ReadCntABPhase(int Slot, int Channel, ref Int32 Cnt32U, ref Int32 Overflow);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadCntPulseDir")]
public static extern Int16 pac_i8084W_ReadCntPulseDir(int Slot, int Channel, ref Int32 Cnt32U, ref Int32 Overflow);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadCntUpDown")]
public static extern Int16 pac_i8084W_ReadCntUpDown(int Slot, int Channel, ref Int32 Cnt32U, ref Int32 Overflow);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadFreq")]
public static extern Int16 pac_i8084W_ReadFreq(int Slot, int Channel, ref UInt32 Freq);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadFreq_Float")]
public static extern Int16 pac_i8084W_ReadFreq_Float(int Slot, int Channel, ref float Freq);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadCntUp")]
public static extern Int16 pac_i8084W_ReadCntUp(int Slot, int Channel,ref UInt32 Cnt32U, ref Int32 OverFlow);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ClrCnt")]
public static extern Int16 pac_i8084W_ClrCnt(int Slot, int Channel);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_RecoverDefaultSetting")]
public static extern void pac_i8084W_RecoverDefaultSetting(int Slot);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadXorRegister")]
public static extern Int16 pac_i8084W_ReadXorRegister(int Slot, int Channel, ref Int32 XorReg);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetXorRegister")]
public static extern Int16 pac_i8084W_SetXorRegister(int Slot, int Channel, Int16 XorReg);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadChannelMode")]
public static extern Int16 pac_i8084W_ReadChannelMode(int Slot, int Channel, ref Int32 Mode);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadLowPassFilter_Us")]
public static extern Int16 pac_i8084W_ReadLowPassFilter_Us(int Slot, int Channel, ref UInt16 Us);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetLowPassFilter_Us")]
public static extern Int16 pac_i8084W_SetLowPassFilter_Us(int Slot, int Channel, UInt16 Us);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadLowPassFilter_Status")]
public static extern void pac_i8084W_ReadLowPassFilter_Status(int Slot, int Channel, ref Int32 Status);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetLowPassFilter_Status")]
public static extern void pac_i8084W_SetLowPassFilter_Status(int Slot,int Channel,Int16 Status);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadFreqMode")]
public static extern void pac_i8084W_ReadFreqMode(int Slot, int Channel, ref Int32 Mode);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetFreqMode")]
public static extern void pac_i8084W_SetFreqMode(int Slot, int Channel, Int16 Mode);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadFreqTimeoutValue")]
public static extern UInt16 pac_i8084W_ReadFreqTimeoutValue(int Slot, int Channel );
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetFreqTimeoutValue")]
public static extern void pac_i8084W_SetFreqTimeoutValue(int Slot, int Channel, UInt16 TimeOutValue );
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadDI_Xor")]
public static extern Int16 pac_i8084W_ReadDI_Xor(int Slot, ref Int32 DI);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_ReadDI_XorLPF")]
public static extern Int16 pac_i8084W_ReadDI_XorLPF(int Slot, ref Int32 DI);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_SetEdgeDetectMode")]
public static extern Int16 pac_i8084W_SetEdgeDetectMode(int Slot, int Channel, Int32 EdgeMode);
[DllImport("pac_i8084W.dll", EntryPoint = "pac_i8084W_GetEdgeDetectMode")]
public static extern Int16 pac_i8084W_GetEdgeDetectMode(int Slot, int Channel,ref Int32 EdgeMode);
}
///
/// Get the firmware version of I-8084W in the specified slot.
///
/// 1 ~ 7 (the slot which I-8084W is inserted)
/// firmware version
public static Int16 LibVersion()
{
return ICPDAS.pac_i8084W_GetLibVersion();
}
///
/// Get the library version of this DLL (pac_i8084W.dll)
///
/// library version
public static Int16 FirmwareVersion(int slot,ref Int16 firmware)
{
return ICPDAS.pac_i8084W_GetFirmwareVersion(slot, ref firmware);
}
///
/// Get the building date of this DLL (pac_i8084W.dll)
///
/// library building date
public static string LibDate()
{
//return Arr2Str(ICPDAS.pac_i8017H_GetLibDate());
byte[] libDate= new byte[32] ;
ICPDAS.pac_i8084W_GetLibDate(libDate);
//return libDate.Length.ToString();
//Console.WriteLine("libDate= {0} Len= {1}\n", AscArr2Str(libDate), libDate.Length.ToString());
return AscArr2Str(libDate);
}
///
/// Initialize the I-8084W in the specified slot
///
/// 1 ~ 7 (the slot which I-8084W is inserted)
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h
public static Int16 Init(int slot)
{
// Console.WriteLine("8084 Init dotnet {0}", slot);
return ICPDAS.pac_i8084W_InitDriver(slot);
}
public static Int16 SetLED(int slot, UInt32 led)
{
return 0;
}
private static string AscArr2Str(byte[] b)
{
return System.Text.UnicodeEncoding.Unicode.GetString(
System.Text.ASCIIEncoding.Convert(System.Text.Encoding.ASCII,
System.Text.Encoding.Unicode, b), 0, b.Length);
}
public static string Arr2Str(byte[] buffer)
{
return (new UnicodeEncoding()).GetString(buffer, 0, buffer.Length);
}
///
/// Set the operation mode of the specified channel of the I-8084W in the specified slot.
/// The original function in pac_i8084W.dll is SetChannelMode()
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// operation mode, including DirectionPulse, UpDown, Frequency, Up, Quadrant
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 SetChannelMode(int Slot, int Channel, Int16 Mode)
{
return ICPDAS.pac_i8084W_SetChannelMode(Slot, Channel, Mode);
}
///
/// Get the operation mode of the specified channel of the I-8084W in the specified slot.
/// The original function in pac_i8084W.dll is ReadChannelMode()
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// operation mode, including DirectionPulse, UpDown, Frequency, Up, Quadrant
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadChannelMode(int Slot, int Channel, ref Int16 Mode)
{
Int32 m = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadChannelMode(Slot, Channel, ref m);
Mode = (Int16)(m & 0xffff);
return ret;
}
///
/// Recover the default settings of the I-8084W in the specified slot.
/// Note: Default settings are as followings:
/// XOR register=0 (active high);
/// channel mode= 3 (Up counter mode);
/// Frequency operate mode = 0 (normal mode);
/// Low Pass Filter status = disable;
/// Low Pass Filter signal width = 1 us;
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
public static void RecoverDefaultSetting(int Slot)
{
ICPDAS.pac_i8084W_RecoverDefaultSetting( Slot);
}
///
/// Depreciated.
/// Use ReadCount() instead.
/// Read the counts of Dir/Pulse mode in the specified channel.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the counts (direction=0, negative counts; direction=1, positive counts)
/// the count status, overflow: 1; not overflow: 0
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadPulseDir(int Slot, int Channel,ref Int32 Cnt32U,ref Int16 Overflow)
{
Int32 o = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadCntPulseDir(Slot, Channel, ref Cnt32U, ref o);
Overflow = (Int16)(o & 0xffff);
return ret;
}
///
/// Depreciated.
/// Use ReadCount() instead.
/// Read the counts of Up/Down mode in the specified channel.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the counts read back
/// the count status, overflow: 1; not overflow: 0
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadUpDown(int Slot, int Channel,ref Int32 Cnt32U,ref Int16 Overflow)
{
Int32 o = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadCntUpDown( Slot, Channel,ref Cnt32U,ref o);
Overflow = (Int16)(o & 0xffff);
return ret;
}
///
/// Read the frequency in the specified channel of the I-8084W in the specified slot.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the frequency in the specified channel (unit: Hz)
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadFreq(int Slot, int Channel, ref UInt32 Freq)
{
return ICPDAS.pac_i8084W_ReadFreq(Slot, Channel, ref Freq);
}
public static Int16 ReadFreqFloat(int Slot, int Channel, ref float Freq)
{
return ICPDAS.pac_i8084W_ReadFreq_Float(Slot, Channel, ref Freq);
}
///
/// Depreciated.
/// Use ReadCount() instead.
/// Read the counts of Up mode of the specified channel.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the counts read back
/// the count status, overflow: 1; not overflow: 0
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadUpCnt(int Slot, int Channel,ref UInt32 Cnt32U, ref UInt16 OverFlow)
{
Int32 o = 0;
Int16 ret= ICPDAS.pac_i8084W_ReadCntUp(Slot, Channel,ref Cnt32U, ref o);
OverFlow = (UInt16)(o & 0xffff);
return ret;
}
///
/// Depreciated.
/// Use ReadCount() instead.
/// Read the counts of Quadrant mode in the specified channel.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the counts read back
/// the count status, overflow: 1; not overflow: 0
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadABPhase(int Slot, int Channel,ref Int32 Cnt32U,ref Int16 Overflow)
{
Int32 o = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadCntABPhase(Slot, Channel, ref Cnt32U, ref o);
Overflow = (Int16)(o & 0xffff);
return ret;
}
///
/// Clear the counts of the specified channel of I-8084W in the specified slot..
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static void ClrCnt(int Slot, int Channel)
{
ICPDAS.pac_i8084W_ClrCnt( Slot, Channel);
}
///
/// Depreciated.
/// Use ReadActiveState() instead.
/// Read XOR register to see that input signal as active low or active high.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 0: active high (signal: Low -> High, count changed); 1: active low
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadXor(int Slot, int Channel, ref Int16 XorReg)
{
Int32 x = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadXorRegister(Slot, Channel, ref x);
XorReg = (Int16)(x & 0xffff);
return ret;
}
///
/// Depreciated.
/// Use SetActiveState() instead.
/// Set XOR register to make input signal as active low or active high.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 0: active high (signal: Low -> High, count changed); 1: active low
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 SetXor(int Slot, int Channel, Int16 XorReg)
{
return ICPDAS.pac_i8084W_SetXorRegister(Slot, Channel, XorReg);
}
///
/// Read the width of low pass filter in the specified channel of the I-8084W in the specified slot.
/// I-8084W has only three independent 2nd-order digital noise filters to remove noises.
/// Channel 0/1 use the first; channel 2/3 use the second; channel 4/5/6/7 use the third.
/// Assume that (high level width of input signal) = x,
/// if x is less than us, then the input signal is filtered.
/// if x is greater than 2*us, then the input signal passes.
/// if x is between us and 2*us, then some passes and some be filtered.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 1~32767, pulse width, unit= 1us
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadLowPassFilter_Us(int Slot, int Channel, ref UInt16 Us)
{
return ICPDAS.pac_i8084W_ReadLowPassFilter_Us(Slot, Channel, ref Us);
}
///
/// Set the width of low pass filter in the specified channel of the I-8084W in the specified slot.
/// I-8084W has only three independent 2nd-order digital noise filters to remove noises.
/// Channel 0/1 use the first; channel 2/3 use the second; channel 4/5/6/7 use the third.
/// Assume that (high level width of input signal) = x,
/// if x is less than us, then the input signal is filtered.
/// if x is greater than 2*us, then the input signal passes.
/// if x is between us and 2*us, then some passes and some be filtered.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 1~32767, pulse width, unit= 1us
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 SetLowPassFilter_Us(int Slot, int Channel, UInt16 Us)
{
return ICPDAS.pac_i8084W_SetLowPassFilter_Us(Slot, Channel, Us);
}
///
/// Read the status of low pass filter in the specified channel of the I-8084W in the specified slot.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the status of low pass filter, Disabled or Enabled
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static void ReadLowPassFilter_Status(int Slot,int Channel,ref Int16 Status)
{
Int32 s = 0;
ICPDAS.pac_i8084W_ReadLowPassFilter_Status(Slot,Channel,ref s);
Status = (Int16)(s & 0xffff);
}
///
/// Set the status of low pass filter in the specified channel of the I-8084W in the specified slot.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the status of low pass filter, Disabled or Enabled
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static void SetLowPassFilter_Status(int Slot,int Channel,Int16 Status)
{
ICPDAS.pac_i8084W_SetLowPassFilter_Status(Slot,Channel,Status);
}
///
/// Read the frequency mode in the specified channel of the I-8084W in the specified slot.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the frequency mode in the specified channel, Normal or HighSpeed
public static void ReadFreqMode(int Slot, int Channel, ref Int16 Mode)
{
Int32 m = 0;
ICPDAS.pac_i8084W_ReadFreqMode(Slot, Channel, ref m);
Mode = (Int16)(m & 0xffff);
}
///
/// Set the frequency mode in the specified channel of the I-8084W in the specified slot.
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// the frequency mode in the specified channel, Normal or HighSpeed
public static void SetFreqMode(int Slot, int Channel, Int16 Mode)
{
ICPDAS.pac_i8084W_SetFreqMode(Slot,Channel,Mode);
}
///
/// Read the timeout value while measuring frequecny in the specified channel of the I-8084W
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// timeout value while measuring frequency (unit: ms)
public static UInt16 ReadFreqTimeoutValue(int Slot, int Channel )
{
return ICPDAS.pac_i8084W_ReadFreqTimeoutValue(Slot, Channel );
}
///
/// Set the timeout value while measuring frequecny in the specified channel of the I-8084W
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// timeout value while measuring frequency (unit: ms)
public static void SetFreqTimeoutValue(int Slot, int Channel, UInt16 TimeOutValue )
{
ICPDAS.pac_i8084W_SetFreqTimeoutValue(Slot, Channel, TimeOutValue );
}
///
/// Read the DI value after xor operation in the specified channel.
/// I-8084W uses xor operations to toggle active states of each channel.
/// The original function in pac_i8084W.dll is ReadDIXor().
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// the value of digital input which represents DI status of 8 channels
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadDI_Xor(int Slot, ref Int16 DI)
{
Int32 i = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadDI_Xor( Slot, ref i);
DI = (Int16)(i & 0xffff);
return ret;
}
///
/// Read the DI value after xor operation and Low pass filter in the specified channel.
/// I-8084W uses xor operations to toggle active states of each channel.
/// The original function in pac_i8084W.dll is ReadDIXorLPF().
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// the value of digital input which represents DI status of 8 channels
/// Error Code of the DLL, pac_i8084W.dll (refer to pac_i8084W.h)
public static Int16 ReadDI_XorLPF(int Slot,ref Int16 DI)
{
Int32 i = 0;
Int16 ret = ICPDAS.pac_i8084W_ReadDI_XorLPF(Slot, ref i);
DI = (Int16)(i & 0xffff);
return ret;
}
///
/// Get the detect mode for rising edge/falling edge
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 0: rising edge mode, 1: falling edge mode
public static void GetEdgeMode(int Slot, int Channel, ref Int16 Mode)
{
Int32 m = 0;
ICPDAS.pac_i8084W_GetEdgeDetectMode(Slot, Channel, ref m);
Mode = (Int16)(m & 0xffff);
}
///
/// Set the detect mode for rising edge/falling edge
///
/// 0 ~ 7 (the slot which I-8084W is inserted)
/// 0 ~ 7 (from where the input comes)
/// 0: rising edge mode, 1: falling edge mode
public static void SetEdgeMode(int Slot, int Channel, Int16 Mode)
{
ICPDAS.pac_i8084W_SetEdgeDetectMode(Slot, Channel, Mode);
}
}
}