Now I am studying the custom-class example. My device can recognized by computer, and I can control the device with the commandline tool. Then I want to build a GUI tool with C++Builder. Now I have builded the tool, I can sent data to device to control the LED, but can't read data from device to get the LED status.
Run the program and press the "Get Status" Button, result is:
cnt=1;
buffer[0]=null;
buffer[1]=null;
buffer[2]=null;
buffer[3]=null;
I post the code below, hope somebody can help me
Code: Select all
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "usb.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define USBDEV_VENDOR 0x16c0
#define USBDEV_PRODUCT 0x05dc
TForm1 *Form1;
static usb_dev_handle *usbhandle;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
usbled_open();
}
//---------------------------------------------------------------------------
int usb_open()
{
struct usb_bus *bus;
struct usb_device *dev = 0;
usb_init();
usb_find_busses();
usb_find_devices();
for(bus=usb_get_busses(); bus; bus=bus->next)
{
for(dev=bus->devices; dev; dev=dev->next)
{
if((dev->descriptor.idVendor == USBDEV_VENDOR) && (dev->descriptor.idProduct == USBDEV_PRODUCT))
break;
}
if(dev)
break;
}
if(!dev){
ShowMessage("Can't find USB device!");
usbhandle=NULL;
return 1;
}
usbhandle = usb_open(dev);
if(!usbhandle){
ShowMessage("Can't open USB!");
usbhandle=NULL;
return 2;
}
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(!usbhandle)
return;
usb_close(usbhandle);
}
//LED ON---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int cnt;
char send[4];
char buffer[4];
cnt = usb_control_msg(usbhandle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
1,
1,
0,
buffer,
0,
5000);
}
//LED OFF---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int cnt;
char send[4];
char buffer[4];
cnt = usb_control_msg(usbhandle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
1,
0,
0,
buffer,
0,
5000);
}
//GET Status---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int cnt;
char send[4];
char buffer[4];
cnt = usb_control_msg(usbhandle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
2,
0,
0,
(char *)buffer,
sizeof(buffer),
5000);
Edit2->Text=cnt;
Edit1->Text=buffer[0];
Edit3->Text=sizeof(buffer);
Edit4->Text=buffer[0];
Edit5->Text=buffer[1];
Edit6->Text=buffer[2];
Edit7->Text=buffer[3];
}
//---------------------------------------------------------------------------