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

namespace DIO
{
	/// <summary>
	/// Ccapdio ̊Tv̐łB
	/// </summary>
	public class Ccapdio
	{
		public Ccapdio()
		{
			// 
			// TODO: RXgN^ WbNɒǉĂB
			//
		}
		//======================================================================
		//======================================================================
		//	API-CAP(W32)
		//	CCAPDIO.CS					CONTEC.CO.,LTD.
		//======================================================================
		//======================================================================


		//======================================================================
		// Error Code
		//======================================================================
		public const short	DIO_ERR_SUCCESS					= 0;		//I 
		public const short	DIO_ERR_INI_MEMORY				= 3;		//̊蓖ĂɎs܂B 
		public const short	DIO_ERR_INI_REGISTRY			= 4;		//ݒt@C̃ANZXɎs܂B 
		public const short	DIO_ERR_DLL_DEVICE_NAME			= 10000;	//ݒt@Cɓo^ĂȂfoCXw肳܂B 
		public const short	DIO_ERR_DLL_INVALID_ID 			= 10001;	//IDw肳܂B 
		public const short	DIO_ERR_DLL_CREATE_FILE			= 10003;	//nh̎擾ɍ쐬Ɏs܂B 
		public const short	DIO_ERR_DLL_CLOSE_FILE			= 10004;	//nh̃N[YɎs܂B 
		public const short	DIO_ERR_ACCESS_RIGHT			= 10005;	//ANZXG[łB
		public const short	DIO_ERR_DLL_TIMEOUT				= 10006;	//ʐM^CAEg܂B
		public const short	DIO_ERR_COMPOSITION				= 10007;	//@\G[łB
		public const short	DIO_ERR_INFO_INVALID_DEVICE		= 10050;	//w肵foCX̂܂BXymFĂB 
		public const short	DIO_ERR_INFO_NOT_FIND_DEVICE	= 10051;	//p\ȃfoCX܂B 
		public const short	DIO_ERR_DLL_BUFF_ADDRESS		= 10100;	//f[^obt@AhXsłB 
		public const short	DIO_ERR_SYS_NOT_SUPPORTED		= 20001;	//̃foCXł͂̊֐͎gpł܂B
		public const short	DIO_ERR_SYS_PORT_NO				= 20100;	//|[gԍw\͈͂𒴂Ă܂B 
		public const short	DIO_ERR_SYS_PORT_NUM			= 20101;	//|[gw\͈͂𒴂Ă܂B 
		public const short	DIO_ERR_SYS_BIT_NO				= 20102;	//rbgԍw\͈͂𒴂Ă܂B 
		public const short	DIO_ERR_SYS_BIT_NUM				= 20103;	//rbgw\͈͂𒴂Ă܂B 
		public const short	DIO_ERR_SYS_BIT_DATA			= 20104;	//rbgf[^01ȊOłB 
		public const short	DIO_ERR_SYS_DIRECTION			= 20106;	//o͕w͈͊OłB
		public const short	DIO_ERR_SYS_FILTER				= 20400;	//tB^萔w͈͊OłB 

		//======================================================================
		// Function Prototype
		//======================================================================
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioInit(string Devicename, ref short Id);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioExit(short Id);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetErrorString(int Err, StringBuilder szMsg);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioQueryDeviceName(short Index, ref short GroupID, ref short UnitID, ref short DeviceID, StringBuilder DeviceName, StringBuilder Device);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioSetDirectMode(short Id, short DirectMode);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioSetIOInterval(int Interval);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetRemoteStatus(short Id, ref byte Start, ref byte Status);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioSetDigitalFilter(short Id, short FilterValue);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetDigitalFilter(short Id, ref short FilterValue);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioSetIoDirection(short Id, int Dir);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetIoDirection(short Id, ref int Dir);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioInpByte(short Id, short PortNo, ref byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioInpBit(short Id, short BitNo, ref byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioOutByte(short Id, short PortNo, byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioOutBit(short Id, short BitNo, byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioEchoBackByte(short Id, short PortNo, ref byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioEchoBackBit(short Id, short BitNo, ref byte Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioInpMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioInpMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioOutMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioOutMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioEchoBackMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioEchoBackMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetMaxPorts(short Id, ref short InPortNum, ref short OutPortNum);
		[DllImport("CCAPDIO.DLL")]
		public static extern int DioGetMaxBits(short Id, ref short InBitNum, ref short OutBitNum);
        [DllImport("CCAPDIO.DLL")]
        public static extern int DioResetPatternEvent(short Id, [MarshalAs(UnmanagedType.LPArray)]byte[] Data);
        [DllImport("CCAPDIO.DLL")]
        public static extern int DioGetPatternEventStatus(short Id, ref short Status, short EventType);

    }
}