using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.Text; ///I-8014W version 1.0.0.8, 2011/07/05 namespace pac8014WNet { public enum ErrorCode { NoError = (Int16)0, ID_ERROR = (Int16)(-1), SLOT_ERROR = (Int16)(-2), CHANNEL_ERROR = (Int16)(-3), GAIN_ERROR = (Int16)(-4), FIFO_EMPTY = (Int16)(-5), FIFO_LATCHED = (Int16)(-6), FIFO_OVERFLOW = (Int16)(-7), TX_NOTREADY = (Int16)(-8), MAX_FIFO = (Int16)4096 } public class pac8014W { public delegate void IsrFunction(int slot); private class ICPDAS { [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_Init")] public static extern Int16 pac_i8014W_Init(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_GetLibVersion")] public static extern Int16 pac_i8014W_GetLibVersion(); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_GetLibDate")] public static extern void pac_i8014W_GetLibDate(byte[] libDate); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_GetFirmwareVer_L1")] public static extern Int16 pac_i8014W_GetFirmwareVer_L1(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_GetFirmwareVer_L2")] public static extern Int16 pac_i8014W_GetFirmwareVer_L2(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_GetSingleEndJumper")] public static extern Int16 pac_i8014W_GetSingleEndJumper(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_SelectedImpedency")] public static extern void pac_i8014W_SelectedImpedency(Int16 slot,Int16 resistorType); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ReadGainOffset")] public static extern void pac_i8014W_ReadGainOffset(Int16 slot,Int16 gain, ref UInt16 gainValue,ref Int16 offsetValue); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ReadAIHex")] public static extern Int16 pac_i8014W_ReadAIHex(Int16 slot,Int16 ch,Int16 gain, ref Int16 hVal); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ReadAI")] public static extern Int16 pac_i8014W_ReadAI(Int16 slot,Int16 ch,Int16 gain, ref float fVal); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ClearFIFO")] public static extern void pac_i8014W_ClearFIFO(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_UnLockFIFO")] public static extern void pac_i8014W_UnLockFIFO(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ConfigMagicScan")] public static extern void pac_i8014W_ConfigMagicScan(Int16 slot,Int16[] chArr,Int16[] gainArr,Int16 scanChCount,float sampleRate,Int16 scanMode,Int16 triggerSource,Int16 triggerState,ref float realSampleRate); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_StartMagicScan")] public static extern Int16 pac_i8014W_StartMagicScan(Int16 slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_StopMagicScan")] public static extern Int16 pac_i8014W_StopMagicScan(Int16 slot); //[DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ReadFIFO")] //unsafe public static extern Int16 pac_i8014W_ReadFIFO(Int16 slot, short* hexData, Int16 readCount, short* dataCountFromFIFO);// <=== get data from FIFO ,long* dataCnt [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ReadFIFO")] public static extern Int16 pac_i8014W_ReadFIFO(Int16 slot, Int16[] hexData, Int16 readCount, ref Int16 dataCountFromFIFO);// <=== get data from FIFO ,long* dataCnt [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_CalibrateDataHex")] public static extern void pac_i8014W_CalibrateDataHex(Int16 slot, Int16 iGain,Int16 dataFromFIFO,ref Int16 calibratedAI); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_CalibrateData")] public static extern void pac_i8014W_CalibrateData(Int16 slot, Int16 iGain,Int16 dataFromFIFO,ref float calibratedAI); // public delegate void IsrFunction(int slot); [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_InstallMagicScanISR")] public static extern Int16 pac_i8014W_InstallMagicScanISR(Int16 slot, IsrFunction isr, Int16 triggerLevel); //<=== give a callback function for interrupt [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_UnInstallMagicScanISR")] public static extern Int16 pac_i8014W_UnInstallMagicScanISR(Int16 slot); //<=== give a callback function for interrupt [DllImport("pac_i8014W.dll", EntryPoint = "pac_i8014W_ClearInt")] public static extern void pac_i8014W_ClearInt(Int16 slot); } /// /// Get the library version of this DLL (pac_i8014W.dll) /// /// library version public static Int16 LibVersion() { return ICPDAS.pac_i8014W_GetLibVersion(); } /// /// Get the building date of this DLL (pac_i8014W.dll) /// /// library building date public static string LibDate() { byte[] libDate = new byte[32]; ICPDAS.pac_i8014W_GetLibDate(libDate); return AscArr2Str(libDate); } /// /// Initialize the I-8014W in the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static Int16 Init(Int16 slot) { return ICPDAS.pac_i8014W_Init(slot); } /// /// Get the first firmware version of the I-8014W in the specified slot /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// the read-back firmware version /// 0 = SUCCESS. -1 = ERROR. public static Int16 FirmwareVer_L1(Int16 slot) { return ICPDAS.pac_i8014W_GetFirmwareVer_L1(slot); } /// /// Get the second firmware version of the I-8014W in the specified slot /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// the read-back firmware version /// 0 = SUCCESS. -1 = ERROR. public static Int16 FirmwareVer_L2(Int16 slot) { return ICPDAS.pac_i8014W_GetFirmwareVer_L2(slot); } /// /// Get the type of the input channels; differential or single-ended /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// jumper status of input channel type; Differential or SingleEnded /// 0 = SUCCESS. -1 = ERROR. public static Int16 SingleEndJumper(Int16 slot) { return ICPDAS.pac_i8014W_GetSingleEndJumper(slot); } public static void SelectedImpedency(Int16 slot, Int16 resistorType) { ICPDAS.pac_i8014W_SelectedImpedency( slot, resistorType); } /// /// Get the gain value and the offset for a certain gain of I-8014W in the specified slot /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// the gain which is defined by the enum Gain /// the calibated gain value /// the calibated offset value /// 0 = SUCCESS. -1 = ERROR. public static void ReadGainOffset(Int16 slot, Int16 gain, ref UInt16 gainValue, ref Int16 offsetValue) { ICPDAS.pac_i8014W_ReadGainOffset( slot, gain, ref gainValue, ref offsetValue); } /// /// Read the analog input value in the hex format from the specified channel /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// 0 ~ 7 if Differential mode; 0 ~ 15 if SingleEnded mode /// the gain which is defined by the enum Gain /// the read back value in hexadecimal format /// 0 = SUCCESS. -1 = ERROR. public static Int16 ReadAIHex(Int16 slot, Int16 ch, Int16 gain, ref Int16 hVal) { return ICPDAS.pac_i8014W_ReadAIHex(slot, ch, gain, ref hVal); } /// /// Read the analog input value in the floating-point format from the specified channel /// /// 1 ~ 7 (the slot which I-8014W is inserted) /// 0 ~ 7 if Differential mode; 0 ~ 15 if SingleEnded mode /// the gain which is defined by the enum Gain /// the read back floating-point value /// 0 = SUCCESS. -1 = ERROR. public static Int16 ReadAI(Int16 slot, Int16 ch, Int16 gain, ref float fVal) { return ICPDAS.pac_i8014W_ReadAI(slot, ch, gain, ref fVal); } /// /// To clear FIFO data in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static void ClearFIFO(Int16 slot) { ICPDAS.pac_i8014W_ClearFIFO( slot); } /// /// To unlock the FIFO in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static void UnLockFIFO(Int16 slot) { ICPDAS.pac_i8014W_UnLockFIFO(slot); } /// /// To stop Magic Scan function in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// chArr[]: Sampled channel array, it can be have maximum 16 channel counts /// scanChCount: Total channel counts that Magic Scan will operate. ///sampleRate: 1 ~ 250KHz ///scanMode: 1 => Standard Mode; 2 => Sample and Hold ///triggerSource: 0 => Software Trigger; 1 => Internal Interrupt Signal Trigger; /// 2 => External Input Signal Trigger. ///triggerState: 0 => When external input signal level is high to activate the trigger. /// 1 => When external input signal level is low to activate the trigger. /// 0 = SUCCESS. -1 = ERROR. public static void ConfigMagicScan(Int16 slot, Int16[] chArr, Int16[] gainArr, Int16 scanChCount, float sampleRate, Int16 scanMode, Int16 triggerSource, Int16 triggerState, ref float realSampleRate) { ICPDAS.pac_i8014W_ConfigMagicScan(slot, chArr, gainArr, scanChCount, sampleRate, scanMode, triggerSource, triggerState, ref realSampleRate); } /// /// To start Magic Scan function in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static Int16 StartMagicScan(Int16 slot) { return ICPDAS.pac_i8014W_StartMagicScan(slot); } /// /// To stop Magic Scan function in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static Int16 StopMagicScan(Int16 slot) { return ICPDAS.pac_i8014W_StopMagicScan(slot); } /// /// To read data from FIFO and how many data in the FIFO on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// hexData []: read hex format analog input data array from FIFO /// * dataCountFromFIFO: how many counters of date was read from FIFO /// 0 = SUCCESS. -1 = ERROR. public static Int16 ReadFIFO(Int16 slot, Int16[] hexData, Int16 readCount, ref Int16 dataCountFromFIFO)// <=== get data from FIFO ,long* dataCnt { return ICPDAS.pac_i8014W_ReadFIFO(slot, hexData, readCount,ref dataCountFromFIFO); } public static void CalibrateDataHex(Int16 slot, Int16 iGain, Int16 dataFromFIFO, ref Int16 calibratedAI) { ICPDAS.pac_i8014W_CalibrateDataHex(slot, iGain, dataFromFIFO, ref calibratedAI); } public static void CalibrateData(Int16 slot, Int16 iGain, Int16 dataFromFIFO, ref float calibratedAI) { ICPDAS.pac_i8014W_CalibrateData(slot, iGain, dataFromFIFO, ref calibratedAI); } /// /// To Install Magic Scan ISR function in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// *isr (int slot): A pointer that pointer to function and parameter is slot /// triggerLevel: 0~ 7 (the value is equal to 7 when the value is more than 7) /// 0 = SUCCESS. -1 = ERROR. public static Int16 InstallMagicScanISR(Int16 slot, IsrFunction isr, Int16 triggerLevel) //<=== give a callback function for interrupt { return ICPDAS.pac_i8014W_InstallMagicScanISR(slot, isr, triggerLevel); } /// /// To uninstall Magic Scan ISR function in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static Int16 UnInstallMagicScanISR(Int16 slot) //<=== give a callback function for interrupt { return ICPDAS.pac_i8014W_UnInstallMagicScanISR(slot); } /// /// To clear Interrupt in the I-8014W on the specified slot /// /// 1 ~ 7 (the slot which I-8014HW is inserted) /// 0 = SUCCESS. -1 = ERROR. public static void ClearInt(Int16 slot) { ICPDAS.pac_i8014W_ClearInt(slot); } 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); } } }