﻿//============================================================-
// CCNTCS.CS
// Class file for CONTEC Counter device
//												CONTEC.Co.,Ltd.
//============================================================-

using	System;
using	System.Runtime.InteropServices;

public enum CcntConst
{
//-------------------------------------------------
// Device type
//-------------------------------------------------
	DEVICE_TYPE_ISA						=	0,		// ISA or C bus
	DEVICE_TYPE_PCI						=	1,		// PCI bus
	DEVICE_TYPE_PCMCIA					=	2,		// PCMCIA
	DEVICE_TYPE_USB						=	3,		// USB
	DEVICE_TYPE_FIT						=	4,		// FIT

//-------------------------------------------------
// Mode definition
//-------------------------------------------------
// Z Phase Mode
	CNT_ZPHASE_NOT_USE					=	1,
	CNT_ZPHASE_NEXT_ONE					=	2,
	CNT_ZPHASE_EVERY_TIME				=	3,
// Z Phase Logic
	CNT_ZLOGIC_POSITIVE					=	0,
	CNT_ZLOGIC_NEGATIVE					=	1,
// Signal Source
	CNT_SIGTYPE_ISOLATE					=	0,
	CNT_SIGTYPE_TTL						=	1,
	CNT_SIGTYPE_LINERECEIVER			=	2,
// Count Direction
	CNT_DIR_DOWN						=	0,
	CNT_DIR_UP							=	1,
// 1Phase/2Phase
	CNT_MODE_1PHASE						=	0,
	CNT_MODE_2PHASE						=	1,
	CNT_MODE_GATECONTROL				=	2,
// Mul
	CNT_MUL_X1							=	0,
	CNT_MUL_X2							=	1,
	CNT_MUL_X4							=	2,
// Sync Clear
	CNT_CLR_ASYNC						=	0,
	CNT_CLR_SYNC						=	1,
		
// Gate Control
	CNT_UPDOWN							=	1,
	CNT_GATECONTROL						=	0,

// Generic I/O Signal
	CNT_GENIO_LINERECEIVER				=	0,
	CNT_GENIO_TTL						=	1,
		
// Output Logic
	CNT_OEVENT_NEGATIVE					=	0,
	CNT_OEVENT_POSITIVE					=	1,

// Input Signal Event
	CNT_IEVENT_GENINP					=	0,
	CNT_IEVENT_STARTSTOP				=	1,
	CNT_IEVENT_PRESET					=	2,
	CNT_IEVENT_CLEAR					=	3,

// Output Hardware Event
	CNT_OEVENT_GENOUT					=	0x00,
	CNT_OEVENT_MATCH0					=	0x01,
	CNT_OEVENT_MATCH1					=	0x02,
	CNT_OEVENT_FLTERR					=	0x04,
	CNT_OEVENT_AIERR					=	0x08,
	CNT_OEVENT_ALMERR					=	0x10,

// RIZE/FALL
	CNT_INP_NONE						=	0,
	CNT_INP_RIZE						=	1,
	CNT_INP_FALL						=	2,

// Couunt Match Event
	CNT_MATCH_NONE						=	0,
	CNT_MATCH_PRESET					=	1,
	CNT_MATCH_CLEAR						=	2,

// Error Type
	CNT_AI_ERROR						=	1,
	CNT_FLT_ERROR						=	2,
	CNT_ALM_ERROR						=	3,

// Carry/Borrow
	CNT_CB_CARRY						=	1,
	CNT_CB_BORROW						=	2,

// Device Information
	ICNT_DEVICE_TYPE					=	0,		// device type.		Param1:short
	ICNT_NUMBER_OF_CH					=	1,		// channel number	Param1:int
	ICNT_BIT							=	2,		// bit number 		Param1:int
	ICNT_IS_DIGITAL_FILTER				=	3,		// digital filter	Param1:BOOL(True/False)
	ICNT_IS_DEFF						=	4,		// defferential replace of all-purpose input	Param1:BOOL(True/False)
	ICNT_CNTSOURCE						=	5,		// counter signal source	Param1:int(BIT0:TTL, BIT1:PHOTO, BIT2:LINERECEIVER)
// Cnt Source
	ICNT_CNTSOURCE_TTL					=	0x01,
	ICNT_CNTSOURCE_PHOTO				=	0x02,
	ICNT_CNTSOURCE_LINERECEIVER			=	0x04,
		
// SyncSignal
	CNTS_CLOCK_OUT_OFF					=	0,
	CNTS_CLOCK_OUT_ON					=	1,
	CNTS_START_OUT_OFF					=	0,
	CNTS_START_OUT_ON					=	1,
	CNTS_STOP_OUT_OFF					=	0,
	CNTS_STOP_OUT_ON					=	1,
		
// ExtSig
	CNTS_EXT_NOTUSE						=	0,		// Not Used
	CNTS_EXT_START_SOFT					=	1,		// Software Start
	CNTS_EXT_STOP_SOFT					=	2,		// Software Stop
	CNTS_EXT_CLOCK						=	3,		// Internal Clock
	CNTS_EXT_EXT_TRG					=	4,		// External Clock
	CNTS_EXT_START_EXT_RISE				=	5,		// External Start Rize
	CNTS_EXT_START_EXT_FALL				=	6,		// External Start Fall
	CNTS_EXT_STOP_EXT_RISE				=	7,		// External Stop Rize
	CNTS_EXT_STOP_EXT_FALL				=	8,		// External Stop Fall
	CNTS_EXT_CLOCK_ERROR				=	9,		// Sampling Clock Error
	CNTS_EXT_TRNSNUM					=	10,		// Stop Number of Transfer Data
	CNTS_EXT_CNTMATCH_CH0_REG0			=	11,		// Count Match Ch0:REG0
	CNTS_EXT_CNTMATCH_CH1_REG0			=	12,		// Count Match CH1:REG0
	CNTS_EXT_CNTMATCH_CH2_REG0			=	13,		// Count Match CH2:REG0
	CNTS_EXT_CNTMATCH_CH3_REG0			=	14,		// Count Match CH3:REG0
	CNTS_EXT_CNTMATCH_CH4_REG0			=	15,		// Count Match CH4:REG0
	CNTS_EXT_CNTMATCH_CH5_REG0			=	16,		// Count Match CH5:REG0
	CNTS_EXT_CNTMATCH_CH6_REG0			=	17,		// Count Match CH6:REG0
	CNTS_EXT_CNTMATCH_CH7_REG0			=	18,		// Count Match CH7:REG0
	CNTS_EXT_CNTMATCH_CH0_REG1			=	19,		// Count Match CH0:REG1
	CNTS_EXT_CNTMATCH_CH1_REG1			=	20,		// Count Match CH1:REG1
	CNTS_EXT_CNTMATCH_CH2_REG1			=	21,		// Count Match CH2:REG1
	CNTS_EXT_CNTMATCH_CH3_REG1			=	22,		// Count Match CH3:REG1
	CNTS_EXT_CNTMATCH_CH4_REG1			=	23,		// Count Match CH4:REG1
	CNTS_EXT_CNTMATCH_CH5_REG1			=	24,		// Count Match CH5:REG1
	CNTS_EXT_CNTMATCH_CH6_REG1			=	25,		// Count Match CH6:REG1
	CNTS_EXT_CNTMATCH_CH7_REG1			=	26,		// Count Match CH7:REG1
	
// Count Mode
	CNTS_MODE_NORMAL					=	0,		// Normal Counter
	CNTS_MODE_DIFF						=	1,		// Differencial Counter
// Sampling Start
	CNTS_START_SOFT						=	1,		// Software Start
	CNTS_START_EXT_RISE					=	2,		// External Start Rize
	CNTS_START_EXT_FALL					=	3,		// External Start Fall
	CNTS_START_CNTMATCH_CH0_REG0		=	4,		// Count Match CH0:REG0
	CNTS_START_CNTMATCH_CH1_REG0		=	5,		// Count Match CH1:REG0
	CNTS_START_CNTMATCH_CH2_REG0		=	6,		// Count Match CH2:REG0
	CNTS_START_CNTMATCH_CH3_REG0		=	7,		// Count Match CH3:REG0
	CNTS_START_CNTMATCH_CH4_REG0		=	8,		// Count Match CH4:REG0
	CNTS_START_CNTMATCH_CH5_REG0		=	9,		// Count Match CH5:REG0
	CNTS_START_CNTMATCH_CH6_REG0		=	10,		// Count Match CH6:REG0
	CNTS_START_CNTMATCH_CH7_REG0		=	11,		// Count Match CH7:REG0
	CNTS_START_CNTMATCH_CH0_REG1		=	12,		// Count Match CH0:REG1
	CNTS_START_CNTMATCH_CH1_REG1		=	13,		// Count Match CH1:REG1
	CNTS_START_CNTMATCH_CH2_REG1		=	14,		// Count Match CH2:REG1
	CNTS_START_CNTMATCH_CH3_REG1		=	15,		// Count Match CH3:REG1
	CNTS_START_CNTMATCH_CH4_REG1		=	16,		// Count Match CH4:REG1
	CNTS_START_CNTMATCH_CH5_REG1		=	17,		// Count Match CH5:REG1
	CNTS_START_CNTMATCH_CH6_REG1		=	18,		// Count Match CH6:REG1
	CNTS_START_CNTMATCH_CH7_REG1		=	19,		// Count Match CH7:REG1
	CNTS_START_EXTSIG_1					=	20,		// EXTSIG1
	CNTS_START_EXTSIG_2					=	21,		// EXTSIG2
	CNTS_START_EXTSIG_3					=	22,		// EXTSIG3
// Counter Start
	CNTS_START_CNT_NOT_WITH_SAMP		=	3,		// Counter Start not with Sampling Start
	CNTS_START_CNT_WITH_SAMP			=	4,		// Counter Start with Sampling Start
// Sampling Clock
	CNTS_CLOCK_CLOCK					=	1,		// Internal Clock
	CNTS_CLOCK_EXT_TRG					=	2,		// External Clock
	CNTS_CLOCK_EXTSIG_1					=	3,		// EXTSIG1
	CNTS_CLOCK_EXTSIG_2					=	4,		// EXTSIG2
	CNTS_CLOCK_EXTSIG_3					=	5,		// EXTSIG3
// Sampling Stop
	CNTS_STOP_SOFT						=	1,		// Software Stop
	CNTS_STOP_EXT_RISE					=	2,		// External Stop Rize
	CNTS_STOP_EXT_FALL					=	3,		// External Stop Fall
	CNTS_STOP_NUM						=	4,		// Stop with Number of Data
	CNTS_STOP_CNTMATCH_CH0_REG0			=	5,		// Count Match CH0:REG0
	CNTS_STOP_CNTMATCH_CH1_REG0			=	6,		// Count Match CH1:REG0
	CNTS_STOP_CNTMATCH_CH2_REG0			=	7,		// Count Match CH2:REG0
	CNTS_STOP_CNTMATCH_CH3_REG0			=	8,		// Count Match CH3:REG0
	CNTS_STOP_CNTMATCH_CH4_REG0			=	9,		// Count Match CH4:REG0
	CNTS_STOP_CNTMATCH_CH5_REG0			=	10,		// Count Match CH5:REG0
	CNTS_STOP_CNTMATCH_CH6_REG0			=	11,		// Count Match CH6:REG0
	CNTS_STOP_CNTMATCH_CH7_REG0			=	12,		// Count Match CH7:REG0
	CNTS_STOP_CNTMATCH_CH0_REG1			=	13,		// Count Match CH0:REG1
	CNTS_STOP_CNTMATCH_CH1_REG1			=	14,		// Count Match CH1:REG1
	CNTS_STOP_CNTMATCH_CH2_REG1			=	15,		// Count Match CH2:REG1
	CNTS_STOP_CNTMATCH_CH3_REG1			=	16,		// Count Match CH3:REG1
	CNTS_STOP_CNTMATCH_CH4_REG1			=	17,		// Count Match CH4:REG1
	CNTS_STOP_CNTMATCH_CH5_REG1			=	18,		// Count Match CH5:REG1
	CNTS_STOP_CNTMATCH_CH6_REG1			=	19,		// Count Match CH6:REG1
	CNTS_STOP_CNTMATCH_CH7_REG1			=	20,		// Count Match CH7:REG1
	CNTS_STOP_EXTSIG_1					=	21,		// EXTSIG1
	CNTS_STOP_EXTSIG_2					=	22,		// EXTSIG2
	CNTS_STOP_EXTSIG_3					=	23,		// EXTSIG3
// Clock Unit
	CNTS_TIM_UNIT_S						=	1,
	CNTS_TIM_UNIT_MS					=	2,
	CNTS_TIM_UNIT_US					=	3,
	CNTS_TIM_UNIT_NS					=	4,
// Counter Stop
	CNTS_STOP_CNT_NOT_WITH_SAMP			=	3,		// Counter Stop not with Sampling Stop
	CNTS_STOP_CNT_WITH_SAMP				=	4,		// Counter Stop with Sampling Stop
// Buffer
	CNTS_WRITE_ONCE						=	0,
	CNTS_WRITE_RING						=	1,
	
//-------------------------------------------------
// Message
//-------------------------------------------------
	CNTM_COUNTUP_CH0					=	0x1100,	// COUNTUP, CHANNEL No.0
	CNTM_COUNTUP_CH1					=	0x1101,	//         "           1
	CNTM_COUNTUP_CH2					=	0x1102,	//         "           2
	CNTM_COUNTUP_CH3					=	0x1103,	//         "           3
	CNTM_COUNTUP_CH4					=	0x1104,	//         "           4
	CNTM_COUNTUP_CH5					=	0x1105,	//         "           5
	CNTM_COUNTUP_CH6					=	0x1106,	//         "           6
	CNTM_COUNTUP_CH7					=	0x1107,	//         "           7
	
