﻿//============================================================-
//	CDIOCS.CS
//	Class file for CONTEC Digital I/O device
//												CONTEC.Co.,Ltd.
//============================================================-

using	System;
using	System.Runtime.InteropServices;

public enum CdioConst
{
//-------------------------------------------------
//	Type definition
//-------------------------------------------------
	DEVICE_TYPE_ISA							=	0,	//	ISA or C bus
	DEVICE_TYPE_PC							=	1,	//	PCI bus
	DEVICE_TYPE_PCMCIA						=	2,	//	PCMCIA
	DEVICE_TYPE_USB							=	3,	//	USB
	DEVICE_TYPE_FIT							=	4,	//	FIT
	DEVICE_TYPE_CARDBUS						=	5,	//	CardBus
	DEVICE_TYPE_NET							=	20,	//	Ethernet, Wireless etc

//-------------------------------------------------
//	Parameters
//-------------------------------------------------
//	I/O(for Sample)
	DIO_MAX_ACCS_PORTS						=	256,
//	DioNotifyInt:Logic
	DIO_INT_NONE							=	0,
	DIO_INT_RISE							=	1,
	DIO_INT_FALL							=	2,
//	DioNotifyTrg:TrgKind
	DIO_TRG_RISE							=	1,
	DIO_TRG_FALL							=	2,
//	Message
	DIOM_INTERRUPT							=	0x1300,
	DIOM_TRIGGER							=	0x1340,
	DIO_DMM_STOP							=	0x1350,
	DIO_DMM_COUNT							=	0x1360,
//	Device Information
	IDIO_DEVICE_TYPE						=	0,	//	device type.							Param1:short
	IDIO_NUMBER_OF_8255						=	1,	//	Number of 8255 chip.					Param1:int
	IDIO_IS_8255_BOARD						=	2,	//	Is 8255	board?							Param1:BOOL(True/False)
	IDIO_NUMBER_OF_DI_BIT					=	3,	//	Number of digital input bit.			Param1:int
	IDIO_NUMBER_OF_DO_BIT					=	4,	//	Number of digital outout bit.			Param1:int
	IDIO_NUMBER_OF_DI_PORT					=	5,	//	Number of digital input	port.			Param1:int
	IDIO_NUMBER_OF_DO_PORT					=	6,	//	Number of digital output port.			Param1:int
	IDIO_IS_POSITIVE_LOGIC					=	7,	//	Is positive logic?						Param1:BOOL(True/False)
	IDIO_IS_ECHO_BACK						=	8,	//	Can echo back output port?				Param1:BOOL(True/False)
	IDIO_IS_DIRECTION						=	9,	//	Can DioSetIoDirection function be used?	Param1:int(1:true, 0:false)
	IDIO_IS_FILTER							=	10,	//	Can digital filter be used?				Param1:int(1:true, 0:false)
	IDIO_NUMBER_OF_INT_BIT					=	11,	//	Number of interrupt bit.				Param1:short
//	Net Communication Information
	IDIO_NET_TYPE_REGISTER_PROCESS			=	0,	//	Process is registered?

//	DM
//	Direction
	PI_32									=	1,
	PO_32									=	2,
	PIO_1616								=	3,
	DIODM_DIR_IN							=	0x1,
	DIODM_DIR_OUT							=	0x2,

//  Start
	DIODM_START_SOFT						=	1,
	DIODM_START_EXT_RISE					=	2,
	DIODM_START_EXT_FALL					=	3,
	DIODM_START_PATTERN						=	4,
	DIODM_START_EXTSIG_1					=	5,
	DIODM_START_EXTSIG_2					=	6,
	DIODM_START_EXTSIG_3					=	7,

//  Clock
	DIODM_CLK_CLOCK							=	1,
	DIODM_CLK_EXT_TRG						=	2,
	DIODM_CLK_HANDSHAKE						=	3,
	DIODM_CLK_EXTSIG_1						=	4,
	DIODM_CLK_EXTSIG_2						=	5,
	DIODM_CLK_EXTSIG_3						=	6,

//  Internal Clock
	DIODM_TIM_UNIT_S						=	1,
	DIODM_TIM_UNIT_MS						=	2,
	DIODM_TIM_UNIT_US						=	3,
	DIODM_TIM_UNIT_NS						=	4,

//  Stop
	DIODM_STOP_SOFT							=	1,
	DIODM_STOP_EXT_RISE						=	2,
	DIODM_STOP_EXT_FALL						=	3,
	DIODM_STOP_NUM							=	4,
	DIODM_STOP_EXTSIG_1						=	5,
	DIODM_STOP_EXTSIG_2						=	6,
	DIODM_STOP_EXTSIG_3						=	7,

//	ExtSig
	DIODM_EXT_START_SOFT_IN					=	1,
	DIODM_EXT_STOP_SOFT_IN					=	2,
	DIODM_EXT_CLOCK_IN						=	3,
	DIODM_EXT_EXT_TRG_IN					=	4,
	DIODM_EXT_START_EXT_RISE_IN				=	5,
	DIODM_EXT_START_EXT_FALL_IN				=	6,
	DIODM_EXT_START_PATTERN_IN				=	7,
	DIODM_EXT_STOP_EXT_RISE_IN				=	8,
	DIODM_EXT_STOP_EXT_FALL_IN				=	9,
	DIODM_EXT_CLOCK_ERROR_IN				=	10,
	DIODM_EXT_HANDSHAKE_IN					=	11,
	DIODM_EXT_TRNSNUM_IN					=	12,

