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
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
Thanks.