using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; ///I-8093W version 1.0.1.0, 2011/07/05 ///I-8093W version 1.0.1.1, 2011/07/19 ///Add pac_i8093W_GetPresetValue namespace pac8093WNet { public class pac8093W { private class ICPDAS { [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetFirmwareVersion")] public static extern Int16 pac_i8093W_GetFirmwareVersion(int Slot); // for I/O version management [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetLibVersion")] public static extern Int16 pac_i8093W_GetLibVersion(); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetLibDate")] public static extern void pac_i8093W_GetLibDate(byte[] libDate); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_Init")] public static extern Int16 pac_i8093W_Init(int Slot); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_SetMode")] public static extern Int16 pac_i8093W_SetMode(int Slot, int Channel, Int32 Mode); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetMode")] public static extern Int16 pac_i8093W_GetMode(int Slot, int Channel, ref Int32 Mode); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetLineStatus")] public static extern Int16 pac_i8093W_GetLineStatus(int Slot, int Channel, ref Int32 A_Status, ref Int32 B_Status, ref Int32 C_Status); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetIndex")] public static extern Int16 pac_i8093W_GetIndex(int Slot, int Channel, ref Int32 C_Index); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_SetXOR")] public static extern Int16 pac_i8093W_SetXOR(int Slot, int Channel, Int32 XOR); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetXOR")] public static extern Int16 pac_i8093W_GetXOR(int Slot, int Channel, ref Int32 XOR); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_Read32BitEncoder")] public static extern Int16 pac_i8093W_Read32BitEncoder(int Slot, int Channel, ref Int32 EncoderValue); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_ResetEncoder")] public static extern Int16 pac_i8093W_ResetEncoder(int Slot, int Channel); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_SetPresetValue")] public static extern Int16 pac_i8093W_SetPresetValue(int Slot, int Channel,Int32 preset); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetPresetValue")] public static extern Int16 pac_i8093W_GetPresetValue(int slot, int channel, ref Int32 presetValue); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_ReadFreq")] public static extern Int16 pac_i8093W_ReadFreq(int Slot, int Channel,ref float freq); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_GetIndexLatchStatus")] public static extern Int16 pac_i8093W_GetIndexLatchStatus(int slot, int ch, ref Int32 latched, ref Int32 status); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_SetIndexLatchStatus")] public static extern Int16 pac_i8093W_SetIndexLatchStatus(int Slot, int Channel, Int32 status); [DllImport("pac_i8093W.dll", EntryPoint = "pac_i8093W_ClearLatchedIndex")] public static extern Int16 pac_i8093W_ClearLatchedIndex(int Slot, int Channel); } /// /// Get the library version of this DLL (pac_i8093W.dll) /// /// library version public static Int16 LibVersion() { return ICPDAS.pac_i8093W_GetLibVersion(); } /// /// Get the firmware version of I-8093W in the specified slot. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// firmware version public static Int16 FirmwareVersion(int slot) { return ICPDAS.pac_i8093W_GetFirmwareVersion(slot); } /// /// Get the building date of this DLL (pac_i8093W.dll) /// /// library building date public static string LibDate() { //return Arr2Str(ICPDAS.pac_i8017H_GetLibDate()); byte[] libDate= new byte[32] ; ICPDAS.pac_i8093W_GetLibDate(libDate); return AscArr2Str(libDate); } /// /// Initialize the I-8093W in the specified slot /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// the Error Codes which are defined in pac_i8093W.h public static Int16 Init(int slot) { return ICPDAS.pac_i8093W_Init(slot); } /// /// Set the counting mode of the encoder I-8093W /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the counting mode: CW_CCW, Pulse_Dir, Quadrant /// the Error Codes which are defined in pac_i8093W.h public static Int16 SetChannelMode(int Slot, int Channel, Int32 Mode) { return ICPDAS.pac_i8093W_SetMode(Slot, Channel, Mode); } /// /// Get the counting mode of the encoder I-8093W /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the counting mode: CW_CCW, Pulse_Dir, Quadrant /// the Error Codes which are defined in pac_i8093W.h public static Int16 ReadChannelMode(int Slot, int Channel, ref Int32 Mode) { return ICPDAS.pac_i8093W_GetMode(Slot, Channel, ref Mode); } /// /// Get the active state in the specified channel of I-8093W of the specified slot. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// /// the active state in the specified channel, active high or active low. /// Default is active high. /// /// the Error Codes which are defined in pac_i8093W.h public static Int16 ReadXor(int Slot, int Channel, ref Int32 XorReg) { return ICPDAS.pac_i8093W_GetXOR(Slot, Channel, ref XorReg); } /// /// Set the active state in the specified channel of I-8093W of the specified slot. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// /// the active state in the specified channel, active high or active low. /// Default is active high. /// /// the Error Codes which are defined in pac_i8093W.h public static Int16 SetXor(int Slot, int Channel, Int32 XorReg) { return ICPDAS.pac_i8093W_SetXOR(Slot, Channel, XorReg); } /// /// Set the preset value of the 32-bit counter value in the specified channel /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the preset value of the 32-bit counter value /// the Error Codes which are defined in pac_i8093W.h public static Int16 SetPreset(int Slot, int Channel, Int32 Preset) { return ICPDAS.pac_i8093W_SetPresetValue(Slot, Channel, Preset); } /// /// Set the preset value of the 32-bit counter value in the specified channel /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the preset value of the 32-bit counter value /// the Error Codes which are defined in pac_i8093W.h public static Int16 GetPreset(int Slot, int Channel, ref Int32 Preset) { return ICPDAS.pac_i8093W_GetPresetValue(Slot, Channel,ref Preset); } /// /// Reset (Clear) the counting values in the specified channel of the encorder, I-8093W /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the Error Codes which are defined in pac_i8093W.h public static Int16 ReadEncoder(int Slot, int Channel, ref Int32 EncoderVal) { return ICPDAS.pac_i8093W_Read32BitEncoder(Slot, Channel, ref EncoderVal); } /// /// Read the frequency value of the specified channel. /// Note1: the feature of frequency reading requires firmware version 3 or above. /// Note2: if there is no encoder input, the readback frequency is 0.093, not zero. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the frequency value /// the Error Codes which are defined in pac_i8093W.h public static Int16 ReadFreq(int Slot, int Channel, ref float FreqVal) { return ICPDAS.pac_i8093W_ReadFreq(Slot, Channel, ref FreqVal); } public static void ResetEncoder(int Slot, int Channel) { ICPDAS.pac_i8093W_ResetEncoder( Slot, Channel); } /// /// Read the A, B, C (Z) status of the specified channel /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// A status, 0 or 1 /// B status, 0 or 1 /// C (Z) status, 0 or 1 /// the Error Codes which are defined in pac_i8093W.h public static Int16 ReadLineStatus(int Slot, int Channel, ref Int32 A_Status, ref Int32 B_Status, ref Int32 C_Status) { return ICPDAS.pac_i8093W_GetLineStatus(Slot, Channel, ref A_Status,ref B_Status, ref C_Status); } /// /// Read the index value of the specified channel /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// the index value /// the Error Codes which are defined in pac_i8093W.h public static void ReadIndex(int Slot, int Channel, ref Int32 C_Status) { ICPDAS.pac_i8093W_GetIndex(Slot, Channel, ref C_Status); } /// /// Depreciated. /// When calibrates the Z index point with GetIndex function,it is very difficult to catch the right position /// For the Z index signal is just a very narrow pulse. /// This function helps user to catch the Z index position. /// ifEnableLatch is used to indicate if this function is enable or not. /// latchedStatus is the Z index latched status, if enable this this function. /// If ifEnableLatch is enabled and Z index been latched, the encoder value will be latched at the position. /// Program has to use ClearLatchedIndex to clear the latch status and the encoder value will become normal. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// ifEnableLatch value, 0 or 1 /// latchedStatus value, 0 or 1 /// the Error Codes which are defined in pac_i8093W.h public static Int16 GetIndexLatchStatus(int Slot, int Channel, ref Int32 ifEnableLatch, ref Int32 latchedStatus) { return ICPDAS.pac_i8093W_GetIndexLatchStatus(Slot, Channel, ref ifEnableLatch, ref latchedStatus); } /// /// Depreciated. /// When calibrates the Z index point with GetIndex function,it is very difficult to catch the right position /// For the Z index signal is just a very narrow pulse. /// This function helps user to catch the Z index position. /// ifEnableLatch is used to indicate if this function is enable or not. /// If ifEnableLatch is enabled and Z index been latched, the encoder value will be latched at the position. /// Program has to use ClearLatchedIndex to clear the latch status and the encoder value will become normal. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) /// ifEnableLatch value, 0 or 1 /// the Error Codes which are defined in pac_i8093W.h public static Int16 SetIndexLatchStatus(int Slot, int Channel, Int32 ifEnableLatch) { return ICPDAS.pac_i8093W_SetIndexLatchStatus(Slot, Channel, ifEnableLatch); } /// /// Depreciated. /// If ifEnableLatch is enabled and Z index been latched, the encoder value will be latched at the position. /// Program has to use ClearLatchedIndex to clear the latch status and the encoder value will become normal. /// /// 1 ~ 7 (the slot which I-8093W is inserted) /// 0 ~ 2 (from where the input comes) public static Int16 ClearLatchedIndex(int Slot, int Channel) { return ICPDAS.pac_i8093W_ClearLatchedIndex(Slot, Channel); } 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); } } }