	DIODM_EXT_START_SOFT_OUT				=	101,
	DIODM_EXT_STOP_SOFT_OUT					=	102,
	DIODM_EXT_CLOCK_OUT						=	103,
	DIODM_EXT_EXT_TRG_OUT					=	104,
	DIODM_EXT_START_EXT_RISE_OUT			=	105,
	DIODM_EXT_START_EXT_FALL_OUT			=	106,
	DIODM_EXT_STOP_EXT_RISE_OUT				=	107,
	DIODM_EXT_STOP_EXT_FALL_OUT				=	108,
	DIODM_EXT_CLOCK_ERROR_OUT				=	109,
	DIODM_EXT_HANDSHAKE_OUT					=	110,
	DIODM_EXT_TRNSNUM_OUT					=	111,

//	Status
	DIODM_STATUS_BMSTOP						=	0x1,
	DIODM_STATUS_PIOSTART					=	0x2,
	DIODM_STATUS_PIOSTOP					=	0x4,
	DIODM_STATUS_TRGIN						=	0x8,
	DIODM_STATUS_OVERRUN					=	0x10,

//	Error
	DIODM_STATUS_FIFOEMPTY					=	0x1,
	DIODM_STATUS_FIFOFULL					=	0x2,
	DIODM_STATUS_SGOVERIN					=	0x4,
	DIODM_STATUS_TRGERR						=	0x8,
	DIODM_STATUS_CLKERR						=	0x10,
	DIODM_STATUS_SLAVEHALT					=	0x20,
	DIODM_STATUS_MASTERHALT					=	0x40,

//	Reset
	DIODM_RESET_FIFO_IN						=	0x02,
	DIODM_RESET_FIFO_OUT					=	0x04,

//	Buffer Ring
	DIODM_WRITE_ONCE						=	0,
	DIODM_WRITE_RING						=	1,

//	NET
	DIONET_MODE_DIRECT						=	0,
	DIONET_MODE_AP							=	1,

//	Counter
	DIO_COUNT_EDGE_UP						=	1,
	DIO_COUNT_EDGE_DOWN						=	2,

//	PWM Pulse Unit
	DIO_PWM_PULSE_UNIT_S					=	1,
	DIO_PWM_PULSE_UNIT_MS					=	2,
	DIO_PWM_PULSE_UNIT_US					=	3,
	DIO_PWM_PULSE_UNIT_NS					=	4,

//	PWM Pulse Phase Shift
	DIO_PWM_PULSE_PHASE_0_DEGREES			=	0,
	DIO_PWM_PULSE_PHASE_90_DEGREES			=	90,
	DIO_PWM_PULSE_PHASE_180_DEGREES			=	180,

//	Pulse Measurement Logic
	DIO_PULSE_MEASUREMENT_TARGET_OFF_TIME	=	0,
	DIO_PULSE_MEASUREMENT_TARGET_ON_TIME	=	1,

//	Pulse Measurement Time Unit
	DIO_PULSE_MEASUREMENT_TIME_UNIT_S		=	1,
	DIO_PULSE_MEASUREMENT_TIME_UNIT_MS		=	2,
	DIO_PULSE_MEASUREMENT_TIME_UNIT_US		=	3,
	DIO_PULSE_MEASUREMENT_TIME_UNIT_NS		=	4,

//-------------------------------------------------
//	Error	codes
//-------------------------------------------------
//	Initialize	Error
//	Common
	DIO_ERR_SUCCESS							=	0,		//	normal completed
	DIO_ERR_INI_RESOURCE					=	1,		//	invalid resource reference specified
	DIO_ERR_INI_INTERRUPT					=	2,		//	invalid interrupt routine registered
	DIO_ERR_INI_MEMORY						=	3,		//	invalid memory allocationed
	DIO_ERR_INI_REGISTRY					=	4,		//	invalid registry accesse

