using System;
using System.Text;
using System.Runtime.InteropServices;

namespace AIO
{
	/// <summary>
	/// Summary description for Ccapaio.
	/// </summary>
	public class Ccapaio
	{
		public Ccapaio()
		{
			//
			// TODO: Add constructor logic here
			//
		}
	//======================================================================
		//======================================================================
		//	API-CAP(W32)
		//	CCAPAIO.CS					CONTEC.CO.,LTD.
		//======================================================================
		//======================================================================

		//======================================================================
		// Range
		//======================================================================
		public const short PM10		= 0;		//+-10V
		public const short PM5		= 1;		//+-5V
		public const short P10		= 50;		//0~10V
		public const short P5		= 51;		//0~5V
		public const short P20MA	= 100;		//0~20mA

		//======================================================================
		// Error Code
		//======================================================================
		public const short AIO_ERR_SUCCESS					= 0;			//Successed
		public const short AIO_ERR_INI_MEMORY				= 3;			//Cannot allocate memory
		public const short AIO_ERR_INI_REGISTRY				= 4;			//Cannot access setting file
		public const short AIO_ERR_DLL_DEVICE_NAME			= 10000;		//Used DeviceName unwritten in setting file
		public const short AIO_ERR_DLL_INVALID_ID			= 10001;		//Invalid Id
		public const short AIO_ERR_DLL_CREATE_FILE			= 10003;		//Cannot open
		public const short AIO_ERR_DLL_CLOSE_FILE			= 10004;		//Close error
		public const short AIO_ERR_INFO_NOT_FIND_DEVICE		= 10006;		//Not find device
		public const short AIO_ERR_PTR_DEVICE_NAME			= 10007;		//DeviceName is NULL
		public const short AIO_ERR_PTR_DEVICE				= 10008;		//Device is NULL
		public const short AIO_ERR_UNKNOWN_DEVICE			= 10013;		//Unknown device
		public const short AIO_ERR_PTR_DEVICE_TYPE			= 10015;		//DeviceType is NULL
		public const short AIO_ERR_INFO_INVALID_DEVICE		= 10050;		//Invalid device
		public const short AIO_ERR_NULL_DEVICE				= 10100;		//DeviceName is NULL string
		public const short AIO_ERR_PTR_ID					= 10101;		//Id is NULL
		public const short AIO_ERR_PTR_ERR_STR				= 10180;		//ErrorString is NULL
		public const short AIO_ERR_PTR_AI_RANGE				= 11051;		//AiRange is NULL
		public const short AIO_ERR_AI_RANGE					= 11060;		//AiRange is out of range
		public const short AIO_ERR_AI_CHANNEL				= 11420;		//AiChannel is out of range
		public const short AIO_ERR_PTR_AI_DATA				= 11421;		//AiData is NULL
		public const short AIO_ERR_PTR_AI_MAX_CHANNELS		= 11720;		//AiMaxChannels is NULL
		public const short AIO_ERR_PTR_AI_RESOLUTION		= 11740;		//AiResolution is NULL
		public const short AIO_ERR_AO_CHANNEL				= 13000;		//AoChannel is out of range
		public const short AIO_ERR_AO_CHANNELS				= 13020;		//AoChannels is out of range
		public const short AIO_ERR_PTR_AO_DATA				= 13021;		//AoData is NULL
		public const short AIO_ERR_PTR_AO_RESOLUTION		= 13040;		//AoResolution is NULL
		public const short AIO_ERR_PTR_AO_MAX_CHANNELS		= 13080;		//AoMaxChannels is NULL
		public const short AIO_ERR_PTR_AO_RANGE				= 13111;		//AoRange is NULL
		public const short AIO_ERR_AO_RANGE					= 13120;		//AoRange is out of range
		public const short AIO_ERR_SYS_NOT_SUPPORTED		= 20001;		//Function is not supported
		public const short AIO_ERR_DEVICE_BUSY				= 20002;		//Device is busy
		public const short AIO_ERR_AI_DEVICE_RANGE			= 21060;		//AiRange is out of range
		public const short AIO_ERR_AI_SET_RANGE				= 21063;		//Range setting error
		public const short AIO_ERR_AI_DEVICE_CHANNEL		= 21420;		//AiChannel is out of range
		public const short AIO_ERR_AD_ERROR					= 21421;		//AD transter error
		public const short AIO_ERR_AI_CHANNEL_NUM_OVER		= 21441;		//Number of Channels is over
		public const short AIO_ERR_AO_DEVICE_CHANNEL		= 23000;		//AoChannel is out of range
		public const short AIO_ERR_DA_ERROR					= 23001;		//DA transfer error
		public const short AIO_ERR_AO_DATA_RANGE1			= 23002;		//AoData must be from 0 to 4095
		public const short AIO_ERR_AO_DATA_RANGE2			= 23003;		//AoData must be from 0 to 605535
		public const short AIO_ERR_AO_CHANNEL_NUM_OVER		= 23021;		//Number of Channels is over
		public const short AIO_ERR_AO_DEVICE_RANGE			= 23120;		//AoRange is out of range
		public const short AIO_ERR_AO_SET_RANGE				= 23121;		//Range setting error
		public const short AIO_ERR_START_ERROR				= 28001;		//Module start error
		public const short AIO_ERR_NOT_START				= 28002;		//Device is not started
		public const short AIO_ERR_ACCESS_RIGHT				= 28003;		//Access rights error
		public const short AIO_ERR_DLL_TIMEOUT				= 28004;		//Timeout
		public const short AIO_ERR_COMPOSITION				= 28005;		//Unknown error

		//======================================================================
		// Function Prototype
		//======================================================================
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioInit(string Devicename, ref short Id);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioExit(short Id);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetErrorString(int Err, StringBuilder szMsg);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioQueryDeviceName(short Index, ref short GroupID, ref short UnitID, ref short DeviceID, StringBuilder DeviceName, StringBuilder Device);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetDeviceType(string Device, ref short DeviceType);
		
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSetDirectMode(short Id, short DirectMode);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSetIOInterval(int Interval);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioStartIO(short Id, short Start);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetRemoteStatus(short Id, ref byte Start, ref byte Status);
		
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSingleAi(short Id, short AiChannel, ref int AiData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSingleAiEx(short Id, short AiChannel, ref float AiData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioMultiAi(short Id, short AiChannels, [MarshalAs(UnmanagedType.LPArray)]int[] AiData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioMultiAiEx(short Id, short AiChannels, [MarshalAs(UnmanagedType.LPArray)]float[] AiData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAiResolution(short Id, ref short AiResolution);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAiMaxChannels(short Id, ref short AiMaxChannels);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSetAiRangeAll(short Id, short AiRange);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAiRange(short Id, short AiChannel, ref short AiRange);
		
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSingleAo(short Id, short AoChannel, int AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSingleAoEx(short Id, short AoChannel, float AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSingleAoEcho(short Id, short AoChannel, ref int AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioMultiAo(short Id, short AoChannels, [MarshalAs(UnmanagedType.LPArray)]int[] AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioMultiAoEx(short Id, short AoChannels, [MarshalAs(UnmanagedType.LPArray)]float[] AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioMultiAoEcho(short Id, short AoChannels, [MarshalAs(UnmanagedType.LPArray)]int[] AoData);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAoResolution(short Id, ref short AoResolution);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAoMaxChannels(short Id, ref short AoMaxChannels);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioSetAoRangeAll(short Id, short AoRange);
		[DllImport("CCAPAIO.DLL")]
		public static extern int AioGetAoRange(short Id, short AoChannel, ref short AoRange);
	
	}
}