hi, i made a vb.net project thant make connection between PC and PIC18F2550 using usb temple (USB HID) when i run it i have this message "badimage exception". I use windows 7 64 bits .
please help me
this is the code.

Attachments
Public Class Form1
    ' vendor and product IDs
    Private Const VendorID As Integer = &H1234    'Replace with your device's
    Private Const ProductID As Integer = &H1      'product and vendor IDs

    ' read and write buffers
    Private Const BufferInSize As Integer = 64 'Size of the data buffer coming IN to the PC
    Private Const BufferOutSize As Integer = 65    'Size of the data buffer going OUT from the PC
    Dim BufferIn(BufferInSize) As Byte          'Received data will be stored here - the first byte in the array is unused
    Dim BufferOut(BufferOutSize) As Byte    'Transmitted data is stored here - the first item in the array must be 0

    Public Function AsciiByteToChar(ByVal b As Byte) As Char

        Dim barr() As Byte = New Byte() {b}

        Dim carr() As Char = Encoding.ASCII.GetChars(barr)

        Return carr(0)

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectToHID(Me)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e 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
            MsgBox("Device connected", vbOKOnly, "USB")
        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)
            MsgBox("Device unplugged", vbOKOnly, "USB")
        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)
        Dim S As String
        If hidRead(pHandle, BufferIn(0)) Then
            For i = 1 To 64
                S += AsciiByteToChar(BufferIn(i))
            Next
            TextBox2.Text = S
            End If
    End Sub

    Public Shared Function StrToByteArray(ByVal str As String) As Byte()
        Dim encoding As New System.Text.UTF8Encoding()
        Return encoding.GetBytes(str)
    End Function 'StrToByteArray

    Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
        Dim i = 0
        BufferOut(0) = 0
        Dim k = TextBox1.Text.Length
        Dim datatosend As Byte() = StrToByteArray(TextBox1.Text)
        For i = 0 To k - 1
            BufferOut(i + 1) = datatosend(i)
        Next
        hidWriteEx(VendorID, ProductID, BufferOut(0))
    End Sub
End Class

Welcome to DaniWeb. It's ok to put your code directly into your post. Use the Code option in the edit window menu.

In addition to the code we also need the exact wording of the whole exception and the line in the code that triggered the exception.

Public Class Form1
    ' vendor and product IDs
    Private Const VendorID As Integer = &H1234    'Replace with your device's
    Private Const ProductID As Integer = &H1      'product and vendor IDs

    ' read and write buffers
    Private Const BufferInSize As Integer = 64 'Size of the data buffer coming IN to the PC
    Private Const BufferOutSize As Integer = 65    'Size of the data buffer going OUT from the PC
    Dim BufferIn(BufferInSize) As Byte          'Received data will be stored here - the first byte in the array is unused
    Dim BufferOut(BufferOutSize) As Byte    'Transmitted data is stored here - the first item in the array must be 0

    Public Function AsciiByteToChar(ByVal b As Byte) As Char

        Dim barr() As Byte = New Byte() {b}

        Dim carr() As Char = Encoding.ASCII.GetChars(barr)

        Return carr(0)

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectToHID(Me)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e 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
            MsgBox("Device connected", vbOKOnly, "USB")
        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)
            MsgBox("Device unplugged", vbOKOnly, "USB")
        End If
    End Sub

BadImageException

In addition to the code we also need the exact wording of the whole exception and the line in the code that triggered the exception.

Error:" Une exception de première chance de type 'System.BadImageFormatException' s'est produite dans USBTemplate21.exe" without line

@ tinstaafl, please i need your help when i connect the microcontroller to usb port i listent the sound of connection but the vb.net interface does not work. can you help me

The exception seems to be because the library you're using is incompatible with the visual studio you're using. You'll have to check the source of the library and see if there is a better version for you.

Here's the documentation on using usb devices with .net

@ tinsttafl i cant find any solution in that links , what about vb.net 2008? i can try whith it or i have a mistake in the syntaxe?!!

This article has been dead for over six months. Start a new discussion instead.