CdioCs.cs 26.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
//============================================================-
//	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

//-------------------------------------------------
//	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

//	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,

//-------------------------------------------------
//	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_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

//  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
}

namespace OnlineStore.DeviceLibrary
{
	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 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 多个Io同时操作
		[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 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, int hWnd);
		[DllImport("cdio.dll")] unsafe static extern int DioSetInterruptCallBackProc(short Id, PINTCALLBACK pIntCallBack, void *Param);
		
		// Trigger functions
		[DllImport("cdio.dll")] static extern int DioNotifyTrg(short Id, short TrgBit, short TrgKind, int Tim, int hWnd);
		[DllImport("cdio.dll")] static extern int DioStopNotifyTrg(short Id, short TrgBit);
		[DllImport("cdio.dll")] static extern int DioSetTrgCallBackProc(short Id, PTRGCALLBACK 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);

		//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, int hWnd);
		[DllImport("cdio.DLL")] unsafe static extern int DioDmSetStopCallBackProc(short Id, PDMSTOPCALLBACK CallBackProc, void *Param);
		[DllImport("cdio.DLL")] static extern int DioDmSetCountEvent(short Id, short Direction, uint Count, int hWnd);
		[DllImport("cdio.DLL")] unsafe static extern int DioDmSetCountCallBackProc(short Id, PDMCOUNTCALLBACK 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);

		// 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)
		{//获取单个端口的Io值,portNo=十六进制。 返回十六进制
			Data = 0;
			int ret = DioInpByte(Id, PortNo, ref Data);
			return ret;
		}

		public int InpBit(short Id, short BitNo, out byte Data)
		{
            //读取单个IO,按位读取
			Data = 0;
			int ret = DioInpBit(Id, BitNo, ref Data);
			return ret;
		}

		public int OutByte(short Id, short PortNo, byte Data)
		{//写DO
			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)
		{//读DO
			Data = 0;
			int ret = DioEchoBackByte(Id, PortNo, ref Data);
			return ret;
		}

		public int EchoBackBit(short Id, short BitNo, out byte Data)
		{//读DO
			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)
		{
            //获取多个端口的Io值
			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 OutMultiByte(short Id, short[] PortNo, short PortNum, byte[] Data)
		{//写多个DO
			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, int hWnd)
		{
            //中断,通过WndProc接受消息
			int ret = DioNotifyInterrupt(Id, IntBit, Logic, hWnd);
			return ret;
		}

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

		// Trigger functions
		public int NotifyTrg(short Id, short TrgBit, short TrgKind, int Tim, int 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, PTRGCALLBACK 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)
		{
            //获取输入输出端口数量,1个端口表示8个Io

			InPortNum = 0;
			OutPortNum = 0;
			int ret = DioGetMaxPorts(Id, ref InPortNum, ref OutPortNum);
			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, int hWnd)
		{
			int ret = DioDmSetStopEvent(Id, Direction, hWnd);
			return ret;
		}

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

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

		unsafe public int DmSetCountCallBackProc(short Id, PDMCOUNTCALLBACK 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;
		}

	}
}