PIC USB to VB.NET


Closed Thread
Results 1 to 40 of 54

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default PIC USB to VB.NET

    Hi everyone;
    Some time ago, i search this forum for a program in VB.net to comunicate with the USB program that mister_E and Darrel posted in this Thread ( http://www.picbasic.co.uk/forum/show...0434#post80434 ), however i could not found any source code in VB.Net, only in VB6 made by mister_e, so i decided to do it myself.

    I used the PBP code from Darrel Tayler, and converted the mister_e VB6 program to VB.Net.

    Please note that all the credits should go to Darrel Tayler and mister_e, i just convert the program to VB.Net, nothing more!

    Here is the PIC16F4550 code;
    Code:
    '***************************************************************************
    '*  Name    : USB.pbp                                                      *
    '*  Author  : Darrel Taylor, changed by Gadelhas                           *
    '*  Date    : 23-01-2011                                                   *
    '*  Version : 1.0                                                          *
    '*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID       *
    '*          : Meant to work with mister-e's GUI                            *
    '***************************************************************************
    ;--- if you un-comment these, you must comment the ones in the .inc file ---
    ASM  ; 18F2550/4550, 8mhz crystal
       __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
       __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
       __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
       __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
       __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    DEFINE OSC 48
    CLEAR
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"     ; Base Interrupt System
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
            INT_Handler   USB_Handler
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    ;--- Setup USB -------------------------------------------------------------
    INCLUDE "DT_HID260.pbp"
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Hugo Oliveira"
    DEFINE USB_PRODUCTNAME "Demo USB"
    DEFINE USB_SERIAL      "001Hugo"
    DEFINE USB_INSIZE      8    ;  IN report is PIC to PC (8,16,32,64)
    DEFINE USB_OUTSIZE     8    ; OUT report is PC to PIC
    DEFINE USB_POLLIN      10   ; Polling times in mS, MIN=1 MAX=10
    DEFINE USB_POLLOUT     10
    ; --- Each USB status LED is optional, comment them if not used ------------
    ; --- They can be assigned to any pin, and no further action is required --- 
    DEFINE USB_LEDPOLARITY 1       ; LED ON State [0 or 1]  (default = 1)
    ;DEFINE USB_PLUGGEDLED  PORTB,7 ; LED indicates if USB is connected
    ;DEFINE USB_TXLED       PORTB,6 ;  "      "     data being sent to PC
    ;DEFINE USB_RXLED       PORTB,5 ;  "      "     data being received from PC
     
    ;--- Variables -------------------------------------------------------------
    Value0      VAR  WORD
    Value1      VAR  WORD
    X           VAR  WORD
    DUTY1       VAR  WORD
    DUTY2       VAR  WORD
    Old_PORTA   VAR  BYTE
    New_PORTA   VAR  BYTE
    ;--- Setup ADC -------------------------------------------------------------
    DEFINE ADC_BITS 8  ; Number of bits in ADCIN result
    ;--- Initialize ------------------------------------------------------------
    CCPR1L = 0
    CCPR2L = 0
    CCP1CON =   %00001100       ' CCP1, PWM mode
    CCP2CON =   %00001100       ' CCP2, PWM mode
    PR2     =   249             ' 0-1000 duty range
    T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
    TRISB = 0
    OUTPUT PORTC.1
    OUTPUT PORTC.2
     
    ADCON2.7 = 0       ; left justify    (Change this if ADFM in diff register)
    ADCON1 = %1101     ; AN0/AN1 Analog
    ;--- The Main Loop ---------------------------------------------------------
    Main:
        FOR X = 0 to 1000           ; Check for incomming USB data while waiting
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
     
            New_PORTA = PORTA                ; Check PORTA pins
            IF New_PORTA != Old_PORTA THEN
                Old_PORTA = New_PORTA
                ARRAYWRITE USBTXBuffer, ["HugoDem1"]
                GOSUB WaitToSend
                ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                         PORTA.3, PORTA.4, PORTA.5,0,0]
                GOSUB WaitToSend
            ENDIF
        NEXT X
     
        ADCIN 0, Value0            ; Send A/D about once/sec
        ADCIN 1, Value1
        ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                 PORTA.3, PORTA.4, PORTA.5,0,0]
        GOSUB SendIfReady
    GOTO Main
    ;--- Send Data if Plugged and ready, otherwise discard ---------------------
    SendIfReady:
        IF Plugged AND TX_READY THEN DoUSBOut
    RETURN
    ;--- Wait till Ready to send of until unplugged ----------------------------
    WaitToSend:
        WHILE Plugged and !TX_READY : WEND
        IF TX_READY THEN GOSUB DoUSBOut
    RETURN
    ;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
    HandleRX:
        ARRAYREAD  USBRXBuffer,[PORTB, DUTY1.LowByte, DUTY1.HighByte, _
                                        DUTY2.LowByte, DUTY2.HighByte]
        CCP1CON.5=DUTY1.1       ' load CCP1 duty value            
        CCP1CON.4=DUTY1.0       '      with Duty1 
        CCPR1L=DUTY1>>2         '
     
        CCP2CON.5=DUTY2.1       ' load CCP2 duty value            
        CCP2CON.4=DUTY2.0       '      with Duty2
        CCPR2L=DUTY2>>2         '
    return
    Attached is the code for PIC18F4550
    I the next post is the VB.net Program
    Hope it can help someone!
    Attached Files Attached Files
    Thanks and Regards;
    Gadelhas

  2. #2
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default VB.NET Program

    Here is the VB Program;

    The image of the program;

    Name:  VB.JPG
