C++builder读写USB口的问题,请大家尽量提供参考

stuwei 2002-09-05 07:51:59
抛开驱动程序这一层,使用createfile 怎么读写USB口
哪位能给个例子,谢谢了!!!
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hlmsoft 2002-09-05
  • 打赏
  • 举报
回复
好,怎么改vc了:)
kingcaiyao 2002-09-05
  • 打赏
  • 举报
回复
给一个usb鼠标的例子(我用VC写的),需要hid.dll:
unsigned int VendorID,ProductID;//Productid,VendorID
char temp_buffer[100];
unsigned char DataBuffer[100],BufferLength; //接收
unsigned char SendDataBuffer[100],SendBufferLength; //发送
int HIDCounter=0;
GUID hidGuid;
HDEVINFO AllHIDDeviceInfo=NULL;
HANDLE HIDDevice=NULL,ReadHIDDevice=NULL;
HIDD_ATTRIBUTES Attributes;
PSP_DEVICE_INTERFACE_DETAIL_DATA ClassDeviceData;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG neededLength,requiredLength;
DWORD bytesRead;

bool InitUSB()
{
//HID human interface device
//VendorID = 0x0000; //Vendor ID
//ProductID = 0x0000;//Product ID
VendorID=0x4B4; //这里我们使用的是CYPRESS公司的USB鼠标
ProductID=0x1;
// Search device
// 本例程使用HID设备的API,它查找HID设备列表,找出与Vendor ID 和 Product ID匹配的设备

ClassDeviceData =NULL;
HIDDevice =NULL;
deviceInfoData.cbSize =sizeof(deviceInfoData);

//We have to get the GUID from hid class device
HidD_GetHidGuid(&hidGuid);

// Get informations about hid device
AllHIDDeviceInfo=SetupDiGetClassDevs(&hidGuid,NULL,NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
//DIGCF_PRESENT=&H2
//DIGCF_INTERFACEDEVICE=&H10
HIDCounter=0;
while (TRUE)
{

// 这个API将发现的设备信息写入 deviceInfoData
// HIDCounter 允许这个API重复调用所有HID设备
// 如果API调用返回0,没有更多的HID设备发现
if (!SetupDiEnumDeviceInterfaces(AllHIDDeviceInfo,0,&hidGuid,
HIDCounter,&deviceInfoData))
{
// 没有发现与Vendor ID 和 Product ID匹配的HID设备
AfxMessageBox("I found no hid device");
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
return FALSE;
//break;
}
else
{

// 发现一个HID设备,获取设备的详细信息
// 第一次调用SetupDiGetDeviceInterfaceDetail得到ClassDeviceData
// 的大小,但返回错误

SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData,
NULL,0,&requiredLength,NULL);

neededLength =requiredLength;
ClassDeviceData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(neededLength);
ClassDeviceData->cbSize =sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

//第二次调用SetupDiGetDeviceInterfaceDetail
// 使用 合适的neededLength.
if (!SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData,
ClassDeviceData,neededLength,&requiredLength,NULL))
{
free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
return FALSE;
}

// 建立HID设备的句柄
HIDDevice=CreateFile(ClassDeviceData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL);

// 获取 attributes 以便得到Vendor ID 和 Product ID

HidD_GetAttributes(HIDDevice,&Attributes);

if ((Attributes.VendorID == VendorID) &&
(Attributes.ProductID == ProductID))
{


// 找到了匹配的Vendor ID 和 Product ID的HID设备
// 建立ReadHIDDevice设备的句柄以便读取信息

ReadHIDDevice=CreateFile(ClassDeviceData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL);
free(ClassDeviceData);
SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo);
AfxMessageBox("My device is found!!!");
//初始化成功
return TRUE;
}
else
{

//CString str;
//str.Format("%d,%d",VendorID,ProductID);
//AfxMessageBox(str);
//AfxMessageBox("Initialization success!");
CloseHandle(HIDDevice);
}
free(ClassDeviceData);
HIDCounter = HIDCounter+1;
}
}

}
//Read from usb prot,that is listen to usb port ,if it has character,system would display it on user interface
bool ReadUSB()
{


BufferLength=100;
memset(DataBuffer,0,100);

if(!ReadFile(ReadHIDDevice,DataBuffer,BufferLength,&bytesRead,NULL)) //失败
return false; //读数失败立即返回
else



return true;

}
//Wirte a character or a string to usb port
bool WriteUSB()
{

/*CString str;
SendBufferLength=100;
for(int i=0;i<100;i++)
{
SendDataBuffer[i]=65;
}
str=SendDataBuffer;
if (!WriteFile(ReadHIDDevice,SendDataBuffer,SendBufferLength,&bytesRead,NULL))

{
return false;

//失败
}
else
{
AfxMessageBox("Success!");
return true;
//成功
} */
return true;

}





void CUsblistenDlg::OnCancel()
{
int button;
button=AfxMessageBox("Do you want to exit?",MB_YESNO+MB_ICONWARNING);
if(button==IDYES)
{
KillTimer(1);
EndDialog(IDYES);
}
}

//Thread callback function
void CUsblistenDlg::OnSend()
{



// TODO: Add your control notification handler code here
SendDataBuffer[0]='C';

WriteUSB();

}

void CUsblistenDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString str;

if(ReadUSB())
{
//AfxMessageBox("Read Successfully");
}
else
{
//AfxMessageBox("Read Failingly");
return;
}
str=DataBuffer;
if(DataBuffer[0]!=0)
{
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST1);
listbox->AddString(str);
}
else
{
CListBox *listbox=(CListBox*)GetDlgItem(IDC_LIST1);
listbox->AddString("No Charcters is sent to usb port");
}
CDialog::OnTimer(nIDEvent);
}

void CUsblistenDlg::OnOK()
{
// TODO: Add extra validation here

CDialog::OnOK();
}

HBRUSH CUsblistenDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

UINT uID=pWnd->GetDlgCtrlID();
switch(uID)
{
case IDC_LIST1:
pDC->SetTextColor(RGB(255,255,0));
pDC->SetBkColor(RGB(255,0,0));
break;
}
// TODO: Change any attributes of the DC here

// TODO: Return a different brush if the default is not desired
return hbr;
}
maxying 2002-09-05
  • 打赏
  • 举报
回复
hoho, 哪位大侠帖些代码啊~~~
kingcaiyao 2002-09-05
  • 打赏
  • 举报
回复
如果完全抛开驱动程序这一层来对usb设备进行操作,这需要供应商提供一个开发包给你.当然如果你的设备属于hid设备(Human Interface Device)那就不必了,那直接用hid.dll就可以了(比如说usb鼠标,usb键盘).
hillhero789 2002-09-05
  • 打赏
  • 举报
回复
我都很想知道

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