	CNTM_TIMER							=	0x1140,	// Timer
	CNTM_COUNTER_ERROR					=	0x1141,	// Counter error
	CNTM_CARRY_BORROW					=	0x1142,	// Carry/Borrow

	CNTM_SAMPLING_STOP					=	0x1150,	// Stop sampling
	CNTM_SAMPLING_COUNT					=	0x1151,	// Specified number sampling completed

// Count Match Direction(UP=1, DOWN=2)
	CNTM_DIR_UP							=	1,		// consistent with up count
	CNTM_DIR_DOWN						=	2,		// consistent with down count

//-------------------------------------------------
// Status
//-------------------------------------------------
	CNTS_STATUS_BMSTOP 					=	0x00000001,
	CNTS_STATUS_CNTSTART				=	0x00000002,
	CNTS_STATUS_CNTSTOP					=	0x00000004,
	CNTS_STATUS_TRGIN					=	0x00000008,
	CNTS_STATUS_OVERRUN					=	0x00000010,

	CNTS_STATUS_FIFOFULL				=	0x00000002,
	CNTS_STATUS_SGOVERIN				=	0x00000004,
	CNTS_STATUS_TRGERR					=	0x00000008,
	CNTS_STATUS_CLKERR					=	0x00000010,
	CNTS_STATUS_SLAVEHALT				=	0x00000020,
	CNTS_STATUS_MASTERHALT				=	0x00000040,
//-------------------------------------------------
// Error code
//-------------------------------------------------
// Initialize Error
// Common
	CNT_ERR_SUCCESS						=	0,		// normal completed
	CNT_ERR_INI_RESOURCE				=	1,		// invalid resource reference specified
	CNT_ERR_INI_INTERRUPT				=	2,		// invalid interrupt routine registered
	CNT_ERR_INI_MEMORY					=	3,		// invalid memory allocationed
	CNT_ERR_INI_REGISTRY				=	4,		// invalid registry accesse
	
