PDA

View Full Version : USB communication with pic



Shamir
- 29th June 2006, 12:18
Hi,

I'm working on a porject that should send data from the pic to the PC by USB.

I use the EasyHid to get the pic programm and the VB programs, and I do get handle to the pic (182455), and when I send data the result is true (=data was sent), and also on the read command, but the problem is that I dont see it on the pic, the data it get is always zero.
Also if i put data in the buffer of the pic before sending it the VB program trough me a way.

Dose any one have an idea ? I guess that the communication is OK because I get the handle, the vendorID and productId - isn't it ?


Thanks,
Shamir

mister_e
- 29th June 2006, 13:49
As usual, post your PC code and PIC code here, it will be easier to see what could be the problem

Shamir
- 29th June 2006, 14:22
Hi,

all the project files are attached,

This is the pic code:

define OSC 20 ' Osc is 20MHz
DEFINE LOADER_USED 1 ' Bootlader if used
DEFINE RESET_ORG 800h ' For Microchip USB Bootloader
DEFINE INTERRUPT_ORG 808h ' For Microchip USB Bootloader

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 ' initialise USB...
usbbuffer[0] = $00
usbbuffer[1] = $01
usbbuffer[2] = $0f
usbbuffer[3] = $ff
ProgramStart:
gosub DoUSBIn
toggle portc.0
if usbbuffer[2] = 0 then
toggle portc.1
endif

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


The portc.1 is changed no matter what I send ==> the buffer = 0, but why?

*********************************************

The Visual basic code:


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

' read and write buffers
Private Const BufferInSize = 8
Private Const BufferOutSize = 8
Dim BufferIn(0 To BufferInSize)
Dim BufferOut(0 To BufferOutSize)



Private Sub find_Click()
hidConnect (Me.hwnd)
DeviceHandle = hidGetHandle(VendorID, ProductID)
did = hidGetVendorID(DeviceHandle)
pid = hidGetProductID(DeviceHandle)
InLng = hidGetInputReportLength(DeviceHandle)
OutLng = hidGetOutputReportLength(DeviceHandle)

hidSetReadNotify DeviceHandle, True
If DeviceHandle = 0 Then
Text1.Text = "Not FOUND"

Else
Text1.Text = "FOUND" & vbCrLf 'display data of the pic
Text1.Text = Text1.Text & did & vbCrLf
Text1.Text = Text1.Text & pid & vbCrLf
Text1.Text = Text1.Text & InLng & vbCrLf
Text1.Text = Text1.Text & OutLng & vbCrLf
End If


End Sub

' ************************************************** **************
' 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()
' do not remove!
' ConnectToHID (Me.hwnd)
' DeviceHandle = hidGetHandle(VendorID, ProductID)

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

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

'************************************************* ****************
' this is how you write some data...
'************************************************* ****************
Public Sub WriteSomeData()
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = 10 ' first data item, etc etc

' write the data (don't forget, pass the whole array)...
hidWriteEx VendorID, ProductID, BufferOut(0)
End Sub

Private Sub trans_Click()
'send data
DeviceHandle = hidGetHandle(VendorID, ProductID)
If Option1 = 0 Then
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = &HFF
BufferOut(2) = &HFF
BufferOut(3) = &HFF
BufferOut(4) = &HFF
BufferOut(5) = &HFF
BufferOut(6) = &HFF
BufferOut(7) = &HFF
BufferOut(8) = &HFF
Else
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = &H0
BufferOut(2) = &H0
BufferOut(3) = &H0
BufferOut(4) = &H0
BufferOut(5) = &H0
BufferOut(6) = &H0
BufferOut(7) = &H0
BufferOut(8) = &H0

End If
' write the data (don't forget, pass the whole array)...
sendsecc = hidWrite(DeviceHandle, BufferOut(0))
'sendsecc = hidWriteEx(VendorID, ProductID, BufferOut(0))
If sendsecc = 0 Then
Text2.Text = "no send" & vbCrLf
Else
Text2.Text = "send" & vbCrLf
Text2.Text = Text2.Text & BufferOut(0) & vbCrLf
Text2.Text = Text2.Text & BufferOut(1) & vbCrLf
Text2.Text = Text2.Text & BufferOut(2) & vbCrLf
Text2.Text = Text2.Text & BufferOut(3) & vbCrLf
Text2.Text = Text2.Text & BufferOut(4) & vbCrLf
Text2.Text = Text2.Text & BufferOut(5) & vbCrLf
Text2.Text = Text2.Text & BufferOut(6) & vbCrLf
Text2.Text = Text2.Text & BufferOut(7) & vbCrLf
Text2.Text = Text2.Text & BufferOut(8) & vbCrLf
End If
readseccs = 0
' read the data (don't forget, pass the whole array)...
readseccs = hidReadEx(VendorID, ProductID, BufferIn(0))
If readseccs = 0 Then
Text3.Text = "no read"
Else
Text3.Text = "read" & vbCrLf
Text3.Text = Text3.Text & "data" & vbCrLf
Text3.Text = Text3.Text & BufferIn(0) & vbCrLf
Text3.Text = Text3.Text & BufferIn(1) & vbCrLf
Text3.Text = Text3.Text & BufferIn(2) & vbCrLf
Text3.Text = Text3.Text & BufferIn(3) & vbCrLf
Text3.Text = Text3.Text & BufferIn(4) & vbCrLf
Text3.Text = Text3.Text & BufferIn(5) & vbCrLf
Text3.Text = Text3.Text & BufferIn(6) & vbCrLf
Text3.Text = Text3.Text & BufferIn(7) & vbCrLf
Text3.Text = Text3.Text & BufferIn(8) & vbCrLf

End If


End Sub
*****************************************

On the picture you can see the display, left to right:

1. the display as the program start.
2. the display after pressing "find f" - we see the P.id and Vid value and the size of the reports
3. What we send and what we get=0 after pressing "trans"

Thanks

Shamir
- 2nd July 2006, 05:49
Dose any one have a working HDI program using VB on the pc, so I can check my circute and lern from it ?

Thanks
Shamir

Demon
- 14th August 2006, 22:02
Shamir,

I see you have commented out the following line:
' ConnectToHID (Me.hwnd)

I assume you had the same error I have:
Sub or function not defined

I have copied MCHID.dll into the VB working folder and still get the same error (DLL copy suggested by Sean here):
http://www.picbasic.co.uk/forum/showpost.php?p=17460&postcount=12

Robert
:)

Demon
- 14th August 2006, 23:43
Uh, just pretend like I don't exist:
http://www.picbasic.co.uk/forum/showpost.php?p=24219&postcount=4

Robert
:D