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

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


		//======================================================================
		// Error Code
		//======================================================================
		public const short DIO_ERR_SUCCESS					= 0;		//Successed
		public const short DIO_ERR_INI_MEMORY				= 3;		//Cannot allocate memory
		public const short DIO_ERR_INI_REGISTRY				= 4;		//Cannot acceess setting file
		public const short DIO_ERR_DLL_DEVICE_NAME			= 10000;	//Used DeviceName unwritten in setting file
		public const short DIO_ERR_DLL_INVALID_ID 			= 10001;	//Invalid Id
		public const short DIO_ERR_DLL_CREATE_FILE			= 10003;	//Cannot open
		public const short DIO_ERR_DLL_CLOSE_FILE			= 10004;	//Close error
		public const short DIO_ERR_ACCESS_RIGHT				= 10005;	//Access rights error
		public const short DIO_ERR_DLL_TIMEOUT				= 10006;	//Timeout
		public const short DIO_ERR_COMPOSITION				= 10007;	//System error
		public const short DIO_ERR_INFO_INVALID_DEVICE		= 10050;	//Invalid device
		public const short DIO_ERR_INFO_NOT_FIND_DEVICE		= 10051;	//Not find device
		public const short DIO_ERR_DLL_BUFF_ADDRESS			= 10100;	//Buffer address error
		public const short DIO_ERR_SYS_NOT_SUPPORTED		= 20001;	//Function is not supported
		public const short DIO_ERR_SYS_PORT_NO				= 20100;	//PortNo error
		public const short DIO_ERR_SYS_PORT_NUM				= 20101;	//PortNum error
		public const short DIO_ERR_SYS_BIT_NO				= 20102;	//BitNo error
		public const short DIO_ERR_SYS_BIT_NUM				= 20103;	//BitNum error
		public const short DIO_ERR_SYS_BIT_DATA				= 20104;	//Bit data error
		public const short DIO_ERR_SYS_DIRECTION			= 20106;	//Direction error
		public const short DIO_ERR_SYS_FILTER				= 20400;	//Filter value error

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

    }
}