	DIO_ERR_SYS_RECOVERED_FROM_STANDBY		=	7,		//	Execute DioResetDevice function because the device has recovered from standby mode.
	DIO_ERR_INI_NOT_FOUND_SYS_FILE			=	8,		//	Because the Cdio.sys file is not found, it is not possible to initialize it.
	DIO_ERR_INI_DLL_FILE_VERSION			=	9,		//	Because version information on the Cdio.dll file cannot be acquired, it is not possible to initialize it.
	DIO_ERR_INI_SYS_FILE_VERSION			=	10,		//	Because version information on the Cdio.sys file cannot be acquired, it is not possible to initialize it.
	DIO_ERR_INI_NO_MATCH_DRV_VERSION		=	11,		//	Because version information on Cdio.dll and Cdio.sys is different, it is not possible to initialize it.

//	DLL	Error
//	Common
	DIO_ERR_DLL_DEVICE_NAME					=	10000,	//	invalid device name specified.
	DIO_ERR_DLL_INVALID_ID					=	10001,	//	invalid ID specified.
	DIO_ERR_DLL_CALL_DRIVER					=	10002,	//	not call the driver.(Invalid device I/O controller)
	DIO_ERR_DLL_CREATE_FILE					=	10003,	//	not create the file.(Invalid CreateFile)
	DIO_ERR_DLL_CLOSE_FILE					=	10004,	//	not close the file.(Invalid CloseFile)
	DIO_ERR_DLL_CREATE_THREAD				=	10005,	//	not create the thread.(Invalid CreateThread)
	DIO_ERR_INFO_INVALID_DEVICE				=	10050,	//	invalid device infomation specified .Please check the spell.
	DIO_ERR_INFO_NOT_FIND_DEVICE			=	10051,	//	not find the available device
	DIO_ERR_INFO_INVALID_INFOTYPE			=	10052,	//	specified device infomation type beyond the limit

//	DIO
	DIO_ERR_DLL_BUFF_ADDRESS				=	10100,	//	invalid data buffer address
	DIO_ERR_DLL_HWND						=	10200,	//	window handle beyond the limit
	DIO_ERR_DLL_TRG_KIND					=	10300,	//	trigger kind beyond the limit

//	SYS	Error
//	Common
	DIO_ERR_SYS_MEMORY						=	20000,	//	not secure memory
	DIO_ERR_SYS_NOT_SUPPORTED				=	20001,	//	this board couldn't use this function
	DIO_ERR_SYS_BOARD_EXECUTING				=	20002,	//	board is behaving, not execute
	DIO_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,	//	Access denied
	STATUS_SYS_USB_INVALID_HANDLE			=	20034,	//	Invalid handle

//	DIO
	DIO_ERR_SYS_PORT_NO						=	20100,	//	board No. beyond the limit
	DIO_ERR_SYS_PORT_NUM					=	20101,	//	board number beyond the limit
	DIO_ERR_SYS_BIT_NO						=	20102,	//	bit No. beyond the limit
	DIO_ERR_SYS_BIT_NUM						=	20103,	//	bit number beyond the limit
	DIO_ERR_SYS_BIT_DATA					=	20104,	//	bit data beyond the limit of 0 to 1
	DIO_ERR_SYS_CH_NO						=	20105,	//	Channel No. is outside the setting range.
	DIO_ERR_SYS_CH_NUM						=	20106,	//	Channel number is outside the setting range.
	DIO_ERR_SYS_INT_BIT						=	20200,	//	interrupt bit beyond the limit
	DIO_ERR_SYS_INT_LOGIC					=	20201,	//	interrupt logic beyond the limit
	DIO_ERR_SYS_TIM							=	20300,	//	timer value beyond the limit
	DIO_ERR_SYS_FILTER						=	20400,	//	filter number beyond the limit
	DIO_ERR_SYS_IODIRECTION					=	20500,	//	Direction value is out of range
	DIO_ERR_SYS_8255						=	20600,	//	8255 chip number is outside of the range.
	DIO_ERR_SYS_CNT_EDGE					=	20700,	//	Count edge is outside the setting range.
	DIO_ERR_SYS_CMP_REG_NO					=	20800,	//	Comparision register No. is outside the setting range.
	DIO_ERR_SYS_CMP_REG_VAL					=	20801,	//	Comparision register value is outside the setting range.
	DIO_ERR_SYS_COUNT_VAL					=	20810,	//	Count value is outside the setting range.
	DIO_ERR_SYS_PWM_PULSE_PERIOD			=	20900,	//	The PWM pulse period is out of range.
	DIO_ERR_SYS_PWM_PERIOD_UNIT				=	20901,	//	The PWM pulse period unit is out of range.
	DIO_ERR_SYS_PWM_PULSE_DUTY				=	20902,	//	The PWM pulse duty ratio is out of range.
	DIO_ERR_SYS_PWM_PULSE_PHASE				=	20903,	//	The PWM pulse phase setting is out of range.
	DIO_ERR_SYS_PWM_RUNNING_PWM				=	20904,	//	Function is not allowed to be called during the PWM operation.
	DIO_ERR_SYS_PULSE_MEASUREMENT_TARGET	=	20950,	//	The pulse measurement time is out of range.
	DIO_ERR_SYS_PULSE_MEASUREMENT_PERIOD	=	20951,	//	The pulse measurement period is out of range.
	DIO_ERR_SYS_PULSE_MEASUREMENT_RUNNING	=	20952,	//	Function is not allowed to be called during the pulse measurement operation.
	DIO_ERR_SYS_PULSE_MEASUREMENT_TIME_UNIT	=	20953,	//	The pulse measurement time unit is out of range.

//  DM
	DIO_ERR_SYS_SIGNAL						=	21000,	//	Usable signal is outside the setting range.
	DIO_ERR_SYS_START						=	21001,	//	Usable start conditions are outside the setting range.
	DIO_ERR_SYS_CLOCK						=	21002,	//	Clock conditions are outside the setting range.
	DIO_ERR_SYS_CLOCK_VAL					=	21003,	//	Clock value is outside the setting range.
	DIO_ERR_SYS_CLOCK_UNIT					=	21004,	//	Clock value unit is outside the setting range.
	DIO_ERR_SYS_STOP						=	21005,	//	Stop conditions are outside the setting range.
	DIO_ERR_SYS_STOP_NUM					=	21006,	//	Stop number is outside the setting range.
	DIO_ERR_SYS_RESET						=	21007,	//	Contents of reset are outside the setting range.
	DIO_ERR_SYS_LEN							=	21008,	//	Data number is outside the setting range.
	DIO_ERR_SYS_RING						=	21009,	//	Buffer repetition use setup is outside the setting range.
	DIO_ERR_SYS_COUNT						=	21010,	//	Data transmission number is outside the setting range.
	DIO_ERR_DM_BUFFER						=	21100,	//	Buffer was too large and has not secured.
	DIO_ERR_DM_LOCK_MEMORY					=	21101,	//	Memory has not been locked.
	DIO_ERR_DM_PARAM						=	21102,	//	Parameter error
	DIO_ERR_DM_SEQUENCE						=	21103,	//	Procedure error of execution

//	Module Information
	DIO_ERR_SYS_DEVICE_ERR_DRIVER_IC_ERR	=	21200,	//	Driver IC error occurred.
	DIO_ERR_SYS_DEVICE_ERR_PCOM_FAILURE		=	21201,	//	PCOM power failure occurred.

// NET
	DIO_ERR_NET_BASE						=	22000,	//	Access error
	DIO_ERR_NET_ACCESS						=	22001,	//	Access violation
	DIO_ERR_NET_AREA						=	22002,	//	Area error
	DIO_ERR_NET_SIZE						=	22003,	//	Access size error
	DIO_ERR_NET_PARAMETER					=	22004,	//	Parameter error
	DIO_ERR_NET_LENGTH						=	22005,	//	Length error
	DIO_ERR_NET_RESOURCE					=	22006,	//	Insufficient resources
	DIO_ERR_NET_TIMEOUT						=	22016,	//	Communications timeout
	DIO_ERR_NET_HANDLE						=	22017,	//	Handle error
	DIO_ERR_NET_CLOSE						=	22018,	//	Close error
	DIO_ERR_NET_TIMEOUT_WIO					=	22064	//	Wireless communications timeout
}

