Can not send USB HID from VB to PIC

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Can not send USB HID from VB to PIC

Post by garryp4 » Thu Jul 12, 2007 9:54 am

I have the PICDEM FS USB demo board from Microchip, SF 2.0.0.2, and VB6. I can successfully send from the PIC to VB using HID but can not get any data from VB to get to the PIC. I do not know if the problem is in the VB code or the SF code. The hardware works because I can load the USBProject SF and VB code and get the LED's to come on and off when requested.

My VB code shows when the HID is connected and disconnected OK so I think the problem is I do not understand the examples well enough. Did a google search and found an example from mister_e on the PBP forum. I though I followed that VB example (and thanks for posting that mister_e!) but still no luck.

The SF code:

Code: Select all

// device and clock...                                                                                                                                                                                                                                         
Device = 18F4550
Clock = 48
   
// 20Mhz crystal, 48Mhz internal (FS USB)
Config
   PLLDIV = 5,
   CPUDIV = OSC1_PLL2,
   USBDIV = 2,
   FOSC = HSPLL_HS,
   VREGEN = ON

#option USB_DESCRIPTOR = "USBProjectDesc.bas"

// import the HID module...
Include "usart.bas"
Include "convert.bas"
Include "usbhid.bas"

Structure TRXReport
   indata(8) As Byte
End Structure    
Dim RXReport As TRXReport Absolute RXReportRAM

Dim led1     As PORTD.0
Dim led2     As PORTD.1
Dim loop1    As Byte

Sub LED_1(delay As Word)
  High (led1)
  DelayMS (delay)
  Low (led1)
End Sub  

Sub LED_2(delay As Word)
  High (led2)
  DelayMS (delay)
  Low (led2)
End Sub  

SetBaudrate(br19200)
USART.Write("I'M ALIVE",10,13)
LED_1(100)                       ' Do something
LED_2(100)

// connect to USB...
Repeat
Until HID.Attached

While true
   
   If DataAvailable Then
     LED_1(100)                  ' Flash if connected
     ReadReport
     For loop1 = 0 To 8 
       USART.Write(RXReport.indata(loop1)," ")' Verify structure data on Hyperterm
     Next
     USART.Write(10,13)  
   EndIf

'   If DataAvailable Then
'     LED_1(100)
'     ReadArray(Buffer,8)
'     For loop1 = 0 To 8 
'       USART.Write(Buffer(loop1)," ")' Verify structure data on Hyperterm
'     Next
'     USART.Write(10,13)  
'   EndIf  

Wend
Changed the bytes in/out to

Code: Select all

// EasyHID options...
#option USB_HAVE_SERIAL_STRING = false
#option USB_VID                = 6017
#option USB_PID                = 2000
#option HID_EP_OUT_POLLING_MS  = $0A
#option HID_EP_IN_POLLING_MS   = $0A
#option USB_BUS_POWER          = $32
#option HID_REPORT_BYTES_IN    = $40
#option HID_REPORT_BYTES_OUT   = $40
#option USB_SERVICE            = true

The VB code:

Code: Select all

' vendor and product IDs
Private Const VendorID = 6017
Private Const ProductID = 2000

' read and write buffers
Private Const BufferInSize = 64
Private Const BufferOutSize = 64
Dim BufferIn(0 To BufferInSize) As Byte
Dim BufferOut(0 To BufferOutSize) As Byte
Dim DataOut As Byte

Dim loop1 As Integer

' ****************************************************************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'*****************************************************************
Private Sub Form_Load()
   lblConnected.Caption = "HID Device Not Connected"
   ' do not remove!
   ConnectToHID (Me.hwnd)
End Sub

'*****************************************************************
' disconnect from the HID controller...
'*****************************************************************
Private Sub Form_Unload(Cancel As Integer)
   DisconnectFromHID
End Sub

'*****************************************************************
' a HID device has been plugged in...
'*****************************************************************
Public Sub OnPlugged(ByVal pHandle As Long)
   If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      ' ** YOUR CODE HERE **
      lblConnected.Caption = "HID Device Connected"
   End If
End Sub

'*****************************************************************
' a HID device has been unplugged...
'*****************************************************************
Public Sub OnUnplugged(ByVal pHandle As Long)
   If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
      ' ** YOUR CODE HERE **
      lblConnected.Caption = "HID Device Not Connected"
   End If
End Sub

'*****************************************************************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'*****************************************************************
Public Sub OnChanged()
   Dim DeviceHandle As Long
   ' get the handle of the device we are interested in, then set
   ' its read notify flag to true - this ensures you get a read
   ' notification message when there is some data to read...
   DeviceHandle = hidGetHandle(VendorID, ProductID)
   hidSetReadNotify DeviceHandle, True
End Sub

'*****************************************************************
' on read event...
'*****************************************************************
Public Sub OnRead(ByVal pHandle As Long)

   ' read the data (don't forget, pass the whole array)...
   If hidRead(pHandle, BufferIn(0)) Then
      ' ** YOUR CODE HERE **
      ' first byte is the report ID, e.g. BufferIn(0)
      ' the other bytes are the data from the microcontrolller...
        
     For loop1 = 1 To BufferInSize
       txtDataIn.Text = txtDataIn.Text & CInt(BufferIn(loop1)) & " "
     Next loop1
     txtDataIn.Text = txtDataIn.Text & vbCrLf
     
   End If

End Sub

'*****************************************************************
' this is how you write some data...
'*****************************************************************
Public Sub WriteSomeData()

 For loop1 = 0 To 8
   BufferOut(loop1) = loop1   ' first by is always the report ID & populate array
 Next
  
  ' write the data (don't forget, pass the whole array)...
   hidWriteEx VendorID, ProductID, BufferOut(0)
   txtDataIn.Text = "SENT" & vbCrLf
      
End Sub
 
Private Sub cmdSend_Click()
 
WriteSomeData

End Sub
They should look very similar to the EASYHid examples. If anyone can spot the problem I would sure appreciate the help.

Thanks.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Thu Jul 12, 2007 12:18 pm

It looks like you have some errors on the PIC side. The main problem being

Code: Select all

   If DataAvailable Then 
which should read

Code: Select all

   If HID.DataAvailable Then 
as DataAvailable is also available in the USART module and that is first in your includes list. I would also change

Code: Select all

   if HID.DataAvailable then
     ...
     for loop1 = 0 to 7 // only 8 data bytes sent...
        USART.Write(DecToStr(RXReport.indata(loop1))," ") // dec to string!
     next
     ...
   endif
The above works as expected using a Delphi test application and EasyHID DLL. I've also used the default HID descriptor for testing, which I would recommend you do also. That is,

Code: Select all

//  disable for testing #option USB_DESCRIPTOR = "USBProjectDesc.bas"
I can't comment on the VB code as I don't use it...

garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Thu Jul 12, 2007 2:20 pm

David:

You are absolutely correct! That change fixed it. I have got wrap my mind around this module concept and point to the desired one EVERY time. would HID.ReadReport also be correct with this protocol? Wish I could take you down to the local and buy you a pint, or better yet, e-mail a homebrew.

Thanks,

Garry

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Thu Jul 12, 2007 2:38 pm

You don't have to prefix with the module name every time. Check out the Swordfish online help under Language Reference...Creating Modules...Overview which explains it a little bit more

Post Reply