/* AIOeNET.cs — accesio.com */

using System;
using System.Net;
using System.Runtime.InteropServices; // For the DllImport of AIOAIO Driver API

namespace AIOeNET
{
    //These should be copied into source files that reference them.
    using TUInt32 = UInt32;
    using TUInt16 = UInt16;
    using TUInt8 = Byte;
    using TUIntNative = UIntPtr;
    using TSInt32 = Int32;
    using TSInt16 = Int16;
    using TSInt8 = SByte;
    using TDouble = Double;
    using TWinErrCode = UInt32;
    using TChannelIndex = UInt32;
    using TClientRef = UIntPtr;



    public class eNET
    {
        public const TWinErrCode ERROR_SUCCESS = 0;
        public const TChannelIndex ciAll = 0xFFFFFFFF;
        public enum Range : TUInt32
        {
            Code0 = 0,
            Code1 = 1,
            Code2 = 2,
            Code3 = 3,
            Code4 = 4,
            Code5 = 5,
            Code6 = 6,
            Code7 = 7,
            NoChange = 0xFFFFFFFF,
            Unipolar10 = 0x30313055,
            Unipolar5 = 0x35303055,
            Unipolar2 = 0x32303055,
            Unipolar1 = 0x31303055,
            Bipolar24 = 0x3432E142,
            Bipolar20 = 0x3032E142,
            Bipolar12 = 0x3231E142,
            Bipolar10 = 0x3031E142,
            Bipolar5 = 0x3530E142,
            Bipolar2_5 = 0xBD32E142,
            Bipolar2 = 0x3230E142,
            Bipolar1_25 = 0xBC31E142,
            Bipolar1 = 0x3130E142,
            Bipolar_625 = 0x3835E142,
            Bipolar_3125 = 0x3331E142,
            Current420 = 0x30323449,
        }
        public enum TCalMode : TUInt32
        {
            cmRealData = 0,
            cmUnipolarGround = 1,
            cmUnipolarReference = 3,
            cmBipolarGround = 5,
            cmBipolarReference = 7,
        }


        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ScanForDevicesW")]
        internal static extern TWinErrCode ScanForDevices(TUInt32 NameListChAvailable, [Out] Char[] pNameList, out TUInt32 NameListCh);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ScanForDevicesU")]
        //internal static extern TWinErrCode ScanForDevices(TUInt32 NameListChAvailable, [Out] Char[] pNameList, out TUInt32 NameListCh);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ScanForDevicesA(TUInt32 NameListChAvailable, [Out] Char[] pNameList, out TUInt32 NameListCh);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ScanForDevicesW(TUInt32 NameListChAvailable, [Out] Char[] pNameList, out TUInt32 NameListCh);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl)]
        //internal static extern TWinErrCode ScanForDevicesU(TUInt32 NameListChAvailable, [Out] Char[] pNameList, out TUInt32 NameListCh);


        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ConnectW", SetLastError = true)]
        internal static extern TClientRef Connect(Char[] Host, TSInt32 Index);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ConnectU", SetLastError = true)]
        //internal static extern TClientRef Connect(Char[] Host, TSInt32 Index);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        internal static extern TClientRef ConnectA(Char[] Host, TSInt32 Index);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        internal static extern TClientRef ConnectW(Char[] Host, TSInt32 Index);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        //internal static extern TClientRef ConnectU(Char[] Host, TSInt32 Index);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode Disconnect(TClientRef uClientRef);


        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ChangeDeviceHostnameW")]
        internal static extern TWinErrCode ChangeDeviceHostname(TClientRef uClientRef, [Out] Char[] NewHostname);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ChangeDeviceHostnameU")]
        //internal static extern TWinErrCode ChangeDeviceHostname(TClientRef uClientRef, [Out] Char[] NewHostname);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ChangeDeviceHostnameA(TClientRef uClientRef, [Out] Char[] NewHostname);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ChangeDeviceHostnameW(TClientRef uClientRef, [Out] Char[] NewHostname);
        //[DllImport("AIOeNET.DLL", CharSet = CharSet.`, CallingConvention = CallingConvention.Cdecl)]
        //internal static extern TWinErrCode ChangeDeviceHostnameU(TClientRef uClientRef, [Out] Char[] NewHostname);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode GetDeviceFeatures(TClientRef uClientRef, TUInt32 FeatureBytes, [Out] TUInt8[] pFeatures);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode SetTimeout(TClientRef uClientRef, TSInt32 TimeoutMS);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DIO_Configure(TClientRef uClientRef, ref TUInt32 pDataBytes, [In, Out] TUInt8[] pData, [In, Out] TUInt8[] pDirection);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DIO_ReadDirection(TClientRef uClientRef, ref TUInt32 pDataBytes, [Out] TUInt8[] pData);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DIO_WriteAll(TClientRef uClientRef, ref TUInt32 pDataBytes, [In] TUInt8[] pData);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DIO_ReadAll(TClientRef uClientRef, ref TUInt32 pDataBytes, [Out] TUInt8[] pData);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_GetRange(TClientRef uClientRef, TChannelIndex iDAC, out Range RangeCode, out TDouble MinV, out TDouble MaxV);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_Enable(TClientRef uClientRef);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_Disable(TClientRef uClientRef);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DAC_OutputV")]
        internal static extern TWinErrCode DAC_Output(TClientRef uClientRef, TChannelIndex Ch, TDouble Volts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DAC_OutputCounts")]
        internal static extern TWinErrCode DAC_Output(TClientRef uClientRef, TChannelIndex Ch, TUInt16 Counts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DAC_OutputNV")]
        internal static extern TWinErrCode DAC_Output(TClientRef uClientRef, TUInt32 Channels, [In] TDouble[] Volts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DAC_OutputNCounts")]
        internal static extern TWinErrCode DAC_Output(TClientRef uClientRef, TUInt32 Channels, [In] TUInt16[] Counts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputV(TClientRef uClientRef, TChannelIndex Ch, TDouble Volts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputCounts(TClientRef uClientRef, TChannelIndex Ch, TUInt16 Counts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputFrac(TClientRef uClientRef, TChannelIndex Ch, TDouble Frac);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputNV(TClientRef uClientRef, TUInt32 Channels, [In] TDouble[] Volts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputNCounts(TClientRef uClientRef, TUInt32 Channels, [In] TUInt16[] Counts);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode DAC_OutputNFrac(TClientRef uClientRef, TUInt32 Channels, [In] TDouble[] Frac);


        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_Reset(TClientRef uClientRef);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetRange(TClientRef uClientRef, TChannelIndex Ch, Range RangeCode);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetDifferential(TClientRef uClientRef, TChannelIndex Ch, TUInt8 bDifferential);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetChScan(TClientRef uClientRef, TChannelIndex FirstCh, TChannelIndex LastCh);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetOversample(TClientRef uClientRef, TUInt32 Oversample);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetRate(TClientRef uClientRef, TDouble Rate);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_GetRate(TClientRef uClientRef, ref TDouble Rate);


        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_ImmediateGetScanV")]
        internal static extern TWinErrCode ADC_ImmediateGetData(TClientRef uClientRef, [Out] TDouble[] pData, [Out] TUInt16[] pStatus, [Out] TUInt16[] pTimestamp);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_ImmediateGetScanRaw")]
        internal static extern TWinErrCode ADC_ImmediateGetData(TClientRef uClientRef, [Out] TUInt32[] pDataRaw);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_ImmediateGetSampleV")]
        internal static extern TWinErrCode ADC_ImmediateGetData(TClientRef uClientRef, TChannelIndex Ch, [Out] TDouble[] pData, [Out] TUInt16 pStatus, [Out] TUInt16 pTimestamp);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_ImmediateGetSampleRaw")]
        internal static extern TWinErrCode ADC_ImmediateGetData(TClientRef uClientRef, TChannelIndex Ch, [Out] TUInt32[] pDataRaw);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_ImmediateGetScanV(TClientRef uClientRef, [Out] TDouble[] pData, [Out] TUInt16[] pStatus, [Out] TUInt16[] pTimestamp);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_ImmediateGetScanRaw(TClientRef uClientRef, [Out] TUInt32[] pDataRaw);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_ImmediateGetSampleV(TClientRef uClientRef, TChannelIndex Ch, [Out] TDouble[] pData, [Out] TUInt16 pStatus, [Out] TUInt16 pTimestamp);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_ImmediateGetSampleRaw(TClientRef uClientRef, TChannelIndex Ch, [Out] TUInt32[] pDataRaw);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetCalMode(TClientRef uClientRef, TCalMode CalMode = TCalMode.cmRealData);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_SetRangeCal(TClientRef uClientRef, Range RangeCode, TDouble ScaleFactor = 1, TDouble OffsetCounts = 0);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_AcquireBegin(TClientRef uClientRef, TUInt8 TriggerMode = 0x15);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_AcquireEnd(TClientRef uClientRef);

        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_AcquireGetDataRaw")]
        internal static extern TWinErrCode ADC_AcquireGetData(TClientRef uClientRef, ref TUInt32 pDataSamples, [Out] TUInt32[] pDataRaw);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ADC_AcquireGetDataV")]
        internal static extern TWinErrCode ADC_AcquireGetData(TClientRef uClientRef, ref TUInt32 pDataSamples, [Out] TDouble[] pData, [Out] TUInt16[] pStatus, [Out] TUInt16[] pTimestamp);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_AcquireGetDataRaw(TClientRef uClientRef, ref TUInt32 pDataSamples, [Out] TUInt32[] pDataRaw);
        [DllImport("AIOeNET.DLL", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        internal static extern TWinErrCode ADC_AcquireGetDataV(TClientRef uClientRef, ref TUInt32 pDataSamples, [Out] TDouble[] pData, [Out] TUInt16[] pStatus, [Out] TUInt16[] pTimestamp);
    }
}
