using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
///I-8024W version 0.2.0.7, 2011/07/05
///I-8024W version 0.2.0.8, 2011/07/19
///just update the version 
namespace pac8024WNet
{
    public class pac8024W
    {
        private class ICPDAS
        {
            // for I/O version management
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_GetLibVersion")]
            public static extern Int16 pac_i8024W_GetLibVersion();
            // for I/O version management
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_GetLibDate")]
            public static extern void pac_i8024W_GetLibDate(byte[] libDate);
            // for I/O version management
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_Initial")]
            public static extern Int16 pac_i8024W_Initial(Int16 slot);
            //iData: -32768  ~  +32767
            // for I/O version management
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_VoltageOut")]
            public static extern void pac_i8024W_VoltageOut(Int16 slot, Int16 ch, float fData);
            //fData: -10.0 ~ +10.0
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_VoltageOut_Hex")]
            public static extern void pac_i8024W_VoltageOut_Hex(Int16 slot, Int16 ch, Int16 hData);        
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_CurrentOut")]
            public static extern void pac_i8024W_CurrentOut(Int16 slot, Int16 ch, float fData);
            //fData:  0.0 ~ +20.0 
            
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_CurrentOut_Hex")]
            public static extern void pac_i8024W_CurrentOut_Hex(Int16 slot, Int16 ch, Int16 hData);
            //Voltage output readback (software readback)
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_VoltageOut_Readback")]
            public static extern float pac_i8024W_VoltageOut_Readback(int slot,int ch);
//fData: -10.0 ~ +10.0
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_VoltageOut_Hex_Readback")]
            public static extern int pac_i8024W_VoltageOut_Hex_Readback(int slot,int ch);
//iData: -32768  ~  +32767
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_CurrentOut_Readback")]
            public static extern float pac_i8024W_CurrentOut_Readback(int slot,int ch);
//fData:  0.0 ~ +20.0 
            [DllImport("pac_i8024W.dll", EntryPoint = "pac_i8024W_CurrentOut_Hex_Readback")]
            public static extern int pac_i8024W_CurrentOut_Hex_Readback(int slot, int ch);
//iData: 0  ~  +32767
            //iData: 0  ~  +32767 
        }
        /// 
        /// Get the library version of this DLL (pac_i8024W.dll)
        /// 
        /// library version
        public static Int16 LibVersion()
        {
            return ICPDAS.pac_i8024W_GetLibVersion();
        }
        // 
        /// Get the building date of this DLL (pac_i8024W.dll)
        /// 
        /// library building date
        public static string LibDate()
        {
            byte[] libDate= new byte[32] ;
            ICPDAS.pac_i8024W_GetLibDate(libDate);
            return AscArr2Str(libDate);
        }
        /// 
        /// Initialize the I-8024W in the specified slot
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// no error, return 0
        public static Int16 Init(Int16 slot)
        {
            return ICPDAS.pac_i8024W_Initial(slot);
        }
        /// 
        /// Output the voltage of hex format to the specified channel.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the output value in hex format, -32768  ~  +32767
        
        public static void VoltageOut_Hex(Int16 slot, Int16 ch, Int16 hData)
        {
             ICPDAS.pac_i8024W_VoltageOut_Hex(slot, ch,hData);
        }
        /// 
        /// Output the voltage of floating-point format to the specified channel.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the output value in floating-point format, -10.0 ~ +10.0 V
        public static void VoltageOut(Int16 slot, Int16 ch, float fData)
        {
            ICPDAS.pac_i8024W_VoltageOut(slot, ch, fData);
        }
        /// 
        /// Output the current of floating-point format to the specified channel.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the output value in floating-point format, 0.0 ~ +20.0 mA
        public static void CurrentOut(Int16 slot, Int16 ch, float fData)
        {
            ICPDAS.pac_i8024W_CurrentOut(slot, ch, fData);
        }
        /// 
        /// Output the current of hex format to the specified channel.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the output value in hex format, 0  ~  +32767
        public static void CurrentOut_Hex(Int16 slot, Int16 ch, Int16 hData)
        {
            ICPDAS.pac_i8024W_CurrentOut_Hex(slot, ch, hData);
        }
        /// 
        /// Read voltage in floating-point format from the specified channel.
        /// Note: the value is not read from the hardware of I-8024W, but memory.
        /// Close the program makes reading fail.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the voltage in floating-point format read from the specified channel
        public static float VoltageOut_Readback(Int16 slot, Int16 ch)
        {
            return ICPDAS.pac_i8024W_VoltageOut_Readback(slot, ch);
        }
        /// 
        /// Read current in floating-point format from the specified channel.
        /// Note: the value is not read from the hardware of I-8024W, but memory.
        /// Close the program makes reading fail.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the current in floating-point format read from the specified channel
        public static float CurrentOut_Readback(Int16 slot, Int16 ch)
        {
            return ICPDAS.pac_i8024W_CurrentOut_Readback(slot, ch);
        }
        /// 
        /// Read voltage in hex format from the specified channel.
        /// Note: the value is not read from the hardware of I-8024W, but memory.
        /// Close the program makes reading fail.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the voltage in hex format read from the specified channel
        public static Int16 VoltageOut_Hex_Readback(Int16 slot, Int16 ch)
        {
            return (Int16)ICPDAS.pac_i8024W_VoltageOut_Hex_Readback(slot, ch);
        }
        /// 
        /// Read current in hex format from the specified channel.
        /// Note: the value is not read from the hardware of I-8024W, but memory.
        /// Close the program makes reading fail.
        /// 
        /// 1 ~ 7 (the slot which I-8024W is inserted)
        /// 0 ~ 7 (to where the output goes)
        /// the current in hex format read from the specified channel
        public static Int16 CurrentOut_Hex_Readback(Int16 slot, Int16 ch)
        {
            return (Int16)ICPDAS.pac_i8024W_CurrentOut_Hex_Readback(slot, ch);
        }
        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);
        } 
    }
}