USB communication with pic


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Posts
    14

    Default USB communication with pic

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Wink

    As usual, post your PC code and PIC code here, it will be easier to see what could be the problem
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    May 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default the code...

    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
    Attached Images Attached Images  
    Attached Files Attached Files

  4. #4
    Join Date
    May 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default HDI programm

    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

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default

    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/show...0&postcount=12

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default

    Uh, just pretend like I don't exist:
    http://www.picbasic.co.uk/forum/show...19&postcount=4

    Robert
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

Similar Threads

  1. USB CDC Communications for Dummies!
    By Squibcakes in forum USB
    Replies: 104
    Last Post: - 15th January 2014, 13:43
  2. Reading a slave USB with a pic
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th October 2008, 12:00
  3. Replies: 15
    Last Post: - 30th October 2007, 19:25
  4. USB PIC without USB Connection
    By Tissy in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th December 2005, 17:39
  5. Pic to Pic communication?
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th April 2004, 19:41

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts