PDA

View Full Version : EasyHid and 18F2550 - why? what? how?



Giulio
- 28th December 2006, 07:57
Hi,

I have a 18F2550 talking to windows with no apparent problems [see attached screendump].

Easyhid generated the code, and I made what I believed to be the relevant changes [minor ones at first, in order to test all is well].

Sure enough, if I connect the cable, the 'OnPlugged' event fires and the textbox changes to 'Plugged In..." [see code below].

However, no matter what I do, I cannot get the 'hidRead' event to fire, nor does the 'OnUnplugged' event ever fire, when I disconnect the cable.

I have scoured these forums and the 'net in general, and cannot see what I am missing...

I've included the code snippets below. Any help would not only be much appreciated, but would save me from ritual hari-kari... ;-)

Giulio

;------------------------------

'************************************************* ****************
' 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 **

Text1.Text = "Plugged In..."

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 **

Text1.Text = "Unplugged..."

End If
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 **

Text1.Text = Val(BufferIn(0))

' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontrolller...
End If
End Sub

;-------------------------------------------------

pic code
;---------

DEFINE OSC 20

USBBufferSizeMax con 8 ' maximum buffer size
USBBufferSizeTX con 8 ' input
USBBufferSizeRX con 8 ' output

' the USB buffer...
USBBuffer Var Byte[USBBufferSizeMax]
USBBufferCount Var Byte

' ************************************************** **********
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBService every couple *
' * of milliseconds or so... *
' ************************************************** **********

usbinit
ProgramStart:
' gosub DoUSBIn
USBBuffer[0] = 79
USBBuffer[1] = 75
gosub DoUSBOut
goto ProgramStart

' ************************************************** **********
' * receive data from the USB bus *
' ************************************************** **********
DoUSBIn:
USBBufferCount = USBBufferSizeRX ' RX buffer size
USBService ' keep connection alive
USBIn 1, USBBuffer, USBBufferCount, DoUSBIn ' read data, if available
return

' ************************************************** **********
' * wait for USB interface to attach *
' ************************************************** **********
DoUSBOut:
USBBufferCount = USBBufferSizeTX ' TX buffer size
USBService ' keep connection alive
USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
return