title: STM32F103 - PC communication via USB
body: <p>I am trying to use the <a href="https://code.google.com/p/found-bits/" rel="nofollow">found-bits</a> project to communicate with my STM32F103 via USB from a Windows application with no success.</p>
  
  <p>The program keeps returning an error code 31 at the <code>WinUsb_ControlTransfer</code> function call.</p>
  
  <p>I made some changes that solved the problem for receiving the data from the default end point but the issue remains when trying to send data to it.</p>
  
  <p>How can I get my windows application to send and receive data from the STM32? </p>
  
  <p>P.S.: I created .inf and .cat files for my device and installed the driver</p>
  
  <pre><code>BOOL SendDatatoDefaultEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, BYTE byWinUSBCommControl, BYTE *pbyData = NULL, WORD wNumBytesCount = 0)
  {
    if (hDeviceHandle==INVALID_HANDLE_VALUE)
    {
      return FALSE;
    }
    BOOL bResult = TRUE;
    WINUSB_SETUP_PACKET SetupPacket;
    ZeroMemory(&amp;SetupPacket, sizeof(WINUSB_SETUP_PACKET));
    ULONG cbSent = 0;
  
    //Create the setup packet
    SetupPacket.RequestType = (BMREQUEST_HOST_TO_DEVICE &lt;&lt; 7)/* | (BMREQUEST_VENDOR &lt;&lt; 5) | BMREQUEST_TO_INTERFACE*/;
    //SetupPacket.Request = byWinUSBCommControl;
    //SetupPacket.Value = 0;
    SetupPacket.Index = 0; // specify WinUSBComm interface
    //SetupPacket.Length = wNumBytesCount;
    SetupPacket.Request = USB_REQUEST_GET_DESCRIPTOR;
    SetupPacket.Value = USB_DEVICE_DESCRIPTOR_TYPE &lt;&lt; 8;
    SetupPacket.Length = sizeof(USB_DEVICE_DESCRIPTOR);
  
    bResult = WinUsb_ControlTransfer(hDeviceHandle, SetupPacket, pbyData, wNumBytesCount, &amp;cbSent, 0);
    if(!bResult)
    {
      printf("Error WinUsb_ControlTransfer: %d.\n", GetLastError());
      goto done;
    }
  
    PTRACE("Data sent: %d \nActual data transferred: %d.\n", wNumBytesCount, cbSent);
  
    done:
      return bResult;
  }
  
  BOOL GetDataFromDefaultEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, BYTE byWinUSBCommControl, BYTE *pbyData, WORD wNumBytesCount)
  {
    if ( 0 == wNumBytesCount )
    {
      return TRUE;
    }
  
    if ( NULL == pbyData )
    {
      return FALSE;
    }
  
    if (hDeviceHandle==INVALID_HANDLE_VALUE)
    {
      return FALSE;
    }
    BOOL bResult = TRUE;
    WINUSB_SETUP_PACKET SetupPacket;
    ZeroMemory(&amp;SetupPacket, sizeof(WINUSB_SETUP_PACKET));
    ULONG cbSent = 0;
  
    //Create the setup packet
    SetupPacket.RequestType = (BMREQUEST_DEVICE_TO_HOST &lt;&lt; 7)/* | (BMREQUEST_VENDOR &lt;&lt; 5) | BMREQUEST_TO_DEVICE*/;
    //SetupPacket.Request = byWinUSBCommControl;
    //SetupPacket.Value = 0;
    SetupPacket.Index = 0; // specify WinUSBComm interface
    //SetupPacket.Length = wNumBytesCount;
    SetupPacket.Request = USB_REQUEST_GET_DESCRIPTOR;
    SetupPacket.Value = USB_DEVICE_DESCRIPTOR_TYPE &lt;&lt; 8;
    SetupPacket.Length = sizeof(USB_DEVICE_DESCRIPTOR);
  
    bResult = WinUsb_ControlTransfer(hDeviceHandle, SetupPacket, pbyData, wNumBytesCount, &amp;cbSent, 0);
    if(!bResult)
    {
      printf("Error WinUsb_ControlTransfer: %d.\n", GetLastError());
      goto done;
    }
  
    PTRACE("Data get : %d \nActual data transferred: %d.\n", wNumBytesCount, cbSent);
  
    done:
      return bResult;
  }
  </code></pre>