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

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


		//======================================================================
		// Error Code
		//======================================================================
		public const short	CNT_ERR_SUCCESS					= 0;		//I 
		public const short	CNT_ERR_INI_MEMORY				= 3;		//̊蓖ĂɎs܂B 
		public const short	CNT_ERR_INI_REGISTRY			= 4;		//ݒt@C̃ANZXɎs܂B 
		public const short	CNT_ERR_DLL_DEVICE_NAME			= 10000;	//ݒt@Cɓo^ĂȂfoCXw肳܂B 
		public const short	CNT_ERR_DLL_INVALID_ID			= 10001;	//IDw肳܂B 
		public const short	CNT_ERR_START					= 10002;	//W[NɃG[܂B
		public const short	CNT_ERR_DLL_CREATE_FILE			= 10003;	//nh̎擾ɍ쐬Ɏs܂B 
		public const short	CNT_ERR_DLL_CLOSE_FILE			= 10004;	//nh̃N[YɎs܂B 
		public const short	CNT_ERR_ACCESS_RIGHT			= 10005;	//ANZXG[łB
		public const short	CNT_ERR_DLL_TIMEOUT 			= 10006;	//ʐM^CAEg܂B
		public const short	CNT_ERR_COMPOSITION				= 10007;	//@\G[łB
		public const short	CNT_ERR_INFO_INVALID_DEVICE		= 10050;	//w肵foCX̂܂BXymFĂB 
		public const short	CNT_ERR_INFO_NOT_FIND_DEVICE	= 10051;	//p\ȃfoCX܂B 
		public const short	CNT_ERR_DLL_MODE				= 10100;	//[hw\͈͊Oł 
		public const short	CNT_ERR_DLL_BUFF_ADDRESS		= 10101;	//f[^obt@AhXsłB 
		public const short	CNT_ERR_SYS_NOT_SUPPORTED		= 20001;	//̃foCXł͂̊֐͎gpł܂B
		public const short	CNT_ERR_SYS_CH_NO				= 20201;	//`lԍݒ\͈͊Oł 
		public const short	CNT_ERR_SYS_CH_NUM				= 20202;	//`lݒ\͈͊Oł 
		public const short	CNT_ERR_SYS_CH_VALUE			= 20203;	//JE^lݒ\͈͊Oł 

		//======================================================================
		// Parameter Definition
		//======================================================================
		public const short	CNT_ZPHASE_NOT_USE				= 1;	//gp
		public const short	CNT_ZPHASE_NEXT_ONE				= 2;	//1
		public const short	CNT_ZPHASE_EVERY_TIME			= 3;	//

		public const short	CNT_ZLOGIC_POSITIVE				= 0;	//_
		public const short	CNT_ZLOGIC_NEGATIVE				= 1;	//_

		public const short	CNT_SIGTYPE_ISOLATE				= 0;	//tHgJv≏
		public const short	CNT_SIGTYPE_TTL					= 1;	//TTL (\)
		public const short	CNT_SIGTYPE_LINERECEIVER		= 2;	// (\)

		public const short	CNT_DIR_DOWN					= 0;	//_EJEg
		public const short	CNT_DIR_UP						= 1;	//AbvJEg

		public const short	CNT_MODE_1PHASE					= 0;	//P
		public const short	CNT_MODE_2PHASE					= 1;	//2
		public const short	CNT_MODE_GATECONTROL			= 2;	//Q[gRg[

		public const short	CNT_MUL_X1						= 0;	//1{
		public const short	CNT_MUL_X2						= 1;	//2{
		public const short	CNT_MUL_X4						= 2;	//4{

		public const short	CNT_CLR_ASYNC					= 0;	//񓯊NA
		public const short	CNT_CLR_SYNC					= 1;	//NA

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

	}
}