Page 1 of 1

A problem confuse me long time-Can't read data

Posted: Fri Jul 09, 2010 7:57 am
by M.H.Jack
I study V-USB probably two weeks. At first I don't know how to compile the firmware and the commandline tool, usually get into all kinds of trouble. Finally two days ago, I was so happy,I succeeded in compiling them without a error. However, a new problem comes.
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];
}
//---------------------------------------------------------------------------

Re: A problem confuse me long time-Can't read data

Posted: Sat Jul 10, 2010 2:09 am
by M.H.Jack
Nobody help me? :(

Re: A problem confuse me long time-Can't read data

Posted: Tue Aug 31, 2010 3:01 pm
by Vaidotas
Maybe I don't understand your problem, becouse my english is not good, but I'll try to help you. The problem is that you describe buffer as char, but you do not write any information in it. I think you want to know why buffer[1-4]0=NULL, this is becouse you do not put information in it. I think it could help you.
Maybe in this forum is more people who is making programs with builder c++ and maybe they want to share information about it.