Click here to Skip to main content
15,901,853 members
Home / Discussions / C#
   

C#

 
QuestionHow to operate USB port Pin
wk_vigorous17-Jun-04 6:27
wk_vigorous17-Jun-04 6:27 
AnswerRe: How to operate USB port Pin
Aaron Eldreth17-Jun-04 6:46
Aaron Eldreth17-Jun-04 6:46 
GeneralRe: How to operate USB port Pin
eggie517-Jun-04 18:13
eggie517-Jun-04 18:13 
AnswerRe: How to operate USB port Pin
Dave Kreskowiak17-Jun-04 6:54
mveDave Kreskowiak17-Jun-04 6:54 
AnswerRe: How to operate USB port Pin
eggie517-Jun-04 18:14
eggie517-Jun-04 18:14 
GeneralRe: How to operate USB port Pin
wk_vigorous17-Jun-04 21:46
wk_vigorous17-Jun-04 21:46 
GeneralRe: How to operate USB port Pin
eggie517-Jun-04 22:06
eggie517-Jun-04 22:06 
GeneralRe: How to operate USB port Pin
eggie517-Jun-04 22:27
eggie517-Jun-04 22:27 
This complies into a nice .net library with 2 main methods (bulk read, write) that I can seamsly use in my C# app. Actually with no pinvoking.

// This is the main DLL file.

#include "stdafx.h"

#include "csusb.dll" //simple error handler
#include "ezusbsys.h" //comes with the cypress board
#include "usb100.h" //found in windows DDK; contains USB definitions for constants & structures.


using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

namespace EzUsb
{
	[StructLayout(LayoutKind::Explicit, Size=18, CharSet=CharSet::Auto)]
	public __gc class UsbDeviceDescriptor 
	{
	public:
		[FieldOffset(0)] System::Byte bLength;
		[FieldOffset(1)] System::Byte bDescriptorType;
		[FieldOffset(2)] System::UInt16 bcdUSB;
		[FieldOffset(4)] System::Byte bDeviceClass;
		[FieldOffset(5)] System::Byte bDeviceSubClass;
		[FieldOffset(6)] System::Byte bDeviceProtocol;
		[FieldOffset(7)] System::Byte bMaxPacketSize0;
		[FieldOffset(8)] System::UInt16 idVendor;
		[FieldOffset(10)] System::UInt16 idProduct;
		[FieldOffset(12)] System::UInt16 bcdDevice;
		[FieldOffset(14)] System::Byte iManufacturer;
		[FieldOffset(15)] System::Byte iProduct;
		[FieldOffset(16)] System::Byte iSerialNumber;
		[FieldOffset(17)] System::Byte bNumConfigurations;
	};

	public __gc class Usb
	{
	private:
		HANDLE _hEzUsb;

		void ReportError( String* msg )
		{
			if( OnErrorMessage )
			{
				OnErrorMessage( this, new ErrorEventArgs( msg ) );
			}
		}

		void CheckWin32Error()
		{
			int errCode = GetLastError(); 
			if( errCode != 0 )
			{
				char msg[256];
				FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, errCode, 0, 
					msg, 256, NULL );
				ReportError( msg );
			}
		}

	public:
		static const int MaxBlkSize = 64;

		__delegate void ErrorMessage( Object* sender, ErrorEventArgs* e );
		__event ErrorMessage* OnErrorMessage;

		Usb()
		{
			_hEzUsb = INVALID_HANDLE_VALUE;
		}

		~Usb()
		{
			if( _hEzUsb != INVALID_HANDLE_VALUE )
			{
				CloseHandle( _hEzUsb );
			}
		}

		bool Open( String* driverName )
		{
			char* lpFileName = new char[driverName->Length + 10];
			sprintf( lpFileName, "\\\\.\\%s", driverName );
			_hEzUsb = CreateFile( lpFileName,
									GENERIC_WRITE,
									FILE_SHARE_WRITE,
									NULL,
									OPEN_EXISTING,
									0,
									NULL);
			if( _hEzUsb == INVALID_HANDLE_VALUE )
			{
				CheckWin32Error();
			}
			delete lpFileName;

			return( _hEzUsb != INVALID_HANDLE_VALUE );
		}

		void Close()
		{
			CloseHandle( _hEzUsb );
		}

		Usb::UsbDeviceDescriptor* GetDeviceDescriptor()
		{
			BOOLEAN success;
			ULONG nBytes;
			PUSB_DEVICE_DESCRIPTOR pDeviceDiscriptor = new USB_DEVICE_DESCRIPTOR();

			if( _hEzUsb == INVALID_HANDLE_VALUE )
			{
				ReportError( "Driver not open." );
			}
			else
			{
				success = DeviceIoControl( _hEzUsb,
						IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
						NULL,
						0,
						pDeviceDiscriptor,
						sizeof(USB_DEVICE_DESCRIPTOR),
						&nBytes,
						NULL );
				if( success == FALSE )
				{
					CheckWin32Error();
				}
			}

			UsbDeviceDescriptor* DeviceDiscriptor = new UsbDeviceDescriptor();
			DeviceDiscriptor = (UsbDeviceDescriptor*)Marshal::PtrToStructure(
					pDeviceDiscriptor, DeviceDiscriptor->GetType() );

			delete pDeviceDiscriptor;

			return( DeviceDiscriptor );
		}

