求教:BCB如何与USB通信(有例子)
我想与USB设备通信,在网上找到很多文件都没能实现,下面程式是我在网上抄来在BCB中试验,编译可以通过,但还是打不开USB设备。请大虾们帮忙看看那里出错,我只想要打开USB的句柄,并向其他指令(后面要做的事)。
//##Unit1.h中的定义开始##
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//#define WINVER 0x0500
#include <windows.h>
#include <setupapi.h>
#include <basetyps.h>
#include <D:\C++\usb.h\usbdi.h>
#include <initguid.h>
#include <stdio.h>
#pragma comment(lib,"C:\Program Files\Borland\CBuilder6\Lib\Psdk\setupapi.lib")
#pragma comment(lib,"D:\C++\上位机程序1.2\hid.lib")
#pragma comment(lib,"C:\Program Files\Borland\CBuilder6\Lib\Psdk\comctl32.lib")
#ifndef BULKUSBH_INC
#define BULKUSBH_INC
#endif
#define BULKUSB_IOCTL_INDEX 0x0000
#define IOCTL_BULKUSB_GET_CONFIG_DESCRIPTOR CTL_CODE(FILE_DEVICE_UNKNOWN, \
BULKUSB_IOCTL_INDEX,\
METHOD_BUFFERED, \
FILE_ANY_ACCESS)
#define IOCTL_BULKUSB_RESET_DEVICE CTL_CODE(FILE_DEVICE_UNKNOWN, \
BULKUSB_IOCTL_INDEX+1,\
METHOD_BUFFERED, \
FILE_ANY_ACCESS)
#define IOCTL_BULKUSB_RESET_PIPE CTL_CODE(FILE_DEVICE_UNKNOWN, \
BULKUSB_IOCTL_INDEX+2,\
METHOD_BUFFERED, \
FILE_ANY_ACCESS)
extern HANDLE open_file(char *filename);
extern int GetUsbPath(char *path);
extern int WriteUsb(HANDLE hUsb,char *Outbuff, int len);
extern int ReadUsb(HANDLE hUsb,BYTE inbuff[],DWORD &nBytesRead,int nToRead);
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//##Unit1.h完##
///##Unit1.cpp内容开始##
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//8a3bf75d-83c7-440e-8276-5ae3f3ea6e77
//DEFINE_GUID(GUID_CLASS_I82930_BULK,0x8a3bf75d, 0x83c7, 0x440e,0x82, 0x76, 0x5a, 0xe3, 0xf3, 0xea, 0x6e, 0x77);
//{d6c5066e-72c1-11d2-9755-0000f8004788}
//DEFINE_GUID(GUID_CLASS_I82930_BULK,0xd6c5066e, 0x72c1, 0x11d2,0x97, 0x55, 0x00, 0x00, 0xf8, 0x00, 0x47, 0x88);
//{4D36E96B-E325-11CE-BFC1-08002BE10318} 标准键盘
DEFINE_GUID(GUID_CLASS_I82930_BULK,0x4D36E96b, 0xE325, 0x11CE,0xbf,0xc1,0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18);
BOOL GetUsbDeviceFileName( LPGUID pGuid, char *outNameBuf);
HANDLE OpenUsbDevice( LPGUID pGuid, char *outNameBuf);
//HANDLE OpenOneDevice (HDEVINFO HardwareDeviceInfo, PSP_INTE
HANDLE OpenOneDevice ( IN HDEVINFO HardwareDeviceInfo, IN PSP_INTERFACE_DEVICE_DATA DeviceInfoData, IN char *devName );
int WriteUsb(HANDLE hUsb,char *Outbuff, int len);
//int ReadUsb(HANDLE hUsb,BYTE inbuff[], DWORD &nBytesRead,int nToRead),RFACE_DEVICE_DATA DeviceInfoData,char *devName);
int ReadUsb(HANDLE hUsb,BYTE inbuff[], DWORD &nBytesRead,int nToRead) ;
int GetUsbPath(char *path);
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
/*名称:open_file
功能:打开USB设备
参数:filename 定义为”PIPE00” pipe name for bulk input pipe on our test board ,”PIPE01” pipe name for bulk output pipe on our test board。
PIPE00 和 PIPE01 是参考src\usb\bulkusb,我实际在用时这两个效果一样,为了避免USB异常引起的死机,我文件打开采用非阻塞模式。
*/
HANDLE open_file( char *filename)
{
int success = 1;
HANDLE h;
char completeDeviceName[256] = ""; //generated from the GUID registered by the driver itself
if ( !GetUsbDeviceFileName((LPGUID) &GUID_CLASS_I82930_BULK,completeDeviceName) )
{
//NOISY(("Failed to GetUsbDeviceFileName\n", GetLastError()));
ShowMessage("Failed to GetUsbDeviceFileName");
return INVALID_HANDLE_VALUE;
}
strcat (completeDeviceName, "\\");
strcat (completeDeviceName, filename);
//printf("completeDeviceName = (%s)\n", completeDeviceName);
h = CreateFile(completeDeviceName,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (h == INVALID_HANDLE_VALUE) {
//NOISY(("Failed to open (%s) = %d", completeDeviceName, GetLastError()));
success = 0;
} else {
//NOISY(("Opened successfully.\n"));
}
return h;
}
/*名称:GetUsbDeviceFileName
功能:获取USB设备路径
参数:pGUID
返回:outNameBuf USB设备路径
*/
BOOL GetUsbDeviceFileName( LPGUID pGuid, char *outNameBuf)
{
HANDLE hDev = OpenUsbDevice( pGuid, outNameBuf );
if ( hDev != INVALID_HANDLE_VALUE )
{
ShowMessage("open usb device ok!");
CloseHandle( hDev );
return TRUE;
}
ShowMessage("open usb device ng!");
return FALSE;
}
//下面所有