namespace CdioCs
{
	unsafe public delegate void PINTCALLBACK(short Id, int wParam, int lParam, void *Param);	
	unsafe public delegate void PTRGCALLBACK(short Id, int wParam, int lParam, void *Param);	
	unsafe public delegate void PDMCOUNTCALLBACK(short Id, int wParam, int lParam, void *Param);	
	unsafe public delegate void PDMSTOPCALLBACK(short Id, int wParam, int lParam, void *Param);

	public class Cdio
	{
		// Definition of common functions
		[DllImport("cdio.dll")] static extern int DioInit(string DeviceName, ref short Id);
		[DllImport("cdio.dll")] static extern int DioExit(short Id);
		[DllImport("cdio.dll")] static extern int DioResetDevice(short Id);
		[DllImport("cdio.dll")] static extern int DioGetErrorString(int ErrorCode, System.Text.StringBuilder ErrorString);

		// Digital filter functions
		[DllImport("cdio.dll")] static extern int DioSetDigitalFilter(short Id, short FilterValue);
		[DllImport("cdio.dll")] static extern int DioGetDigitalFilter(short Id, ref short FilterValue);
		
		// I/O Direction functions
		[DllImport("cdio.dll")] static extern int DioSetIoDirection(short Id, uint dwDir);
		[DllImport("cdio.dll")] static extern int DioGetIoDirection(short Id, ref uint dwDir);
		[DllImport("cdio.dll")] static extern int DioSetIoDirectionEx(short Id, uint dwDir);
		[DllImport("cdio.dll")] static extern int DioGetIoDirectionEx(short Id, ref uint dwDir);
		[DllImport("cdio.dll")] static extern int DioSet8255Mode(short Id, short ChipNo, short CtrlWord);
		[DllImport("cdio.dll")] static extern int DioGet8255Mode(short Id, short ChipNo, ref short CtrlWord);
		
		// Simple I/O functions
		[DllImport("cdio.dll")] static extern int DioInpByte(short Id, short PortNo, ref byte Data);
		[DllImport("cdio.dll")] static extern int DioInpBit(short Id, short BitNo, ref byte Data);
		[DllImport("cdio.dll")] static extern int DioInpByteSR(short Id, short PortNo, ref byte Data, ref ushort Timestamp, byte Mode);
        [DllImport("cdio.dll")] static extern int DioInpBitSR(short Id, short BitNo, ref byte Data, ref ushort Timestamp, byte Mode);
		[DllImport("cdio.dll")] static extern int DioOutByte(short Id, short PortNo, byte Data);
		[DllImport("cdio.dll")] static extern int DioOutBit(short Id, short BitNo, byte Data);
		[DllImport("cdio.dll")] static extern int DioEchoBackByte(short Id, short PortNo, ref byte Data);
		[DllImport("cdio.dll")] static extern int DioEchoBackBit(short Id, short BitNo, ref byte Data);
		
		// Multiple I/O functions
		[DllImport("cdio.dll")] static extern int DioInpMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioInpMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioInpMultiByteSR(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data, ref ushort Timestamp, byte Mode);
		[DllImport("cdio.dll")] static extern int DioInpMultiBitSR(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data, ref ushort Timestamp, byte Mode);
		[DllImport("cdio.dll")] static extern int DioOutMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioOutMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioEchoBackMultiByte(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] PortNo, short PortNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioEchoBackMultiBit(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] BitNo, short BitNum, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		
		// Interrupt functions
		[DllImport("cdio.dll")] static extern int DioNotifyInterrupt(short Id, short IntBit, short Logic, IntPtr hWnd);
		[DllImport("cdio.dll")] unsafe static extern int DioSetInterruptCallBackProc(short Id, IntPtr pIntCallBack, void *Param);
		
		// Trigger functions
		[DllImport("cdio.dll")] static extern int DioNotifyTrg(short Id, short TrgBit, short TrgKind, int Tim, IntPtr hWnd);
		[DllImport("cdio.dll")] static extern int DioStopNotifyTrg(short Id, short TrgBit);
		[DllImport("cdio.dll")] static extern int DioSetTrgCallBackProc(short Id, IntPtr CallBackProc, ref int Param);
		
		// Information functions
		[DllImport("cdio.dll")] static extern int DioGetDeviceInfo(string Device, short InfoType, ref int Param1, ref int Param2, ref int Param3);
		[DllImport("cdio.dll")] static extern int DioQueryDeviceName(short Index, System.Text.StringBuilder DeviceName, System.Text.StringBuilder Device);
		[DllImport("cdio.dll")] static extern int DioGetDeviceType(string Device, ref short DeviceType);
		[DllImport("cdio.dll")] static extern int DioGetMaxPorts(short Id, ref short InPortNum, ref short OutPortNum);
        [DllImport("cdio.dll")] static extern int DioGetMaxCountChannels(short Id, ref short ChannelNum);
        [DllImport("cdio.dll")] static extern int DioGetNetCommunicationInfo(short Id, short InfoType, ref int InfoData);

