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

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


		//======================================================================
		// Error Code
		//======================================================================
		public const short CNT_ERR_SUCCESS					= 0;		//Successed
		public const short CNT_ERR_INI_MEMORY				= 3;		//Cannot allocate memory
		public const short CNT_ERR_INI_REGISTRY				= 4;		//Cannot acceess setting file
		public const short CNT_ERR_DLL_DEVICE_NAME			= 10000;	//Used DeviceName unwritten in setting file
		public const short CNT_ERR_DLL_INVALID_ID			= 10001;	//Invalid Id
		public const short CNT_ERR_START					= 10002;	//Module start error
		public const short CNT_ERR_DLL_CREATE_FILE			= 10003;	//Cannot open
		public const short CNT_ERR_DLL_CLOSE_FILE			= 10004;	//Close error
		public const short CNT_ERR_ACCESS_RIGHT				= 10005;	//Access rights error
		public const short CNT_ERR_DLL_TIMEOUT 				= 10006;	//Timeout
		public const short CNT_ERR_COMPOSITION				= 10007;	//System error
		public const short CNT_ERR_INFO_INVALID_DEVICE		= 10050;	//Invalid device
		public const short CNT_ERR_INFO_NOT_FIND_DEVICE		= 10051;	//Not find device
		public const short CNT_ERR_DLL_MODE					= 10100;	//Invalid mode
		public const short CNT_ERR_DLL_BUFF_ADDRESS			= 10101;	//Buffer address error
		public const short CNT_ERR_SYS_NOT_SUPPORTED		= 20001;	//Function is not supported
		public const short CNT_ERR_SYS_CH_NO				= 20201;	//Invalid channdl number
		public const short CNT_ERR_SYS_CH_NUM				= 20202;	//Invalid number of channels
		public const short CNT_ERR_SYS_CH_VALUE				= 20203;	//Invalid counter value

		//======================================================================
		// Parameter Definition
		//======================================================================
		public const short CNT_ZPHASE_NOT_USE			= 1;	//Not use
		public const short CNT_ZPHASE_NEXT_ONE			= 2;	//Next time
		public const short CNT_ZPHASE_EVERY_TIME		= 3;	//Everytime

		public const short CNT_ZLOGIC_POSITIVE			= 0;	//Positive logic 
		public const short CNT_ZLOGIC_NEGATIVE			= 1;	//Negative logic

		public const short CNT_SIGTYPE_ISOLATE			= 0;	//Photoisolator
		public const short CNT_SIGTYPE_TTL				= 1;	//TTL(reserved)
		public const short CNT_SIGTYPE_LINERECEIVER		= 2;	//Differential(reserved)

		public const short CNT_DIR_DOWN					= 0;	//Down-count
		public const short CNT_DIR_UP					= 1;	//Up-count

		public const short CNT_MODE_1PHASE				= 0;	//1 Phase
		public const short CNT_MODE_2PHASE				= 1;	//2 Phase
		public const short CNT_MODE_GATECONTROL			= 2;	//Gate control

		public const short CNT_MUL_X1					= 0;	//x1
		public const short CNT_MUL_X2					= 1;	//x2
		public const short CNT_MUL_X4					= 2;	//x4

		public const short CNT_CLR_ASYNC				= 0;	//Asynchronous clear
		public const short CNT_CLR_SYNC					= 1;	//Synchronous clear

		//======================================================================
		// Function Prototype
		//======================================================================
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntInit(string Devicename, ref short Id);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntExit(short Id);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetErrorString(Int32 Err, StringBuilder szMsg);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntQueryDeviceName(short Index, ref short GroupID, ref short UnitID, ref short DeviceID, StringBuilder DeviceName, StringBuilder Device);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetDirectMode(short Id, short DirectMode);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetIOInterval(Int32 Interval);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntStartIO(short Id, short Start);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetRemoteStatus(short Id, ref byte Start, ref byte Status);

		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetZMode(short Id, short ChNo, short Mode);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetZLogic(short Id, short ChNo, short ZLogic);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSelectChannelSignal(short Id, short ChNo, short SigType);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetCountDirection(short Id, short ChNo, short Dir);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetOperationMode(short Id, short ChNo, short Phase, short Mul, short SyncClr);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetDigitalFilter(short Id, short ChNo, short FilterValue);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetPulseWidth(short Id, short ChNo, Int32 PlsWidth);

		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetZMode(short Id, short ChNo, ref short Mode);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetZLogic(short Id, short ChNo, ref short ZLogic);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetChannelSignal(short Id, short ChNo, ref short SigType);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetCountDirection(short Id, short ChNo, ref short Dir);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetOperationMode(short Id, short ChNo, ref short Phase, ref short Mul, ref short SyncClr);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetDigitalFilter(short Id, short ChNo, ref short FilterValue);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetPulseWidth(short Id, short ChNo, ref Int32 PlsWidth);

		[DllImport("CCAPCNT.DLL")]
		public static extern int CntStartCount(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] ChNo, short ChNum);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntStopCount(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] ChNo, short ChNum);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntPreset(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)]int[] PresetData);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetStartPreset(short Id, short ChNo, Int32 PresetData, short bEnable);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetStartPreset(short Id, short ChNo, ref Int32 PresetData, ref short bEnable);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntReadCount(short Id, [MarshalAs(UnmanagedType.LPArray)]short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)]int[] CntDat);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntReadStatus(short Id, short ChNo, ref short Sts);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntResetStatus(short Id, short ChNo, short Sts);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntInputDIByte(short Id, short Reserved, ref byte bData);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntSetCountMatchEvent(short Id, short ChNo, short RegNo, Int32 Count, short Start);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetCountMatchReg(short Id, short ChNo, short RegNo, ref Int32 Count);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntGetMaxChannels(short Id, ref short ChannelNum);
		[DllImport("CCAPCNT.DLL")]
		public static extern int CntResetChannel(short Id, IntPtr ChNo, short ChNum);

	}
}