	CNT_ERR_SYS_RECOVERED_FROM_STANDBY	=	7,		// Execute the function CntResetDevice to return from the standby mode.
	CNT_ERR_INI_NOT_FOUND_SYS_FILE		=	8,		// Because the ccnt.sys file is not found, it is not possible to initialize it.
	CNT_ERR_INI_DLL_FILE_VERSION		=	9,		// Because the version information on the ccnt.dll file cannot be retrieved, it is not possible to initialize it.
	CNT_ERR_INI_SYS_FILE_VERSION		=	10,		// Because the version information on the ccnt.sys file cannot be retrieved, it is not possible to initialize it.
	CNT_ERR_INI_NO_MATCH_DRV_VERSION	=	11,		// Because the version information of ccnt.dll is not corresponding to the version information of ccnt.sys, it is not possible to initialize it.
// CNT

// DLL Error
// Common
	CNT_ERR_DLL_DEVICE_NAME				=	10000,	// invalid device name specified.
	CNT_ERR_DLL_INVALID_ID				=	10001,	// invalid ID specified.
	CNT_ERR_DLL_CALL_DRIVER				=	10002,	// not call the driver.(Invalid device I/O controller)
	CNT_ERR_DLL_CREATE_FILE				=	10003,	// not make the file.(Invalid CreateFile)
	CNT_ERR_DLL_CLOSE_FILE				=	10004,	// not close the file.(Invalid CloseFile)
	CNT_ERR_DLL_CREATE_THREAD			=	10005,	// not create the thread.(Invalid CreateThread)
	CNT_ERR_INFO_INVALID_DEVICE			=	10050,	// invalid device infomation specified .Please check the spell.
	CNT_ERR_INFO_NOT_FIND_DEVICE		=	10051,	// not find the available device
	CNT_ERR_INFO_INVALID_INFOTYPE		=	10052,	// specified device infomation type beyond the limit
// CNT
	CNT_ERR_DLL_MODE					=	10100,	// mode beyond the limit
	CNT_ERR_DLL_BUFF_ADDRESS			=	10101,	// unfairness data buffer address
	CNT_ERR_DLL_HWND					=	10200,	// windows handler beyond the limit

// SYS Error
// Common
	CNT_ERR_SYS_MEMORY					=	20000,	// not secure memory
	CNT_ERR_SYS_NOT_SUPPORTED			=	20001,	// this board couldn't use this function
	CNT_ERR_SYS_BOARD_EXECUTING			=	20002,	// board is behaving, not execute
	CNT_ERR_SYS_USING_OTHER_PROCESS		=	20003,	// other process is using the device, not execute