		System::Byte BulkRead( int pipe ) []
		{
			if( _hEzUsb == INVALID_HANDLE_VALUE )
			{
				ReportError( "Driver not open." );
				return( 0 );
			}

			char lpOutBuffer[MaxBlkSize];

			BULK_TRANSFER_CONTROL btc;
			btc.pipeNum = pipe;
			BOOL success;
			int nBytes;

			success = DeviceIoControl( _hEzUsb,
						IOCTL_EZUSB_BULK_READ, 
						&btc,
						sizeof (BULK_TRANSFER_CONTROL),
						lpOutBuffer,
						MaxBlkSize,
						(unsigned long *)&nBytes,
						NULL);
			if( success == FALSE )
			{
				CheckWin32Error();
				return( 0 );
			}

			System::Byte buffer  __gc[] = new System::Byte[nBytes];
			IntPtr ptr( lpOutBuffer );
			Marshal::Copy( ptr, buffer, 0, nBytes );

			return( buffer );
		}

		int BulkWrite( System::Byte buffer __gc[], int pipe )
		{
			if( _hEzUsb == INVALID_HANDLE_VALUE )
			{
				ReportError( "Driver not open." );
				return( 0 );
			}

			char* lpOutBuffer = new char[buffer->Length];
			Marshal::Copy( buffer, 0, lpOutBuffer, buffer->Length );

			BULK_TRANSFER_CONTROL btc;
			BOOLEAN success;
			int nBytes;

			btc.pipeNum = pipe;

			success = DeviceIoControl( _hEzUsb,
						IOCTL_EZUSB_BULK_WRITE, 
						&btc,
						sizeof (BULK_TRANSFER_CONTROL),
						lpOutBuffer,
						buffer->Length,
						(unsigned long *)&nBytes,
						NULL);
			if( !success )
			{
				CheckWin32Error();
			}

			delete lpOutBuffer;

			return( nBytes );
		}
	};
}


/\ |_ E X E GG
GeneralRe: How to operate USB port Pin
wk_vigorous18-Jun-04 3:41
wk_vigorous18-Jun-04 3:41 
GeneralData Formatting Expression Pin
Brian Delahunty17-Jun-04 4:54
Brian Delahunty17-Jun-04 4:54 
GeneralRe: Data Formatting Expression Pin
Dave Kreskowiak17-Jun-04 6:20
mveDave Kreskowiak17-Jun-04 6:20 
GeneralRe: Data Formatting Expression Pin
Brian Delahunty17-Jun-04 6:29
Brian Delahunty17-Jun-04 6:29 
GeneralRe: Data Formatting Expression Pin
Dave Kreskowiak17-Jun-04 6:50
mveDave Kreskowiak17-Jun-04 6:50 
GeneralRe: Data Formatting Expression Pin
Brian Delahunty17-Jun-04 6:55
Brian Delahunty17-Jun-04 6:55 
GeneralMessaging system Pin
jremignanti17-Jun-04 3:46
jremignanti17-Jun-04 3:46 
GeneralRe: Messaging system Pin
Mazdak17-Jun-04 4:53
Mazdak17-Jun-04 4:53 
GeneralRe: Messaging system Pin
Heath Stewart18-Jun-04 5:07
protectorHeath Stewart18-Jun-04 5:07 
Generalarrowup / arrowdown char Pin
Member 117814517-Jun-04 3:33
Member 117814517-Jun-04 3:33 
GeneralRe: arrowup / arrowdown char Pin
Aaron Eldreth17-Jun-04 4:31
Aaron Eldreth17-Jun-04 4:31 
QuestionASSEMBLY?!? Pin
Brendan Vogt17-Jun-04 2:32
Brendan Vogt17-Jun-04 2:32 
AnswerRe: ASSEMBLY?!? Pin
Heath Stewart17-Jun-04 3:03
protectorHeath Stewart17-Jun-04 3:03 
AnswerRe: ASSEMBLY?!? Pin
Corinna John17-Jun-04 3:02
Corinna John17-Jun-04 3:02 
GeneralRe: ASSEMBLY?!? Pin
Heath Stewart17-Jun-04 3:22
protectorHeath Stewart17-Jun-04 3:22 
Generalcomparing objects Pin
saud_a_k17-Jun-04 1:15
saud_a_k17-Jun-04 1:15 
GeneralRe: comparing objects Pin
Corinna John17-Jun-04 1:27
Corinna John17-Jun-04 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.