        // Counter function        
        [DllImport("cdio.dll")] static extern int DioSetCountEdge(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] short[] CountEdge);
        [DllImport("cdio.dll")] static extern int DioGetCountEdge(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] short[] CountEdge);
        [DllImport("cdio.dll")] static extern int DioSetCountMatchValue(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] short[] CompareRegNo, [MarshalAs(UnmanagedType.LPArray)] uint[] CompareCount);        
        [DllImport("cdio.dll")] static extern int DioStartCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum);
        [DllImport("cdio.dll")] static extern int DioStopCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum);
        [DllImport("cdio.dll")] static extern int DioGetCountStatus(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] CountStatus);
        [DllImport("cdio.dll")] static extern int DioCountPreset(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] PresetCount);
        [DllImport("cdio.dll")] static extern int DioReadCount(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] Count);
        [DllImport("cdio.dll")] static extern int DioReadCountSR(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChNo, short ChNum, [MarshalAs(UnmanagedType.LPArray)] uint[] Count, ref ushort Timestamp, byte Mode);

		// PWM function
		[DllImport("cdio.DLL")] static extern int DioGetMaxPwmChannels(short Id, ref ushort ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioSetPwmPulsePeriod(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ulong[] PulsePeriodValue, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulsePeriodUnit);
		[DllImport("cdio.DLL")] static extern int DioGetPwmPulsePeriod(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ulong[] PulsePeriodValue, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulsePeriodUnit);
		[DllImport("cdio.DLL")] static extern int DioSetPwmPulseDuty(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulseDuty);
		[DllImport("cdio.DLL")] static extern int DioGetPwmPulseDuty(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulseDuty);
		[DllImport("cdio.DLL")] static extern int DioSetPwmPulsePhase(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] short[] PulsePhase);
		[DllImport("cdio.DLL")] static extern int DioGetPwmPulsePhase(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] short[] PulsePhase);
		[DllImport("cdio.DLL")] static extern int DioStartPwm(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioStopPwm(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioGetPwmStatus(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] Status, [MarshalAs(UnmanagedType.LPArray)] ushort[] Error);

		// Pulse Monitor function
		[DllImport("cdio.DLL")] static extern int DioGetMaxPulseMeasurementChannels(short Id, ref ushort ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioSetPulseMeasurementSetting(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] MeasurementTarget, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulsePeriod);
		[DllImport("cdio.DLL")] static extern int DioGetPulseMeasurementSetting(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] MeasurementTarget, [MarshalAs(UnmanagedType.LPArray)] ushort[] PulsePeriod);
		[DllImport("cdio.DLL")] static extern int DioStartPulseMeasurement(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioStopPulseMeasurement(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum);
		[DllImport("cdio.DLL")] static extern int DioReadPulseMeasurementData(short Id, [MarshalAs(UnmanagedType.LPArray)] short[] ChannelNo, short ChannelNum, [MarshalAs(UnmanagedType.LPArray)] ushort[] Status, [MarshalAs(UnmanagedType.LPArray)] ushort[] Error, [MarshalAs(UnmanagedType.LPArray)] ulong[] TimeValue, [MarshalAs(UnmanagedType.LPArray)] ushort[] TimeUnit);

		//dm functions
		[DllImport("cdio.DLL")] static extern int DioDmSetDirection(short Id, short Direction);
		[DllImport("cdio.DLL")] static extern int DioDmGetDirection(short Id, ref short Direction);
		[DllImport("cdio.DLL")] static extern int DioDmSetStandAlone(short Id);
		[DllImport("cdio.DLL")] static extern int DioDmSetMaster(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt);
		[DllImport("cdio.DLL")] static extern int DioDmSetSlave(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt);
		[DllImport("cdio.DLL")] static extern int DioDmSetStartTrigger(short Id, short Direction, short Start);
		[DllImport("cdio.DLL")] static extern int DioDmSetStartPattern(short Id, uint Pattern, uint Mask);
		[DllImport("cdio.DLL")] static extern int DioDmSetClockTrigger(short Id, short Direction, short Clock);
		[DllImport("cdio.DLL")] static extern int DioDmSetInternalClock(short Id, short Direction, uint Clock, short Unit);
		[DllImport("cdio.DLL")] static extern int DioDmSetStopTrigger(short Id, short Direction, short Stop);
		[DllImport("cdio.DLL")] static extern int DioDmSetStopNumber(short Id, short Direction, uint StopNumber);
		[DllImport("cdio.DLL")] static extern int DioDmFifoReset(short Id, short Reset);
		[DllImport("cdio.DLL")] static extern int DioDmSetBuffer(short Id, short Direction, IntPtr Buffer, uint Length, short IsRing);
		[DllImport("cdio.DLL")] static extern int DioDmSetTransferStartWait(short Id, short Time);
		[DllImport("cdio.DLL")] static extern int DioDmTransferStart(short Id, short Direction);
		[DllImport("cdio.DLL")] static extern int DioDmTransferStop(short Id, short Direction);
		[DllImport("cdio.DLL")] static extern int DioDmGetStatus(short Id, short Direction, ref uint Status, ref uint Err);
		[DllImport("cdio.DLL")] static extern int DioDmGetCount(short Id, short Direction, ref uint Count, ref uint Carry);
		[DllImport("cdio.DLL")] static extern int DioDmGetWritePointer(short Id, short Direction, ref uint WritePointer, ref uint Count, ref uint Carry);
		[DllImport("cdio.DLL")] static extern int DioDmSetStopEvent(short Id, short Direction, IntPtr hWnd);
		[DllImport("cdio.DLL")] unsafe static extern int DioDmSetStopCallBackProc(short Id, IntPtr CallBackProc, void *Param);
		[DllImport("cdio.DLL")] static extern int DioDmSetCountEvent(short Id, short Direction, uint Count, IntPtr hWnd);
		[DllImport("cdio.DLL")] unsafe static extern int DioDmSetCountCallBackProc(short Id, IntPtr CallBackProc, void *Param);

		// Demo Device I/O functions
		[DllImport("cdio.dll")] static extern int DioSetDemoByte(short Id, short PortNo, byte Data);
		[DllImport("cdio.dll")] static extern int DioSetDemoBit(short Id, short BitNo, byte Data);
		
		[DllImport("cdio.dll")] static extern int DioResetPatternEvent(short Id, [MarshalAs(UnmanagedType.LPArray)] byte[] Data);
		[DllImport("cdio.dll")] static extern int DioGetPatternEventStatus(short Id, ref short Status);

		// Constructor
		public Cdio()
		{
		}

		// Description of common functions
		public int Init(string DeviceName, out short Id)
		{
			Id = 0;
			int ret	= DioInit(DeviceName, ref Id);
			return	ret;
		}

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

		public int ResetDevice(short Id)
		{
			int ret = DioResetDevice(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 = DioGetErrorString(ErrorCode, errorstring);
			if(ret == 0)
			{
				ErrorString = errorstring.ToString();
			}
			return ret;
		}

		// Digital filter functions
		public int SetDigitalFilter(short Id, short FilterValue)
		{
			int ret = DioSetDigitalFilter(Id, FilterValue);
			return ret;
		}

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

		// I/O Direction functions
		public int SetIoDirection(short Id, uint dwDir)
		{
			int ret = DioSetIoDirection(Id, dwDir);
			return ret;
		}

		public int GetIoDirection(short Id, out uint dwDir)
		{
			dwDir = 0;
			int ret = DioGetIoDirection(Id, ref dwDir);
			return ret;
		}

		public int Set8255Mode(short Id, short ChipNo, short CtrlWord)
		{
			int ret = DioSet8255Mode(Id, ChipNo, CtrlWord);
			return ret;
		}

		public int Get8255Mode(short Id, short ChipNo, out short CtrlWord)
		{
			CtrlWord = 0;
			int ret = DioGet8255Mode(Id, ChipNo, ref CtrlWord);
			return ret;
		}

		public int SetIoDirectionEx(short Id, uint dwDir)
		{
			int ret = DioSetIoDirectionEx(Id, dwDir);
			return ret;
		}

		public int GetIoDirectionEx(short Id, out uint dwDir)
		{
			dwDir = 0;
			int ret = DioGetIoDirectionEx(Id, ref dwDir);
			return ret;
		}

		// Simple I/O functions
		public int InpByte(short Id, short PortNo, out byte Data)
		{
			Data = 0;
			int ret = DioInpByte(Id, PortNo, ref Data);
			return ret;
		}

		public int InpBit(short Id, short BitNo, out byte Data)
		{
			Data = 0;
			int ret = DioInpBit(Id, BitNo, ref Data);
			return ret;
		}

        public int InpByteSR(short Id, short PortNo, out byte Data, out ushort Timestamp, byte Mode)
        {
            Data = 0;
            Timestamp = 0;
            int ret = DioInpByteSR(Id, PortNo, ref Data, ref Timestamp, Mode);
            return ret;
        }

        public int InpBitSR(short Id, short BitNo, out byte Data, out ushort Timestamp, byte Mode)
        {
            Data = 0;
            Timestamp = 0;
            int ret = DioInpBitSR(Id, BitNo, ref Data, ref Timestamp, Mode);
            return ret;
        }

        public int OutByte(short Id, short PortNo, byte Data)
		{
			int ret = DioOutByte(Id, PortNo, Data);
			return ret;
		}

		public int OutBit(short Id, short BitNo, byte Data)
		{
			int ret = DioOutBit(Id, BitNo, Data);
			return ret;
		}

		public int EchoBackByte(short Id, short PortNo, out byte Data)
		{
			Data = 0;
			int ret = DioEchoBackByte(Id, PortNo, ref Data);
			return ret;
		}

		public int EchoBackBit(short Id, short BitNo, out byte Data)
		{
			Data = 0;
			int ret = DioEchoBackBit(Id, BitNo, ref Data);
			return ret;
		}

		// Multiple I/O functions
		public int InpMultiByte(short Id, short[] PortNo, short PortNum, byte[] Data)
		{
			int ret = DioInpMultiByte(Id, PortNo, PortNum, Data);
			return ret;
		}

		public int InpMultiBit(short Id, short[] BitNo, short BitNum, byte[] Data)
		{
			int ret = DioInpMultiBit(Id, BitNo, BitNum, Data);
			return ret;
		}

        public int InpMultiByteSR(short Id, short[] PortNo, short PortNum, byte[] Data, out ushort Timestamp, byte Mode)
        {
            Timestamp = 0;
            int ret = DioInpMultiByteSR(Id, PortNo, PortNum, Data, ref Timestamp, Mode);
            return ret;
        }

        public int InpMultiBitSR(short Id, short[] BitNo, short BitNum, byte[] Data, out ushort Timestamp, byte Mode)
        {
            Timestamp = 0;
            int ret = DioInpMultiBitSR(Id, BitNo, BitNum, Data, ref Timestamp, Mode);
            return ret;
        }

        public int OutMultiByte(short Id, short[] PortNo, short PortNum, byte[] Data)
		{
			int ret = DioOutMultiByte(Id, PortNo, PortNum, Data);
			return ret;
		}

		public int OutMultiBit(short Id, short[] BitNo, short BitNum, byte[] Data)
		{
			int ret = DioOutMultiBit(Id, BitNo, BitNum, Data);
			return ret;
		}

		public int EchoBackMultiByte(short Id, short[] PortNo, short PortNum, byte[] Data)
		{
			int ret = DioEchoBackMultiByte(Id, PortNo, PortNum, Data);
			return ret;
		}

		public int EchoBackMultiBit(short Id, short[] BitNo, short BitNum, byte[] Data)
		{
			int ret = DioEchoBackMultiBit(Id, BitNo, BitNum, Data);
			return ret;
		}

		// Interrupt functions
		public int NotifyInterrupt(short Id, short IntBit, short Logic, IntPtr hWnd)
		{
			int ret = DioNotifyInterrupt(Id, IntBit, Logic, hWnd);
			return ret;
		}

		unsafe public int SetInterruptCallBackProc(short Id, IntPtr pIntCallBack, void *Param)
		{
			int ret = DioSetInterruptCallBackProc(Id, pIntCallBack, Param);
			return ret;
		}

		// Trigger functions
		public int NotifyTrg(short Id, short TrgBit, short TrgKind, int Tim, IntPtr hWnd)
		{
			int ret = DioNotifyTrg(Id, TrgBit, TrgKind, Tim, hWnd);
			return ret;
		}

		public int StopNotifyTrg(short Id, short TrgBit)
		{
			int ret = DioStopNotifyTrg(Id, TrgBit);
			return ret;
		}

		public int SetTrgCallBackProc(short Id, IntPtr CallBackProc, out int Param)
		{
			Param = 0;
			int ret = DioSetTrgCallBackProc(Id, CallBackProc, ref Param);
			return ret;
		}

		// Information functions
		public int GetDeviceInfo(string Device, short InfoType, out int Param1, out int Param2, out int Param3)
		{
			Param1 = 0;
			Param2 = 0;
			Param3 = 0;
			int ret = DioGetDeviceInfo(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 = DioQueryDeviceName(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 = DioGetDeviceType(Device, ref DeviceType);
			return ret;
		}

		public int GetMaxPorts(short Id, out short InPortNum, out short OutPortNum)
		{
			InPortNum = 0;
			OutPortNum = 0;
			int ret = DioGetMaxPorts(Id, ref InPortNum, ref OutPortNum);
			return ret;
		}

        public int GetMaxCountChannels(short Id, out short ChannelNum)
        {
            ChannelNum = 0;
            int ret = DioGetMaxCountChannels(Id, ref ChannelNum);
            return ret;
        }
        
        public int GetNetCommunicationInfo(short Id, short InfoType, out int InfoData)
        {
            InfoData = 0;
            int ret = DioGetNetCommunicationInfo(Id, InfoType, ref InfoData);
            return ret;
        }
         
        public int SetCountEdge(short Id, short[] ChNo, short ChNum, short[] CountEdge)
        {
            int ret = DioSetCountEdge(Id, ChNo, ChNum, CountEdge);
            return ret;
        }

        public int GetCountEdge(short Id, short[] ChNo, short ChNum, short[] CountEdge)
        {
            int ret = DioGetCountEdge(Id, ChNo, ChNum, CountEdge);
            return ret;
        }

        public int SetCountMatchValue(short Id, short[] ChNo, short ChNum, short[] CompareRegNo, uint[] CompareCount)
        {
            int ret = DioSetCountMatchValue(Id, ChNo, ChNum, CompareRegNo, CompareCount);
            return ret;
        }

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

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

        public int GetCountStatus(short Id, short[] ChNo, short ChNum, uint[] CountStatus)
        {
            int ret = DioGetCountStatus(Id, ChNo, ChNum, CountStatus);
            return ret;
        }

        public int CountPreset(short Id, short[] ChNo, short ChNum, uint[] PresetCount)
        {
            int ret = DioCountPreset(Id, ChNo, ChNum, PresetCount);
            return ret;
        }

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

		public int GetMaxPwmChannels(short Id, out ushort ChannelNum)
		{
			ChannelNum = 0;
			int ret = DioGetMaxPwmChannels(Id, ref ChannelNum);
			return ret;
		}

		public int SetPwmPulsePeriod(short Id, short[] ChannelNo, short ChannelNum, ulong[] PulsePeriodValue, ushort[] PulsePeriodUnit)
		{
			int ret = DioSetPwmPulsePeriod(Id, ChannelNo, ChannelNum, PulsePeriodValue, PulsePeriodUnit);
			return ret;
		}

		public int GetPwmPulsePeriod(short Id, short[] ChannelNo, short ChannelNum, ulong[] PulsePeriodValue, ushort[] PulsePeriodUnit)
		{
			int ret = DioGetPwmPulsePeriod(Id, ChannelNo, ChannelNum, PulsePeriodValue, PulsePeriodUnit);
			return ret;
		}

		public int SetPwmPulseDuty(short Id, short[] ChannelNo, short ChannelNum, ushort[] PulseDuty)
		{
			int ret = DioSetPwmPulseDuty(Id, ChannelNo, ChannelNum, PulseDuty);
			return ret;
		}

		public int GetPwmPulseDuty(short Id, short[] ChannelNo, short ChannelNum, ushort[] PulseDuty)
		{
			int ret = DioGetPwmPulseDuty(Id, ChannelNo, ChannelNum, PulseDuty);
			return ret;
		}

		public int SetPwmPulsePhase(short Id, short[] ChannelNo, short ChannelNum, short[] PulsePhase)
		{
			int ret = DioSetPwmPulsePhase(Id, ChannelNo, ChannelNum, PulsePhase);
			return ret;
		}

		public int GetPwmPulsePhase(short Id, short[] ChannelNo, short ChannelNum, short[] PulsePhase)
		{
			int ret = DioGetPwmPulsePhase(Id, ChannelNo, ChannelNum, PulsePhase);
			return ret;
		}

		public int StartPwm(short Id, short[] ChannelNo, short ChannelNum)
		{
			int ret = DioStartPwm(Id, ChannelNo, ChannelNum);
			return ret;
		}

		public int StopPwm(short Id, short[] ChannelNo, short ChannelNum)
		{
			int ret = DioStopPwm(Id, ChannelNo, ChannelNum);
			return ret;
		}

		public int GetPwmStatus(short Id, short[] ChannelNo, short ChannelNum, ushort[] Status, ushort[] Error)
		{
			int ret = DioGetPwmStatus(Id, ChannelNo, ChannelNum, Status, Error);
			return ret;
		}

		public int GetMaxPulseMeasurementChannels(short Id, out ushort ChannelNum)
		{
			ChannelNum = 0;
			int ret = DioGetMaxPulseMeasurementChannels(Id, ref ChannelNum);
			return ret;
		}

		public int SetPulseMeasurementSetting(short Id, short[] ChannelNo, short ChannelNum, ushort[] MeasurementTarget, ushort[] PulsePeriod)
		{
			int ret = DioSetPulseMeasurementSetting(Id, ChannelNo, ChannelNum, MeasurementTarget, PulsePeriod);
			return ret;
		}

		public int GetPulseMeasurementSetting(short Id, short[] ChannelNo, short ChannelNum, ushort[] MeasurementTarget, ushort[] PulsePeriod)
		{
			int ret = DioGetPulseMeasurementSetting(Id, ChannelNo, ChannelNum, MeasurementTarget, PulsePeriod);
			return ret;
		}

		public int StartPulseMeasurement(short Id, short[] ChannelNo, short ChannelNum)
		{
			int ret = DioStartPulseMeasurement(Id, ChannelNo, ChannelNum);
			return ret;
		}

		public int StopPulseMeasurement(short Id, short[] ChannelNo, short ChannelNum)
		{
			int ret = DioStopPulseMeasurement(Id, ChannelNo, ChannelNum);
			return ret;
		}

		public int ReadPulseMeasurementData(short Id, short[] ChannelNo, short ChannelNum, ushort[] Status, ushort[] Error, ulong[] TimeValue, ushort[] TimeUnit)
		{
			int ret = DioReadPulseMeasurementData(Id, ChannelNo, ChannelNum, Status, Error, TimeValue, TimeUnit);
			return ret;
		}

		public int DmSetDirection(short Id, short Direction)
		{
			int ret = DioDmSetDirection(Id, Direction);
			return ret;
		}

		public int DmGetDirection(short Id, out short Direction)
		{
			Direction = 0;
			int ret = DioDmGetDirection(Id, ref Direction);
			return ret;
		}

		public int DmSetStandAlone(short Id)
		{
			int ret = DioDmSetStandAlone(Id);
			return ret;
		}

		public int DmSetMaster(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt)
		{
			int ret = DioDmSetMaster(Id, ExtSig1, ExtSig2, ExtSig3, MasterHalt, SlaveHalt);
			return ret;
		}

		public int DmSetSlave(short Id, short ExtSig1, short ExtSig2, short ExtSig3, short MasterHalt, short SlaveHalt)
		{
			int ret = DioDmSetSlave(Id, ExtSig1, ExtSig2, ExtSig3, MasterHalt, SlaveHalt);
			return ret;
		}

		public int DmSetStartTrigger(short Id, short Direction, short Start)
		{
			int ret = DioDmSetStartTrigger(Id, Direction, Start);
			return ret;
		}

		public int DmSetStartPattern(short Id, uint Pattern, uint Mask)
		{
			int ret = DioDmSetStartPattern(Id, Pattern, Mask);
			return ret;
		}

		public int DmSetClockTrigger(short Id, short Direction, short Clock)
		{
			int ret = DioDmSetClockTrigger(Id, Direction, Clock);
			return ret;
		}

		public int DmSetInternalClock(short Id, short Direction, uint Clock, short Unit)
		{
			int ret = DioDmSetInternalClock(Id, Direction, Clock, Unit);
			return ret;
		}

		public int DmSetStopTrigger(short Id, short Direction, short Stop)
		{
			int ret = DioDmSetStopTrigger(Id, Direction, Stop);
			return ret;
		}

		public int DmSetStopNumber(short Id, short Direction, uint StopNumber)
		{
			int ret = DioDmSetStopNumber(Id, Direction, StopNumber);
			return ret;
		}

		public int DmFifoReset(short Id, short Reset)
		{
			int ret = DioDmFifoReset(Id, Reset);
			return ret;
		}

		public int DmSetBuffer(short Id, short Direction, IntPtr Buffer, uint Length, short IsRing)
		{
			int ret = DioDmSetBuffer(Id, Direction, Buffer, Length, IsRing);
			return ret;
		}

		public int DmSetTransferStartWait(short Id, short Time)
		{
			int ret = DioDmSetTransferStartWait(Id, Time);
			return ret;
		}

		public int DmTransferStart(short Id, short Direction)
		{
			int ret = DioDmTransferStart(Id, Direction);
			return ret;
		}

		public int DmTransferStop(short Id, short Direction)
		{
			int ret = DioDmTransferStop(Id, Direction);
			return ret;
		}

		public int DmGetStatus(short Id, short Direction, out uint Status, out uint Err)
		{
			Status = 0;
			Err = 0;
			int ret = DioDmGetStatus(Id, Direction, ref Status, ref Err);
			return ret;
		}

		public int DmGetCount(short Id, short Direction, out uint Count, out uint Carry)
		{
			Count = 0;
			Carry = 0;
			int ret = DioDmGetCount(Id, Direction, ref Count, ref Carry);
			return ret;
		}

		public int DmGetWritePointer(short Id, short Direction, out uint WritePointer, out uint Count, out uint Carry)
		{
			WritePointer = 0;
			Count = 0;
			Carry = 0;
			int ret = DioDmGetWritePointer(Id, Direction, ref WritePointer, ref Count, ref Carry);
			return ret;
		}

		public int DmSetStopEvent(short Id, short Direction, IntPtr hWnd)
		{
			int ret = DioDmSetStopEvent(Id, Direction, hWnd);
			return ret;
		}

		unsafe public int DmSetStopCallBackProc(short Id, IntPtr CallBackProc,  void *Param)
		{
			int ret = DioDmSetStopCallBackProc(Id, CallBackProc, Param);
			return ret;
		}

		public int DmSetCountEvent(short Id, short Direction, uint Count, IntPtr hWnd)
		{
			int ret = DioDmSetCountEvent(Id, Direction, Count, hWnd);
			return ret;
		}

		unsafe public int DmSetCountCallBackProc(short Id, IntPtr CallBackProc,  void *Param)
		{
			int ret = DioDmSetCountCallBackProc(Id, CallBackProc, Param);
			return ret;
		}

		public int SetDemoByte(short Id, short PortNo, byte Data)
		{
			int ret = DioSetDemoByte(Id, PortNo, Data);
			return ret;
		}

		public int SetDemoBit(short Id, short BitNo, byte Data)
		{
			int ret = DioSetDemoBit(Id, BitNo, Data);
			return ret;
		}
		
		public int ResetPatternEvent(short Id, byte[] Data)
		{
			int ret = DioResetPatternEvent(Id, Data);
			return ret;
		}

		public int GetPatternEventStatus(short Id, out short Status)
		{
			Status = 0;
			int ret = DioGetPatternEventStatus(Id, ref Status);
			return ret;
		}

	}
}