	STATUS_SYS_USB_CRC					=	20020,	// the last data packet received from end point exist CRC error
	STATUS_SYS_USB_BTSTUFF				=	20021,	// the last data packet received from end point exist bit stuffing offense error
	STATUS_SYS_USB_DATA_TOGGLE_MISMATCH	=	20022,	// the last data packet received from end point exist toggle packet mismatch error
	STATUS_SYS_USB_STALL_PID			=	20023,	// end point return STALL packet identifier
	STATUS_SYS_USB_DEV_NOT_RESPONDING	=	20024,	// device don't respond to token(IN) ,don't support handshake
	STATUS_SYS_USB_PID_CHECK_FAILURE	=	20025,	
	STATUS_SYS_USB_UNEXPECTED_PID		=	20026,	// invalid packet identifier received
	STATUS_SYS_USB_DATA_OVERRUN			=	20027,	// end point return data quantity overrun
	STATUS_SYS_USB_DATA_UNDERRUN		=	20028,	// end point return data quantity underrun
	STATUS_SYS_USB_BUFFER_OVERRUN		=	20029,	// IN transmit specified buffer overrun
	STATUS_SYS_USB_BUFFER_UNDERRUN		=	20030,	// OUT transmit specified buffer underrun
	STATUS_SYS_USB_ENDPOINT_HALTED		=	20031,	// end point status is STALL, not transmit
	STATUS_SYS_USB_NOT_FOUND_DEVINFO	=	20032,	// not found device infomation
	STATUS_SYS_USB_ACCESS_DENIED		=	20033,	// hardware accesse denied
	STATUS_SYS_USB_INVALID_HANDLE		=	20034,	// invalid handle specified
// CNT
	CNT_ERR_SYS_MODE					=	20100,	// mode beyond the limit
	CNT_ERR_SYS_CH_NO					=	20201,	// channel No. beyond the limit
	CNT_ERR_SYS_CH_NUM					=	20202,	// channel numher beyond the limit
	CNT_ERR_SYS_CH_VALUE				=	20203,	// counter value beyond the limit
	CNT_ERR_SYS_REGNO					=	20204,	// comparison register No. beyond the limit
	CNT_ERR_SYS_TIMER_VALUE				=	20300,	// timer value beyond the limit
	CNT_ERR_SYS_OUTPUT_LOGIC			=	20301,	// The output logic of the control output signal is out of range
	CNT_ERR_SYS_EVENT_TYPE				=	20302,	// The kind of hardware event is out of range
	CNT_ERR_SYS_PULSE_WIDTH				=	20303,	// The coefficient of one-shot pulse width is out of range
	CNT_ERR_SYS_INPUT_LOGIC				=	20304,	// The input logic of control input signal is out of range
	CNT_ERR_SYS_OUTPUT_DATA				=	20305,	// The output data is out of range

// CNT Sampling
	CNT_ERR_SYS_START_SIGNAL			=	21000,	// The start signal is out of range
	CNT_ERR_SYS_STOP_SIGNAL				=	21001,	// The stop signal is out of range
	CNT_ERR_SYS_CLOCK_SIGNAL			=	21002,	// The clock signal is out of range
	CNT_ERR_SYS_SIGNAL					=	21003,	// The available signal is out of range
	CNT_ERR_SYS_COUNT_MODE				=	21004,	// The count mode is out of range
	CNT_ERR_SYS_START_TRG				=	21005,	// The start condition is out of range
	CNT_ERR_SYS_COUNT_START_MODE		=	21006,	// The count start condition is out of range
	CNT_ERR_SYS_CLOCK_TRG				=	21007,	// The clock condition is out of range
	CNT_ERR_SYS_STOP_TRG				=	21008,	// The stop condition is out of range
	CNT_ERR_SYS_CLOCK_VAL				=	21009,	// The clock value is out of range
	CNT_ERR_SYS_CLOCK_UNIT				=	21010,	// The unit of clock value is out of range
	CNT_ERR_SYS_STOP_NUM				=	21011,	// The time of sampling stop is out of range
	CNT_ERR_SYS_COUNT_STOP_MODE			=	21012,	// The count stop condition is out of range
	CNT_ERR_SYS_SCAN_NUM				=	21013,	// Frequency of scan of the buffer is out of range.
	CNT_ERR_SYS_RING					=	21014,	// Buffer repetition use setting is out of range.
	CNT_ERR_SYS_TRANS_COUNT				=	21015,	// The data transfer quantity is out of range.

	CNT_ERR_SYS_PARAM					=	21050,	// One of parameter is out of range

	CNT_ERR_SYS_BUFFER					=	21100,	// Because the buffer was too large, it was not possible to secure it
	CNT_ERR_SYS_LOCK_MEMORY				=	21101,	// The memory cannot be locked
	CNT_ERR_SYS_SEQUENCE				=	21102,	// Execution procedure error
}

namespace CcntCs
{
	unsafe public delegate void PCOUNTUPCALLBACK(short Id, int wParam, int lParam, void *Param);
	unsafe public delegate void PCOUNTERERRORCALLBACK(short Id, int wParam, int lParam, void *Param);
	unsafe public delegate void PCARRYBORROWCALLBACK(short Id, int wParam, int lParam, void *Param);
	unsafe public delegate void PTIMERCALLBACK(short Id, int wParam, int lParam, void *Param);

	unsafe public delegate void PSAMPLINGSTOPCALLBACK(short Id, int wParam, int lParam, void *Param);
	unsafe public delegate void PSAMPLINGCOUNTCALLBACK(short Id, int wParam, int lParam, void *Param);

