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

namespace AIO
{
	/// <summary>
	/// Ccapaio ̊Tv̐łB
	/// </summary>
	public class Ccapaio
	{
		public Ccapaio()
		{
			// 
			// TODO: RXgN^ WbNɒǉĂB
			//
		}
		//======================================================================
		//======================================================================
		//	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;			//I
		public const short	AIO_ERR_INI_MEMORY				= 3;			//̊蓖ĂɎs܂
		public const short	AIO_ERR_INI_REGISTRY			= 4;			//ݒt@C̃ANZXɎs܂
		public const short	AIO_ERR_DLL_DEVICE_NAME			= 10000;		//ݒ胆[eBeBɓo^ĂȂfoCXw肳܂
		public const short	AIO_ERR_DLL_INVALID_ID			= 10001;		//IDw肳܂
		public const short	AIO_ERR_DLL_CREATE_FILE			= 10003;		//nh̎擾Ɏs܂
		public const short	AIO_ERR_DLL_CLOSE_FILE			= 10004;		//nh̃N[YɎs܂
		public const short	AIO_ERR_INFO_NOT_FIND_DEVICE	= 10006;		//p\ȃfoCX܂
		public const short	AIO_ERR_PTR_DEVICE_NAME			= 10007;		//DeviceNamẽ|C^NULLł
		public const short	AIO_ERR_PTR_DEVICE				= 10008;		//Devicẽ|C^NULLł
		public const short	AIO_ERR_UNKNOWN_DEVICE			= 10013;		//foCX̎ނ𔻕ʂł܂
		public const short	AIO_ERR_PTR_DEVICE_TYPE			= 10015;		//DeviceTypẽ|C^NULLł
		public const short	AIO_ERR_INFO_INVALID_DEVICE		= 10050;		//w肵foCX̂܂BXymFĂB 
		public const short	AIO_ERR_NULL_DEVICE				= 10100;		//DeviceNameɃfoCXi[Ă܂
		public const short	AIO_ERR_PTR_ID					= 10101;		//Id̃|C^NULLł
		public const short	AIO_ERR_PTR_ERR_STR				= 10180;		//ErrorString̃|C^NULLł
		public const short	AIO_ERR_PTR_AI_RANGE			= 11051;		//AiRangeNULLł
		public const short	AIO_ERR_AI_RANGE				= 11060;		//AiRange̒l֐̎w͈͊Oł
		public const short	AIO_ERR_AI_CHANNEL				= 11420;		//AiChannel̒l֐̎w͈͊Oł
		public const short	AIO_ERR_PTR_AI_DATA				= 11421;		//AiDatã|C^NULLł
		public const short	AIO_ERR_PTR_AI_MAX_CHANNELS		= 11720;		//AiMaxChannels̃|C^NULLł
		public const short	AIO_ERR_PTR_AI_RESOLUTION		= 11740;		//AiResolutioñ|C^NULLł
		public const short	AIO_ERR_AO_CHANNEL				= 13000;		//AoChannel̒l֐̎w͈͊Oł
		public const short	AIO_ERR_AO_CHANNELS				= 13020;		//AoChannels̒l֐̎w͈͊Oł
		public const short	AIO_ERR_PTR_AO_DATA				= 13021;		//AoDatã|C^NULLł
		public const short	AIO_ERR_PTR_AO_RESOLUTION		= 13040;		//AoResolutioñ|C^NULLł
		public const short	AIO_ERR_PTR_AO_MAX_CHANNELS		= 13080;		//AoMaxChannels̃|C^NULLł
		public const short	AIO_ERR_PTR_AO_RANGE			= 13111;		//AoRangẽ|C^NULLł
		public const short	AIO_ERR_AO_RANGE				= 13120;		//AoRange̒l֐̎w͈͊Oł
		public const short	AIO_ERR_AP_CHANNEL				= 13500;		//AoChannel̒l֐̎w͈͊Oł
		public const short	AIO_ERR_SYS_NOT_SUPPORTED		= 20001;		//gpĂfoCXł͂̊֐gp邱Ƃł܂
		public const short	AIO_ERR_DEVICE_BUSY				= 20002;		//foCX쒆̂ߎsł܂
		public const short	AIO_ERR_AI_DEVICE_RANGE			= 21060;		//AiRange̒lgpĂfoCX̎w͈͊Oł
		public const short	AIO_ERR_AI_SET_RANGE			= 21063;		//W̐ݒɎs܂
		public const short	AIO_ERR_AI_DEVICE_CHANNEL		= 21420;		//AiChannel̒lgpĂfoCX͈̔͊Oł
		public const short	AIO_ERR_AD_ERROR				= 21421;		//ADϊG[܂
		public const short	AIO_ERR_AI_CHANNEL_NUM_OVER		= 21441;		//ϊ`lI[o[܂Aő`lŕϊ܂
		public const short	AIO_ERR_AO_DEVICE_CHANNEL		= 23000;		//AoChannel̒lgpĂfoCX͈̔͊Oł
		public const short	AIO_ERR_DA_ERROR				= 23001;		//DAϊG[܂
		public const short	AIO_ERR_AO_DATA_RANGE1			= 23002;		//AoDataɐݒ肷f[^͂0`4095͈̔͂Ŏw肵Ă
		public const short	AIO_ERR_AO_DATA_RANGE2			= 23003;		//AoDataɐݒ肷f[^͂0`65535͈̔͂Ŏw肵Ă
		public const short	AIO_ERR_AO_CHANNEL_NUM_OVER		= 23021;		//ϊ`lI[o[܂Bő`lŕϊ܂
		public const short	AIO_ERR_AO_DEVICE_RANGE			= 23120;		//AoRange̒lgpĂfoCX̎w͈͊Oł
		public const short	AIO_ERR_AO_SET_RANGE			= 23121;		//W̐ݒɎs܂
		public const short	AIO_ERR_START_ERROR				= 28001;		//W[NɃG[܂B
		public const short	AIO_ERR_NOT_START				= 28002;		//foCX͂܂NĂ܂B
		public const short	AIO_ERR_ACCESS_RIGHT			= 28003;		//ANZXG[łB
		public const short	AIO_ERR_DLL_TIMEOUT				= 28004;		//ʐM^CAEg܂B
		public const short	AIO_ERR_COMPOSITION				= 28005;		//@\G[łBO[vID=8ȊO̐ݒłIOAVXgT[o[KvłB

		//======================================================================
		// 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);
	
	}
}