Views: 18931
Size:  39.9 KB
    And here is the code;

    Code:
    PublicClass Form1
    ' vendor and product IDs
    PrivateConst VendorID AsShort = 6017 'Replace with your device's
    PrivateConst ProductID AsShort = 2000 'product and vendor IDs
     
    ' read and write buffers
    PrivateConst BufferInSize AsShort = 8 'Size of the data buffer coming IN to the PC
    PrivateConst BufferOutSize AsShort = 8 'Size of the data buffer going OUT from the PC
    Dim BufferIn(BufferInSize) AsByte'Received data will be stored here - the first byte in the array is unused
    Dim BufferOut(BufferOutSize) AsByte'Transmitted data is stored here - the first item in the array must be 0
    Dim PORTB(8) AsInteger
    ' ****************************************************************
    ' when the form loads, connect to the HID controller - pass
    ' the form window handle so that you can receive notification
    ' events...
    '*****************************************************************
    PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
    ' do not remove!
    ConnectToHID(Me)
    Pictureoff.Visible = True
    Pictureon.Visible = False
    TrackBar1.Enabled = False
    TrackBar2.Enabled = False
    Label8.Text = "AN0"
    Label8.Text = "AN1"
    Label10.Text = "CCP1"
    Label11.Text = "CCP2"
    PictureBox1.Visible = False
    PictureBox3.Visible = False
    PictureBox5.Visible = False
    PictureBox7.Visible = False
    CheckBox1.Enabled = False
    CheckBox2.Enabled = False
    CheckBox3.Enabled = False
    CheckBox4.Enabled = False
    CheckBox5.Enabled = False
    CheckBox6.Enabled = False
    CheckBox7.Enabled = False
    CheckBox8.Enabled = False
    StatusLB2.Text = "Dispositivo USB Desconectado"
    EndSub
    '*****************************************************************
    ' disconnect from the HID controller...
    '*****************************************************************
    PrivateSub Form1_FormClosed(ByVal sender AsObject, ByVal e As System.Windows.Forms.FormClosedEventArgs) HandlesMe.FormClosed
    Splash.Close()
    DisconnectFromHID()
    EndSub
    '*****************************************************************
    ' a HID device has been plugged in...
    '*****************************************************************
    PublicSub OnPlugged(ByVal pHandle AsInteger)
    Dim VendorName AsString = "00000000000000000000"
    Dim serial AsString = "00000000000000000000"
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    ' ** YOUR CODE HERE **)
    hidGetVendorName(pHandle, VendorName, 20)
    hidGetSerialNumber(pHandle, serial, 20)
    Label1.Text = "Vendor Name: " & VendorName
    Label2.Text = "Product Name: " & ProductName
    Label3.Text = "Vendor ID: " & VendorID
    Label4.Text = "Product ID: " & ProductID
    Label5.Text = "Serial: " & serial
    Pictureoff.Visible = False
    Pictureon.Visible = True
    TrackBar1.Enabled = True
    TrackBar2.Enabled = True
    Label8.Text = Val(BufferIn(1))
    Label8.Text = Val(BufferIn(2))
    Label10.Text = TrackBar1.Value
    Label11.Text = TrackBar2.Value
    CheckBox1.Enabled = True
    CheckBox2.Enabled = True
    CheckBox3.Enabled = True
    CheckBox4.Enabled = True
    CheckBox5.Enabled = True
    CheckBox6.Enabled = True
    CheckBox7.Enabled = True
    CheckBox8.Enabled = True
    StatusLB2.Text = "Dispositivo USB Conectado"
    EndIf
    EndSub
    '*****************************************************************
    ' a HID device has been unplugged...
    '*****************************************************************
    PublicSub OnUnplugged(ByVal pHandle AsInteger)
    If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
    ' ** YOUR CODE HERE **
    Pictureoff.Visible = True
    Pictureon.Visible = False
    TrackBar1.Enabled = False
    TrackBar2.Enabled = False
    Label8.Text = "AN0"
    Label8.Text = "AN1"
    Label10.Text = "CCP1"
    Label11.Text = "CCP2"
    PictureBox1.Visible = False
    PictureBox3.Visible = False
    PictureBox5.Visible = False
    PictureBox7.Visible = False
    CheckBox1.Enabled = False
    CheckBox2.Enabled = False
    CheckBox3.Enabled = False
    CheckBox4.Enabled = False
    CheckBox5.Enabled = False
    CheckBox6.Enabled = False
    CheckBox7.Enabled = False
    CheckBox8.Enabled = False
    StatusLB2.Text = "Dispositivo USB Desconectado"
    EndIf
    EndSub
    '*****************************************************************
    ' controller changed notification - called
    ' after ALL HID devices are plugged or unplugged
    '*****************************************************************
    PublicSub 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 AsInteger
    pHandle = hidGetHandle(VendorID, ProductID)
    hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
    EndSub
    '*****************************************************************
    ' on read event...
    '*****************************************************************
    PublicSub OnRead(ByVal pHandle AsInteger)
    ' read the data (don't forget, pass the whole array)...
    Dim index AsByte
    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...
    If BufferIn(7) = 0 And BufferIn(8) = 0 Then
    '
    ' incomming ADC reading and PORTA status
    ' --------------------------------------
    ProgressBar1.Value = BufferIn(1) 'Display ADC results to
    ProgressBar2.Value = BufferIn(2) 'the progress bars
    '
    Label8.Text = Val(BufferIn(1)) 'Display Progress bars
    Label9.Text = Val(BufferIn(2)) 'Values
    '
    PictureBox1.Visible = BufferIn(3) 'Display PORTA
    PictureBox3.Visible = BufferIn(4) 'status to
    PictureBox5.Visible = BufferIn(5) 'the according
    PictureBox7.Visible = BufferIn(6) 'LEDs
    Else
    '
    ' Incomming data is a text string
    ' -------------------------------
    For index = 1 To BufferInSize ' pass the whole array but skip (0)
    If BufferIn(index) <> 0 Then' valid data?
    txtreception.AppendText(Chr(BufferIn(index)))
    EndIf
    Next
    txtreception.Text += vbNewLine
    txtreception.Select(txtreception.Text.Length - 1, 0)
    txtreception.ScrollToCaret()
    EndIf
     
    EndIf
    EndSub
    PublicSub WriteSomeData()
    ' Use to send data to the USB bus.
    ' data must be store in BufferOut array
    '
    ' Actual structure:
    ' -----------------
    ' BufferOut(0) = Report id => always 0
    ' BufferOut(1) = PORTB ' first data item, etc etc
    ' BufferOut(2) = HPWM slider1 MSB
    ' BufferOut(3) = HPWM slider1 LSB
    ' BufferOut(4) = HPWM slider2 MSB
    ' BufferOut(5) = HPWM slider2 LSB
    ' BufferOut(6) = n/a = 0
    ' BufferOut(7) = n/a = 0
    ' BufferOut(8) = n/a = 0
    BufferOut(0) = 0 ' first by is always the report ID
    BufferOut(6) = 0
    BufferOut(7) = 0
    BufferOut(8) = 0
    hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
    EndSub
    
    PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    txtreception.Clear()
    EndSub
    PrivateSub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
    BufferOut(2) = (TrackBar1.Value) And 255 ' keep only 8 LSB
    BufferOut(3) = Int(TrackBar1.Value / 256) ' keep only 8 MSB
    Label10.Text = TrackBar1.Value ' display it
    WriteSomeData() ' send it
    EndSub
    PrivateSub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
    BufferOut(4) = (TrackBar2.Value) And 255 ' keep only 8 LSB
    BufferOut(5) = Int(TrackBar2.Value / 256) ' keep only 8 MSB
    Label11.Text = TrackBar2.Value ' display it
    WriteSomeData() ' send it
    EndSub
    PublicSub ActualizaPORTAB()
    Dim i AsInteger
    BufferOut(1) = 0
    If CheckBox1.Checked = TrueThen
    PORTB(0) = 1
    Else
    PORTB(0) = 0
    EndIf
    If CheckBox2.Checked = TrueThen
    PORTB(1) = 1
    Else
    PORTB(1) = 0
    EndIf
    If CheckBox3.Checked = TrueThen
    PORTB(2) = 1
    Else
    PORTB(2) = 0
    EndIf
    If CheckBox4.Checked = TrueThen
    PORTB(3) = 1
    Else
    PORTB(3) = 0
    EndIf
    If CheckBox5.Checked = TrueThen
    PORTB(4) = 1
    Else
    PORTB(4) = 0
    EndIf
    If CheckBox6.Checked = TrueThen
    PORTB(5) = 1
    Else
    PORTB(5) = 0
    EndIf
    If CheckBox7.Checked = TrueThen
    PORTB(6) = 1
    Else
    PORTB(6) = 0
    EndIf
    If CheckBox8.Checked = TrueThen
    PORTB(7) = 1
    Else
    PORTB(7) = 0
    EndIf
    For i = 0 To 7
    BufferOut(1) = BufferOut(1) + (PORTB(i) * (2 ^ i))
    Next
    WriteSomeData()
    EndSub
    PrivateSub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
    ActualizaPORTAB()
    EndSub
    PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    StatusLB3.Text = DateAndTime.Now.ToString
    EndSub
    EndClass
    
    Attached is the hole Project in VB.Net 2008
    Attached Files Attached Files
    Thanks and Regards;
    Gadelhas

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

    Default Re: PIC USB to VB.NET

    Nice work, will be useful for many, that's for sure!

    I've already posted a snip for vb.net here awhile back. Amr Nekhitdid a VB.NET template based on my work
    http://helmpcb.com/software/usb-hid-...ual-basic-2005
    Steve

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

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    Nice work, will be useful for many, that's for sure!

    I've already posted a snip for vb.net here awhile back. Amr Nekhitdid a VB.NET template based on my work
    http://helmpcb.com/software/usb-hid-...ual-basic-2005
    Thank you mister_e

    I see that post, when i was searching, however since it was made in VB2005, i decided to this this one in VB2008, not to much but some things change.
    Thanks and Regards;
    Gadelhas

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

    Default Re: PIC USB to VB.NET

    Time goes by way too fast
    Steve

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

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: PIC USB to VB.NET

    Quote Originally Posted by mister_e View Post
    Time goes by way too fast
    You are right... Now i have to do it in VB2010.... or perhaps in one future new version, like VB2012........ hummm i don't think so!!!
    Thanks and Regards;
    Gadelhas

  7. #7

    Default Re: VB.NET Program

    Thanks for posting Gadelhas,

    Been wanting to try USB for a long time. But it seems everything changes rapidly. Windows, Visual Studio, .net etc. This was a popular subject a few years back. So most of the examples seem to be VB6 or in this example VS 2008

    Can anyone tell me if the code in posts 1 and 2 can be used with Visual Studio Community 2015? I was able to download VS 2008 sp1 ISO, which may be good enough?

    Also can't seem to find where to download EasyHID, maybe its out of date. HIDmaker FS looks great, but $399 is hard to justify for casual use.

    Thanks
    Mark

  8. #8
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: VB.NET Program

    Quote Originally Posted by mark_s View Post
    Thanks for posting Gadelhas,

    Been wanting to try USB for a long time. But it seems everything changes rapidly. Windows, Visual Studio, .net etc. This was a popular subject a few years back. So most of the examples seem to be VB6 or in this example VS 2008

    Can anyone tell me if the code in posts 1 and 2 can be used with Visual Studio Community 2015? I was able to download VS 2008 sp1 ISO, which may be good enough?

    Also can't seem to find where to download EasyHID, maybe its out of date. HIDmaker FS looks great, but $399 is hard to justify for casual use.

    Thanks
    Mark
    It's been a long time since i don't use this project. It was made in VB.net 2008, so it should be easy to use with VS 2015.

    At his moment i really don't have the time to port the code to VS2015, lost of projects at the same time, but i'll put it on my list.

    I not even know if EasyHID still exists, but i never used it, its to expensive.

    Also let me know what are you trying to do with USB.
    Thanks and Regards;
    Gadelhas

  9. #9

    Default Re: VB.NET Program

    Thanks for answering. I don't have a specific project in mind, just looking for a templet to build on. Your example is perfect, a small app written in VB to read and write to a pic's ports and modules.

    I worry a little, starting projects based on VB6, mister-e and DT's great work. Some of this material is 10 years old now and they are no longer here to answer or modify these routines.

    Found this site which sells a DLL for Visual Studio that looks very interesting. It can be used with VS 2015 and examples in MikroC. I have Mikrobasic so this may be the best path. The only reason I have not purchased it, the examples and videos are not in English, so I need a little more time to study.

    http://www.usbhidnetclass.com/

    Regards
    Mark

  10. #10
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Re: VB.NET Program

    Quote Originally Posted by mark_s View Post
    Thanks for answering. I don't have a specific project in mind, just looking for a templet to build on. Your example is perfect, a small app written in VB to read and write to a pic's ports and modules.

    I worry a little, starting projects based on VB6, mister-e and DT's great work. Some of this material is 10 years old now and they are no longer here to answer or modify these routines.

    Found this site which sells a DLL for Visual Studio that looks very interesting. It can be used with VS 2015 and examples in MikroC. I have Mikrobasic so this may be the best path. The only reason I have not purchased it, the examples and videos are not in English, so I need a little more time to study.

    http://www.usbhidnetclass.com/

    Regards
    Mark
    Hi mark_s;

    I'm aware of that dll, but i didn't use it.
    You can probably use this to try out, but only in C and C#
    http://libstock.mikroe.com/projects/...on-ad-and-leds
    http://libstock.mikroe.com/projects/...oe-development

    Nowadays i only program in C ( embedded) or C#/python/Java (desktop app).
    I few months ago i did an application with MicroC and PIC18F4550 to emulate a HID Keyboard. In this way you don't even need the desktop app.
    The worst part is the descriptor, but after that it works great.

    What language you usually work? PicBasic, MikroBasic, Visual Basica, C#?
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 2

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