	public class Ccnt
	{
		// Common function
		[DllImport("ccnt.dll")] static extern int CntInit(string DeviceName, ref short Id);
		[DllImport("ccnt.dll")] static extern int CntExit(short Id);
		[DllImport("ccnt.dll")] static extern int CntResetDevice(short Id);
		[DllImport("ccnt.dll")] static extern int CntGetErrorString(int ErrorCode, System.Text.StringBuilder ErrorString);

		// Setting function (set)
		[DllImport("ccnt.dll")] static extern int CntSetZMode(short Id, short ChNo, short Mode);
		[DllImport("ccnt.dll")] static extern int CntSetZLogic(short Id, short ChNo, short ZLogic);
		[DllImport("ccnt.dll")] static extern int CntSelectChannelSignal(short Id, short ChNo, short SigType);
		[DllImport("ccnt.dll")] static extern int CntSetCountDirection(short Id, short ChNo, short Dir);
		[DllImport("ccnt.dll")] static extern int CntSetOperationMode(short Id, short ChNo, short Phase, short Mul, short SyncClr);
		[DllImport("ccnt.dll")] static extern int CntSetDigitalFilter(short Id, short ChNo, short FilterValue);
		[DllImport("ccnt.dll")] static extern int CntSetPulseWidth(short Id, short ChNo, short PlsWidth);
		[DllImport("ccnt.dll")] static extern int CntSetDIType(short Id, short ChNo, short InputType);
		[DllImport("ccnt.dll")] static extern int CntSetOutputHardwareEvent(short Id, short ChNo, short OutputLogic, uint EventType, short PulseWidth);
		[DllImport("ccnt.dll")] static extern int CntSetInputHardwareEvent(short Id, short ChNo, uint EventType, short RF0, short RF1, short Reserved);
		[DllImport("ccnt.dll")] static extern int CntSetCountMatchHardwareEvent(short Id, short ChNo, short RegisterNo, uint EventType, short Reserved);
		[DllImport("ccnt.dll")] static extern int CntSetPresetRegister(short Id, short ChNo, uint PresetData, short Reserved);

		// Setting function (get)
		[DllImport("ccnt.dll")] static extern int CntGetZMode(short Id, short ChNo, ref short Mode);
		[DllImport("ccnt.dll")] static extern int CntGetZLogic(short Id, short ChNo,  ref short ZLogic);
		[DllImport("ccnt.dll")] static extern int CntGetChannelSignal(short Id, short ChNo, ref short SigType);
		[DllImport("ccnt.dll")] static extern int CntGetCountDirection(short Id, short ChNo, ref short Dir);
		[DllImport("ccnt.dll")] static extern int CntGetOperationMode(short Id, short ChNo, ref short Phase, ref short Mul, ref short SyncClr);
		[DllImport("ccnt.dll")] static extern int CntGetDigitalFilter(short Id, short ChNo, ref short FilterValue);
		[DllImport("ccnt.dll")] static extern int CntGetPulseWidth(short Id, short ChNo, ref short PlsWidth);

