《C#实现的USB接口操作类.doc》由会员分享,可在线阅读,更多相关《C#实现的USB接口操作类.doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 USB在日常中已经广泛被使用,手机,MP3,MP4,相机等都采用了USB接口。本文向大家介绍一个C#实现的USB接口操作类。namespaceConsoleApplication1classProgramstaticvoidMain(stringargs)USBusb;usb=newUDisk();/插入U盘usb.OutputFile();/从U盘读出文件usb.InputFile();/往U盘写入文件usb.Dispose();/拔出U盘Console.WriteLine();usb=newMDisk();/插入移动硬盘usb.OutputFile();/从移动硬盘读出文件usb.Inp
2、utFile();/往移动硬盘写入文件usb.Dispose();/拔出移动硬盘Console.WriteLine();usb=newMP4();/插入MP4usb.OutputFile();/从MP4读出文件usb.InputFile();/往MP4写入文件usb.Dispose();/拔出MP4Console.ReadKey();/USB接口publicinterfaceUSB:IDisposablevoidOutputFile();/读出文件voidInputFile();/写入文件/U盘publicclassUDisk:USBpublicUDisk()Console.WriteLine
3、(U盘准备就绪.);publicvoidOutputFile()Console.WriteLine(从U盘读出文件);publicvoidInputFile()Console.WriteLine(往U盘写入文件);publicvoidDispose()Console.WriteLine(U盘已被拔出);/移动硬盘publicclassMDisk:USBpublicMDisk()Console.WriteLine(移动硬盘准备就绪.);publicvoidOutputFile()Console.WriteLine(从移动硬盘读出文件);publicvoidInputFile()Console.W
4、riteLine(往移动硬盘写入文件);publicvoidDispose()Console.WriteLine(移动硬盘已被拔出);/MP4publicclassMP4:USBpublicMP4()Console.WriteLine(MP4准备就绪.);publicvoidOutputFile()Console.WriteLine(从MP4读出文件);publicvoidInputFile()Console.WriteLine(往MP4写入文件);publicvoidDispose()Console.WriteLine(MP4已被拔出);用C#写的一个读写USB口操作的类using Syst
5、em;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace WindowsApplication1classClass1 DllImport(kernel32.dll)privatestaticexternIntPtr CreateFile(String lpFileName,UInt32 dwDesiredAccess,UInt32 dwShareMode,IntPtr lpSecurityAttributes,UInt32 dwCreationDis
6、position,UInt32 dwFlagsAndAttributes,IntPtr hTemplateFile); DllImport(Kernel32.dll)privatestaticexternbool ReadFile(IntPtr hFile,byte lpBuffer,uint nNumberOfBytesToRead,refuint lpNumberOfBytesRead,IntPtr lpOverlapped ); DllImport(Kernel32.dll)privatestaticexternbool WriteFile(IntPtr hFile,byte lpBuf
7、fer,uint nNumberOfBytesToWrite,refuint lpNumberOfBytesWritten,IntPtr lpOverlapped ); DllImport(kernel32.dll)privatestaticexternbool CloseHandle(IntPtr hObject );/-IntPtr hFile;privateconstUInt32 GENERIC_READ = 0x80000000;privateconstUInt32 GENERIC_WRITE = 0x40000000;privateconstUInt32 OPEN_EXISTING
8、= 3;privateconstInt32 INVALID_HANDLE_VALUE = -1;privateconstint USB_WRITENUM = 8;privateconstint USB_READNUM = 8;privatebyte m_rd_data = newbyteUSB_READNUM;publicbyte rd_data get return m_rd_data; set m_rd_data = value; privatebyte m_wr_data = newbyteUSB_WRITENUM;publicbyte wr_data get return m_wr_d
9、ata; set m_wr_data = value; publicbool OnInitUSB() hFile = IntPtr.Zero;string deviceName = string.Empty; deviceName = 在这里写上你的设备的地址; hFile = CreateFile( deviceName, GENERIC_READ | GENERIC_WRITE, 0,IntPtr.Zero,OPEN_EXISTING, 0,IntPtr.Zero );return hFile.ToInt32() = INVALID_HANDLE_VALUE ? false : true;
10、 publicbool USBDataRead() uint read = 0;return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero); publicbool USBDataWrite() uint written = 0;return WriteFile(hFile, m_wr_data, (uint)USB_WRITENUM, ref written, IntPtr.Zero); publicvoid CloseConnection() if (hFile.ToInt32() != INVALID_HANDLE_VALUE) CloseHandle(hFile); hFile = IntPtr.Zero; 5 / 5