USB to VB.NET


Closed Thread
Results 1 to 13 of 13

Thread: USB to VB.NET

  1. #1

    Default USB to VB.NET

    Can someone please assist me, i am trying to get a PIC18F4550 to talk to a VB application.
    If i connect the pic the software detects the pic but no data comes through.
    I have hard coded the data witch should be sended.
    any advice on how to send data back to the PIC would also be appreciated

    PIC Code
    Code:
    DEFINE OSC 48          
    DEFINE LOADER_USED 1
    
    USBBufferSizeTX    con 8  ' input 
    USBBufferSizeRX    con 8  ' output
    
    ' the USB buffer...
    USBBufferTX       Var Byte[USBBufferSizeTX]
    USBBufferRX       Var Byte[USBBufferSizeRX] 
    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...
    ProgramStart: 
    	USBBufferTX[0] = "1"
    	USBBufferTX[1] = "2"
    	USBBufferTX[2] = "3"
    	USBBufferTX[3] = "4"
    	USBBufferTX[4] = "5"
    	USBBufferTX[5] = "6"
    	USBBufferTX[6] = "7"
    	USBBufferTX[7] = "8"
       gosub DoUSBIn
       gosub DoUSBOut
       goto ProgramStart   
    
    ' ************************************************************
    ' * receive data from the USB bus                            *
    ' ************************************************************
    DoUSBIn:
       USBBufferCount = USBBufferSizeRX              ' RX buffer size
       USBService                                    ' keep connection alive
       USBIn 1, USBBufferRX, 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, USBBufferTX, USBBufferCount, DoUSBOut ' if bus available, transmit data
       return
    VB.NET code the form only contains textboxes and i use VB.NET2008
    Code:
    Option Strict Off
    Option Explicit On
    
    Friend Class MainForm
        Inherits System.Windows.Forms.Form
    
        ' vendor and product IDs
        Private Const VendorID As Short = 6017
        Private Const ProductID As Short = 2000
    
        ' read and write buffers
        Private Const BufferInSize As Short = 8
        Private Const BufferOutSize As Short = 8
        Dim BufferIn(BufferInSize) As Byte
        Dim BufferOut(BufferOutSize) As Byte
    
        ' ****************************************************************
        ' when the form loads, connect to the HID controller - pass
        ' the form window handle so that you can receive notification
        ' events...
        '*****************************************************************
        Private Sub MainForm_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
            ' do not remove!
            ConnectToHID(Me.Handle.ToInt32)
        End Sub
    
        '*****************************************************************
        ' disconnect from the HID controller...
        '*****************************************************************
        Private Sub MainForm_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            DisconnectFromHID()
        End Sub
    
        '*****************************************************************
        ' a HID device has been plugged in...
        '*****************************************************************
        Public Sub OnPlugged(ByVal pHandle As Integer)
            If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
                ' ** YOUR CODE HERE **
                TextBox1.Text = "plugged"
            End If
        End Sub
    
        '*****************************************************************
        ' a HID device has been unplugged...
        '*****************************************************************
        Public Sub OnUnplugged(ByVal pHandle As Integer)
            If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
                hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
                TextBox1.Text = "unplugged"
            End If
        End Sub
    
        '*****************************************************************
        ' controller changed notification - called
        ' after ALL HID devices are plugged or unplugged
        '*****************************************************************
        Public Sub OnChanged()
            ' 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...
            Dim pHandle As Integer
            pHandle = hidGetHandle(VendorID, ProductID)
            hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
        End Sub
    
        '*****************************************************************
        ' on read event...
        '*****************************************************************
        Public Sub OnRead(ByVal pHandle As Integer)
            ' 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 microcontroller...
                TextBox2.Text = BufferIn(0)
                TextBox3.Text = BufferIn(1)
                TextBox4.Text = BufferIn(2)
                TextBox5.Text = BufferIn(3)
                TextBox6.Text = BufferIn(4)
                TextBox7.Text = BufferIn(5)
                TextBox8.Text = BufferIn(6)
                TextBox9.Text = BufferIn(7)
            End If
        End Sub
    End Class

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    After setting the data in the TX buffer, the first thing you do is ...
    gosub DoUSBIn

    It will sit there until it receives something from the PC, and nothing will be sent.
    The VB program doesn't send anything, so it's stuck in the DoUSBIn subroutine.

    The subroutine can be changed to continue if nothing is received.
    DT

  3. #3
    Join Date
    Oct 2004
    Posts
    448

    Default

    Quote Originally Posted by Darrel Taylor View Post
    It will sit there until it receives something from the PC, and nothing will be sent.
    The VB program doesn't send anything, so it's stuck in the DoUSBIn subroutine.
    Precisely where I am stuck in my misadventures with USB.

    I dont think I have fully understood the syntax and the arguments for USBIN;

    How exactly do you timeout and get out of the subroutine, Darrel?

    "USBIn 1, USBBuffer, USBBufferCount, DoUSBIn"

    Is it by changing the last argument, or is there any other way?

    Anand

  4. #4

    Default

    After looking at some other code this is what i did and it works, now for the next one sending data from the VB application
    Code:
    ' ************************************************************
    ' * receive data from the USB bus                            *
    ' ************************************************************
    DoUSBIn:
       USBBufferCount = USBBufferSizeRX              ' RX buffer size
       USBService                                    ' keep connection alive
       USBIn 1, USBBufferRX, USBBufferCount, Timeout   ' read data, if available
    Timeout:                                            '
       return
        
    ' ************************************************************
    ' * wait for USB interface to attach                         *
    ' ************************************************************
    DoUSBOut:
     WaitPC:
       USBBufferCount = USBBufferSizeTX              ' TX buffer size
       USBService                                    ' keep connection alive
       USBOut 1, USBBufferTX, USBBufferCount, Waitpc ' if bus available, transmit data
       return

  5. #5
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637

    Default

    Quote Originally Posted by Andre_Pretorius View Post
    After looking at some other code this is what i did and it works, now for the next one sending data from the VB application
    Code:
    ' ************************************************************
    ' * receive data from the USB bus                            *
    ' ************************************************************
    DoUSBIn:
       USBBufferCount = USBBufferSizeRX              ' RX buffer size
       USBService                                    ' keep connection alive
       USBIn 1, USBBufferRX, USBBufferCount, Timeout   ' read data, if available
    Timeout:                                            '
       return
        
    ' ************************************************************
    ' * wait for USB interface to attach                         *
    ' ************************************************************
    DoUSBOut:
     WaitPC:
       USBBufferCount = USBBufferSizeTX              ' TX buffer size
       USBService                                    ' keep connection alive
       USBOut 1, USBBufferTX, USBBufferCount, Waitpc ' if bus available, transmit data
       return
    For DoUSBin that is also what I did to get out of the loop. However, I don't know if that is the best solution. The problem that I found with this approach is that I lost some of the incoming USB data. Somehow, some of the USB data going to the PIC got lost while the PIC was doing something else. I don't know if the incomming data sits in the PIC buffers waiting to be read or not.

    I got the best results by combinning the original DoUSBin routine with the modified one.

    Robert

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Another easy way is to use USB_ASM_Service.pbp ...
    http://www.picbasic.co.uk/forum/show...8306#post78306

    Then you don't have to worry about adding USBSERVICE or USBINIT, it's handled by the USB interrupts.

    It also tells you when something has been received, so you can gosub to DoUSBin only when needed.

    Code:
    Main:
        IF RX_READY THEN GOSUB DoUSBin
    ; ...
    GOTO Main
    Has a TX_READY too, which tells you when it's ok to send, instead of getting locked up in the DoUSBout routine.

    hth,
    DT

  7. #7

    Default

    Thanks Darrel after looking at your code i changed the code to look like the this, it works fine, i will again post some code if i get the VB code to send some data.
    Code:
    include "USB_ASM_Service.pbp"
    DEFINE LOADER_USED 1
    Define  OSC 48
    
    ' *****************************************************
    bufferRX	Var	Byte[8]
    bufferTX	Var	Byte[8]
    cntRX	Var	Byte
    cntRX = 8	' Specify input buffer size
    cntTX	Var	Byte
    cntTX = 8	' Specify output buffer size
    LED	Var	PORTD.7
    ' ****************************************************
    
    Main:
    	IF Plugged THEN		'indicate conection to PC
    		LED = 1
    	ELSE
    		LED = 0 
    	ENDIF
    
    	IF RX_READY THEN        ; USB data has been rcvd
            RX_READY = 0        ; clear the Ready flag
            USBIN 1, BufferRX, CntRX, Main
        ENDIF
    
    	IF TX_READY THEN         ; USB is Ready to transmit
            TX_READY = 0         ; clear the Ready flag
            USBOUT 1, BufferTX, CntTX, TXfail
          TXfail:
        ENDIF
    
    	bufferTX[0] = 1
    	bufferTX[1] = 2
    	bufferTX[2] = 3
    	bufferTX[3] = 4
    	bufferTX[4] = 5
    	bufferTX[5] = 6
    	bufferTX[6] = 7
    	bufferTX[7] = 8
    
    	Goto Main
    Last edited by Andre_Pretorius; - 26th June 2010 at 15:22. Reason: Found error

  8. #8

    Default

    Hi there, i am back still batteling with the VB.net part of my project, i have tried this code to send data to my pic when i press a button, it does not work, i know this is not a vb forum but mabe someone can give me some advice
    thank you

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pHandle As Integer
            pHandle = hidGetHandle(VendorID, ProductID)
            BufferOut(0) = 1
            BufferOut(1) = 2
            BufferOut(2) = 3
            BufferOut(3) = 4
            BufferOut(4) = 5
            BufferOut(5) = 6
            BufferOut(6) = 7
            BufferOut(7) = 8
            hidWrite(pHandle, BufferOut(0))
        End Sub

  9. #9
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637

    Default

    Andre,

    I created my application using EasyHID. The code is in VB6 and it works fine. If you think that it might help you, I will post the VB6 code here.

    The VB code in your first post has the "plugged", "unplugged", and "OnRead" subroutines, but I don't see the "WriteSomeData" routine that exists in the VB6 version. What program did you use to get this code? Was it HIDMaker?

    Robert

  10. #10
    Join Date
    Oct 2004
    Posts
    448

    Default

    Robert, I would love to see your VB code; like I said earlier, I too managed to send data from the pic to the PC, but the reverse is just not happening.

    Regards,

    Anand

  11. #11
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637

    Default

    The following VB6 code is created by EasyHID. It is a subroutine to send bytes from the computer to the MCU. You need to comment out the BufferOut(0) and BufferOut(1) lines like I did below.

    Code:
    '*****************************************************************
    ' 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
    All you need to do is give a value to variables BufferOut(1) to BufferOut(8) and call the WriteSomeData routine when the command button is pressed. Then, on the PIC side try to read bytes USBBuffer_IN[0] to USBBuffer_IN[7] by using the DoUSBIn routine.

    Code:
    Private Sub Command1_btn_Click()
    
    BufferOut(1) = 1
    BufferOut(2) = 2
    BufferOut(3) = 3
    BufferOut(4) = 4
    BufferOut(5) = 5
    BufferOut(6) = 6
    BufferOut(7) = 7
    BufferOut(8) = 8    
    
    WriteSomeData  'Calls routine WriteSomeData
        
    End Sub
    Robert

  12. #12
    Join Date
    Oct 2004
    Posts
    448

    Default

    Robert, thanks a lot. I'll try this out soon and be back with a feedback.

    Regards,

    Anand

  13. #13

    Default

    Thank you it works like a charm, now i can continue with the rest of my program, thank you once again

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