		// Counter function
		[DllImport("ccnt.dll")] static extern int CntPreset(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] PresetData);
		[DllImport("ccnt.dll")] static extern int CntStartCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum);
		[DllImport("ccnt.dll")] static extern int CntStopCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum);
		[DllImport("ccnt.dll")] static extern int CntReadCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] CntDat);
		[DllImport("ccnt.dll")] static extern int CntReadStatus(short Id, short ChNo, ref short Sts);
		[DllImport("ccnt.dll")] static extern int CntReadStatusEx(short Id, short ChNo, ref uint Sts);
		[DllImport("ccnt.dll")] static extern int CntZeroClearCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum);

		// Count match function
		[DllImport("ccnt.dll")] static extern int CntNotifyCountUp(short Id, short ChNo, short RegNo, uint Count, IntPtr hWnd);
		[DllImport("ccnt.dll")] static extern int CntStopNotifyCountUp(short Id, short ChNo, short RegNo);
		[DllImport("ccnt.dll")] static extern int CntNotifyCounterError(short Id, IntPtr hWnd);
		[DllImport("ccnt.dll")] static extern int CntStopNotifyCounterError(short Id);
		[DllImport("ccnt.dll")] static extern int CntNotifyCarryBorrow(short Id, IntPtr hWnd);
		[DllImport("ccnt.dll")] static extern int CntStopNotifyCarryBorrow(short Id);
		[DllImport("ccnt.dll")] unsafe static extern int CntCountUpCallbackProc(short Id, IntPtr SetCountUpCallBackProc, void *Param);
		[DllImport("ccnt.dll")] unsafe static extern int CntCounterErrorCallbackProc(short Id , IntPtr CounterErrorCallBackProc , void *Param);
		[DllImport("ccnt.dll")] unsafe static extern int CntCarryBorrowCallbackProc(short Id, IntPtr CarryBorrowCallBackProc, void *Param);
		[DllImport("ccnt.dll")] unsafe static extern int CntTimerCallbackProc(short Id , IntPtr TimerCallBackProc , void *Param);
		[DllImport("ccnt.dll")] unsafe static extern int CntNotifyTimer(short Id, uint TimeValue, IntPtr hWnd);
		[DllImport("ccnt.dll")] unsafe static extern int CntStopNotifyTimer(short Id);

		// Timer function
		//[DllImport("ccnt.dll")] static extern int CntNotifyTimeUp(short Id, uint TimeValue, int hWnd);
		//[DllImport("ccnt.dll")] static extern int CntStopNotifyTimeUp(short Id);

		// General purpose input function
		[DllImport("ccnt.dll")] static extern int CntInputDIByte(short Id, short Reserved, ref byte bData);
		[DllImport("ccnt.dll")] static extern int CntOutputDOBit(short Id, short ChNo, short Reserved, byte OutData);

		// Information function
		[DllImport("ccnt.dll")] static extern int CntGetDeviceInfo(string Device, short InfoType, ref int Param1, ref int Param2, ref int Param3);
		[DllImport("ccnt.dll")] static extern int CntQueryDeviceName(short Index, System.Text.StringBuilder DeviceName, System.Text.StringBuilder Device);
		[DllImport("ccnt.dll")] static extern int CntGetDeviceType(string Device, ref short DeviceType);
		[DllImport("ccnt.dll")] static extern int CntGetMaxChannels(short Id, ref short ChannelNum);
		[DllImport("ccnt.dll")] static extern int CntGetNetCommunicationInfo(short Id, short InfoType, ref int InfoData);
		
		// Test function
		[DllImport("ccnt.dll")] static extern int CntSetTestPulse(short Id, short Internal, short Out, uint Reserved);
		
		// Sampling mode function
		[DllImport("ccnt.dll")] static extern int CntSetSyncSignal(short Id, short StartSignal, short StopSignal, short ClockSignal);
		[DllImport("ccnt.dll")] static extern int CntSetStandAlone(short Id);
		[DllImport("ccnt.dll")] static extern int CntSetMaster(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt);
		[DllImport("ccnt.dll")] static extern int CntSetSlave(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt);
		[DllImport("ccnt.dll")] static extern int CntSetCounterMode(short Id, short ChNo, short CountMode);
		// Sampling condition function
		[DllImport("ccnt.dll")] static extern int CntSetSamplingStartTrigger(short Id, short StartTrg, short CountStartMode, short CountChNum);
		[DllImport("ccnt.dll")] static extern int CntSetSamplingClockTrigger(short Id, short ClockTrg);
		[DllImport("ccnt.dll")] static extern int CntSetSamplingStopTrigger(short Id, short StopTrg, short CountStopMode, short CountChNum);
		[DllImport("ccnt.dll")] static extern int CntSetSamplingInternalClock(short Id, uint Clock, short Unit);
		[DllImport("ccnt.dll")] static extern int CntSetSamplingStopNumber(short Id, uint StopNum);
		// Sampling message function
		[DllImport("ccnt.dll")] static extern int CntNotifySamplingStop(short Id, IntPtr hWnd);
		[DllImport("ccnt.dll")] unsafe static extern int CntSamplingStopCallbackProc(short Id, IntPtr SamplingStopCallBackProc, void *Param);
		[DllImport("ccnt.dll")] static extern int CntNotifySamplingCount(short Id, int Count, IntPtr hWnd);
		[DllImport("ccnt.dll")] unsafe static extern int CntSamplingCountCallbackProc(short Id, IntPtr SamplingCountCallBackProc, void *Param);
		// Sampling operation function
		[DllImport("ccnt.dll")] static extern int CntSamplingFifoReset(short Id);
		[DllImport("ccnt.dll")] static extern int CntSamplingSetBuffer(short Id, IntPtr Buffer, short ChNum, uint ScanNum, short IsRing);
		[DllImport("ccnt.dll")] static extern int CntSamplingStart(short Id);
		[DllImport("ccnt.dll")] static extern int CntSamplingStop(short Id);
		// Sampling status function
		[DllImport("ccnt.dll")] static extern int CntGetSamplingStatus(short Id, ref uint Status, ref uint Err);
		[DllImport("ccnt.dll")] static extern int CntGetSamplingCount(short Id, ref uint ScanCount, ref uint ScanCarry);
		[DllImport("ccnt.dll")] static extern int CntGetSamplingWritePointer(short Id, ref uint WritePointer, ref uint ScanCount, ref uint ScanCarry);

		// Constructor
		public Ccnt()
		{
		}

		// Common function
		public int Init(string DeviceName, out short Id)
		{
			Id = 0;
			int ret	= CntInit(DeviceName, ref Id);
			return	ret;
		}

		public int Exit(short Id)
		{
			int ret = CntExit(Id);
			return ret;
		}

		public int ResetDevice(short Id)
		{
			int ret = CntResetDevice(Id);
			return ret;
		}

		public int GetErrorString(int ErrorCode, out string ErrorString)
		{
			ErrorString = new String('0', 1);
			System.Text.StringBuilder errorstring = new System.Text.StringBuilder(256);
			int ret = CntGetErrorString(ErrorCode, errorstring);
			if(ret == 0)
			{
				ErrorString = errorstring.ToString();
			}
			return ret;
		}

		// Setting function (set)
		public int SetZMode(short Id, short ChNo, short Mode)
		{
			int ret = CntSetZMode(Id, ChNo, Mode);
			return ret;
		}

		public int SetZLogic(short Id, short ChNo, short ZLogic)
		{
			int ret = CntSetZLogic(Id, ChNo, ZLogic);
			return ret;
		}

		public int SelectChannelSignal(short Id, short ChNo, short SigType)
		{
			int ret = CntSelectChannelSignal(Id, ChNo, SigType);
			return ret;
		}

		public int SetCountDirection(short Id, short ChNo, short Dir)
		{
			int ret = CntSetCountDirection(Id, ChNo, Dir);
			return ret;
		}

		public int SetOperationMode(short Id, short ChNo, short Phase, short Mul, short SyncClr)
		{
			int ret = CntSetOperationMode(Id, ChNo, Phase, Mul, SyncClr);
			return ret;
		}

		public int SetDigitalFilter(short Id, short ChNo, short FilterValue)
		{
			int ret = CntSetDigitalFilter(Id, ChNo, FilterValue);
			return ret;
		}

		public int SetPulseWidth(short Id, short ChNo, short PlsWidth)
		{
			int ret = CntSetPulseWidth(Id, ChNo, PlsWidth);
			return ret;
		}
		
		public int SetDIType(short Id, short ChNo, short InputType)
		{
			int ret = CntSetDIType(Id, ChNo, InputType);
			return ret;
		}

		public int SetOutputHardwareEvent(short Id, short ChNo, short OutputLogic, uint EventType, short PulseWidth)
		{
			int ret = CntSetOutputHardwareEvent(Id, ChNo, OutputLogic, EventType, PulseWidth);
			return ret;
		}

		public int SetInputHardwareEvent(short Id, short ChNo, uint EventType, short RF0, short RF1, short Reserved)
		{
			int ret = CntSetInputHardwareEvent(Id, ChNo, EventType, RF0, RF1, Reserved);
			return ret;
		}

		public int SetCountMatchHardwareEvent(short Id, short ChNo, short RegisterNo, uint EventType, short Reserved)
		{
			int ret = CntSetCountMatchHardwareEvent(Id, ChNo, RegisterNo, EventType, Reserved);
			return ret;
		}
		
		public int SetPresetRegister(short Id, short ChNo, uint PresetData, short Reserved)
		{
			int ret = CntSetPresetRegister(Id, ChNo, PresetData, Reserved);
			return ret;
		}

		// Setting function (get)
		public int GetZMode(short Id, short ChNo, out short Mode)
		{
			Mode = 0;
			int ret = CntGetZMode(Id, ChNo, ref Mode);
			return ret;
		}

		public int GetZLogic(short Id, short ChNo, out short ZLogic)
		{
			ZLogic = 0;
			int ret = CntGetZLogic(Id, ChNo, ref ZLogic);
			return ret;
		}

		public int GetChannelSignal(short Id, short ChNo, out short SigType)
		{
			SigType = 0;
			int ret = CntGetChannelSignal(Id, ChNo, ref SigType);
			return ret;
		}

		public int GetCountDirection(short Id, short ChNo, out short Dir)
		{
			Dir = 0;
			int ret = CntGetCountDirection(Id, ChNo, ref Dir);
			return ret;
		}

		public int GetOperationMode(short Id, short ChNo, out short Phase, out short Mul, out short SyncClr)
		{
			Phase = 0;
			Mul = 0;
			SyncClr = 0;
			int ret = CntGetOperationMode(Id, ChNo, ref Phase, ref Mul, ref SyncClr);
			return ret;
		}

		public int GetDigitalFilter(short Id, short ChNo, out short FilterValue)
		{
			FilterValue = 0;
			int ret = CntGetDigitalFilter(Id, ChNo, ref FilterValue);
			return ret;
		}

		public int GetPulseWidth(short Id, short ChNo, out short PlsWidth)
		{
			PlsWidth = 0;
			int ret = CntGetPulseWidth(Id, ChNo, ref PlsWidth);
			return ret;
		}

		// Counter function
		public int Preset(short Id, short[] ChNo, short ChNum, uint[] PresetData)
		{
			int ret = CntPreset(Id, ChNo, ChNum, PresetData);
			return ret;
		}

		public int StartCount(short Id, short[] ChNo, short ChNum)
		{
			int ret = CntStartCount(Id, ChNo, ChNum);
			return ret;
		}

		public int StopCount(short Id, short[] ChNo, short ChNum)
		{
			int ret = CntStopCount(Id, ChNo, ChNum);
			return ret;
		}

		public int ReadCount(short Id, short[] ChNo, short ChNum, uint[] CntDat)
		{
			int ret = CntReadCount(Id, ChNo, ChNum, CntDat);
			return ret;
		}

		public int ReadStatus(short Id, short ChNo, out short Sts)
		{
			Sts = 0;
			int ret =  CntReadStatus(Id, ChNo, ref Sts);
			return ret;
		}

		public int ReadStatusEx(short Id, short ChNo, out uint Sts)
		{
			Sts = 0;
			int ret =  CntReadStatusEx(Id, ChNo, ref Sts);
			return ret;
		}

		public int ZeroClearCount(short Id, short[] ChNo, short ChNum)
		{
			int ret =  CntZeroClearCount(Id, ChNo, ChNum);
			return ret;
		}

		// Count match function
		public int NotifyCountUp(short Id, short ChNo, short RegNo, uint Count, IntPtr hWnd)
		{
			int ret =  CntNotifyCountUp(Id, ChNo, RegNo, Count, hWnd);
			return ret;
		}

		public int StopNotifyCountUp(short Id, short ChNo, short RegNo)
		{
			int ret =  CntStopNotifyCountUp(Id, ChNo, RegNo);
			return ret;
		}

		public int NotifyCounterError(short Id, IntPtr hWnd)
		{
			int ret = CntNotifyCounterError(Id, hWnd);
			return ret;
		}
		
		public int StopNotifyCounterError(short Id)
		{
			int ret = CntStopNotifyCounterError(Id);
			return ret;
		}

		public int NotifyCarryBorrow(short Id, IntPtr hWnd)
		{
			int ret =  CntNotifyCarryBorrow(Id, hWnd);
			return ret;
		}
		
		public int StopNotifyCarryBorrow(short Id)
		{
			int ret = CntStopNotifyCarryBorrow(Id);
			return ret;
		}

		unsafe public int CountUpCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntCountUpCallbackProc(Id, pCallBack, Param);
			return ret;
		}

		unsafe public int CounterErrorCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntCounterErrorCallbackProc(Id, pCallBack, Param);
			return ret;
		}

		unsafe public int CarryBorrowCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntCarryBorrowCallbackProc(Id, pCallBack, Param);
			return ret;
		}

		unsafe public int TimerCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntTimerCallbackProc(Id, pCallBack, Param);
			return ret;
		}

		public int NotifyTimer(short Id, uint TimeValue, IntPtr hWnd)
		{
			int ret = CntNotifyTimer(Id, TimeValue, hWnd);
			return ret;
		}

		public int StopNotifyTimer(short Id)
		{
			int ret = CntStopNotifyTimer(Id);
			return ret;
		}

		// General purpose input function
		public int InputDIByte(short Id, short Reserved, out byte bData)
		{
			bData = 0;
			int ret = CntInputDIByte(Id, Reserved, ref bData);
			return ret;
		}

		public int OutputDOBit(short Id, short ChNo, short Reserved, byte OutData)
		{
			int ret = CntOutputDOBit(Id, ChNo, Reserved, OutData);
			return ret;
		}

		// Information function
		public int GetDeviceInfo(string Device, short InfoType, out int Param1, out int Param2, out int Param3)
		{
			Param1 = 0;
			Param2 = 0;
			Param3 = 0;
			int ret = CntGetDeviceInfo(Device, InfoType, ref Param1, ref Param2, ref Param3);
			return ret;
		}

		public int QueryDeviceName(short Index, out string DeviceName, out string Device)
		{
			DeviceName = new String('0', 1);
			Device = new String('0', 1);
			System.Text.StringBuilder devicename = new System.Text.StringBuilder(256);
			System.Text.StringBuilder device = new System.Text.StringBuilder(256);
			int ret = CntQueryDeviceName(Index, devicename, device);
			if(ret == 0)
			{
				DeviceName = devicename.ToString();
				Device = device.ToString();
			}
			return ret;
		}

		public int GetDeviceType(string Device, out short DeviceType)
		{
			DeviceType = 0;
			int ret = CntGetDeviceType(Device, ref DeviceType);
			return ret;
		}

		public int GetMaxChannels(short Id, out short ChannelNum)
		{
			ChannelNum = 0;
			int ret = CntGetMaxChannels(Id, ref ChannelNum);
			return ret;
		}
		
		public int GetNetCommunicationInfo(short Id, short InfoType, out int InfoData)
		{
			InfoData = 0;
			int ret = CntGetNetCommunicationInfo(Id, InfoType, ref InfoData);
			return ret;
		}
		
		// Test function
		public int SetTestPulse(short Id, short Internal, short Out, uint Reserved)
		{
			int ret = CntSetTestPulse(Id, Internal, Out, Reserved);
			return ret;
		}
		
		// Sampling mode function
		public int SetSyncSignal(short Id, short StartSignal, short StopSignal, short ClockSignal)
		{
			int ret = CntSetSyncSignal(Id, StartSignal, StopSignal, ClockSignal);
			return ret;
		}
		
		public int SetStandAlone(short Id)
		{
			int ret = CntSetStandAlone(Id);
			return ret;
		}
		
		public int SetMaster(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt)
		{
			int ret = CntSetMaster(Id, ExtSig1, ExtSig2, ExtSig3, MasterHalt, SlaveHalt);
			return ret;
		}
		
		public int SetSlave(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt)
		{
			int ret = CntSetSlave(Id, ExtSig1, ExtSig2, ExtSig3, MasterHalt, SlaveHalt);
			return ret;
		}
		
		public int SetCounterMode(short Id, short ChNo, short CountMode)
		{
			int ret = CntSetCounterMode(Id, ChNo, CountMode);
			return ret;
		}
		
		// Sampling condition function
		public int SetSamplingStartTrigger(short Id, short StartTrg, short CountStartMode, short CountChNum)
		{
			int ret = CntSetSamplingStartTrigger(Id, StartTrg, CountStartMode, CountChNum);
			return ret;
		}
		
		public int SetSamplingClockTrigger(short Id, short ClockTrg)
		{
			int ret = CntSetSamplingClockTrigger(Id, ClockTrg);
			return ret;
		}
		
		public int SetSamplingStopTrigger(short Id, short StopTrg, short CountStopMode, short CountChNum)
		{
			int ret = CntSetSamplingStopTrigger(Id, StopTrg, CountStopMode, CountChNum);
			return ret;
		}
		
		public int SetSamplingInternalClock(short Id, uint Clock, short Unit)
		{
			int ret = CntSetSamplingInternalClock(Id, Clock, Unit);
			return ret;
		}
		
		public int SetSamplingStopNumber(short Id, uint StopNum)
		{
			int ret = CntSetSamplingStopNumber(Id, StopNum);
			return ret;
		}
		
		// Sampling message function
		public int NotifySamplingStop(short Id, IntPtr hWnd)
		{
			int ret = CntNotifySamplingStop(Id, hWnd);
			return ret;
		}
		
		unsafe public int SamplingStopCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntSamplingStopCallbackProc(Id, pCallBack, Param);
			return ret;
		}
		
		public int NotifySamplingCount(short Id, int Count, IntPtr hWnd)
		{
			int ret = CntNotifySamplingCount(Id, Count, hWnd);;
			return ret;
		}
		
		unsafe public int SamplingCountCallbackProc(short Id, IntPtr pCallBack, void *Param)
		{
			int ret = CntSamplingCountCallbackProc(Id, pCallBack, Param);
			return ret;
		}
		
		// Sampling operation function
		public int SamplingFifoReset(short Id)
		{
			int ret = CntSamplingFifoReset(Id);
			return ret;
		}
		
		public int SamplingSetBuffer(short Id, IntPtr Buffer, short ChNum, uint ScanNum, short IsRing)
		{
			int ret = CntSamplingSetBuffer(Id, Buffer, ChNum, ScanNum, IsRing);
			return ret;
		}
		
		public int SamplingStart(short Id)
		{
			int ret = CntSamplingStart(Id);
			return ret;
		}
		
		public int SamplingStop(short Id)
		{
			int ret = CntSamplingStop(Id);
			return ret;
		}
		
		// Sampling status function
		public int GetSamplingStatus(short Id, out uint Status, out uint Err)
		{
			Status = 0;
			Err = 0;
			int ret = CntGetSamplingStatus(Id, ref Status, ref Err);
			return ret;
		}
		
		public int GetSamplingCount(short Id, out uint ScanCount, out uint ScanCarry)
		{
			ScanCount = 0;
			ScanCarry = 0;
			int ret = CntGetSamplingCount(Id, ref ScanCount, ref ScanCarry);
			return ret;
		}
		
		public int GetSamplingWritePointer(short Id, out uint WritePointer, out uint ScanCount, out uint ScanCarry)
		{
			WritePointer = 0;
			ScanCount = 0;
			ScanCarry = 0;
			int ret = CntGetSamplingWritePointer(Id, ref WritePointer, ref ScanCount, ref ScanCarry);
			return ret;
		}

	}
}
