Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Help with .net code for FB

  1. #11
    Newbie
    Join Date
    Mar 2008
    Posts
    17

    Post Your Code

    Would someone who has been able to get this working through VB.net be willing to post their entire code? By entire I just mean all of the pieces that are needed to turn outputs on and off.

    I've been pulling my hair out for hours now but my app is still hanging. I know it's just a small stupid error that I've made but for the life of me I cannot find where it is.

    Thanks.

  2. #12
    Constant Bitrate mx270a's Avatar
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    167
    Here is the code I'm using in VB2008 Express.

    Code:
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_Initialize(ByRef pFusionUSB As UInt32, ByVal deviceID As String, ByVal instanceNumber As UInt32) As Boolean
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_Receive(ByVal pFusionUSB As UInt32, ByVal buffer As Byte()) As Boolean
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_Send(ByVal pFusionUSB As UInt32, ByVal buffer As Byte()) As Boolean
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_Free(ByRef pFusionUSB As UInt32) As UInt32
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_printf(ByVal pFusionUSB As UInt32) As Boolean
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_GetWinUSBerror(ByVal pFusionUSB As UInt32) As UInt32
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_GetFUSBstatus(ByVal pFusionUSB As UInt32) As UInt32
        End Function
        <System.Runtime.InteropServices.DllImport("FusionUSB.dll")> _
        Public Shared Function FUSB_GetDeviceInstanceID(ByVal pFusionUSB As UInt32) As String
        End Function
    
    
        Public VIDPID As String = ""
        Dim Uint0 As UInt32 = Convert.ToUInt32(0)   'Uint representing zero
        Private KeepAliveBit As Integer
        Private OutputArray(64) As Byte
        Private InputArray(64) As Byte
        Private DigOut(12) As Integer
        Public AnalogIn(10) As Integer
    
    
    
        Public Sub LoadVIDPIDfromRegistry(ByRef textbox As TextBox)
            Dim location As String = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinUSB\Enum"
            Dim storedvidpid As String = My.Computer.Registry.GetValue(location, "0", Nothing)
            If storedvidpid = Nothing Then
                MsgBox("VID/PID for device #0 not found in the registry. Are the Fusion Brain drivers loaded?")
            Else
                MsgBox("The following VID/PID was found:" & vbCrLf & vbCrLf & storedvidpid)
                VIDPID = storedvidpid
            End If
        End Sub
    
    
    
    
    
        Public Sub FB_Connect()
            'Check the status to see if it is already connected
            Try
                If FUSB_GetFUSBstatus(Uint0) = 0 Then
                    'Status is 0, meaning it is already connected. Can't connect it again.
                Else
                    'Status isn't 0, lets try to connect to the Fusion Brain
                    FUSB_Free(Uint0)
    
                    If FUSB_Initialize(Uint0, VIDPID, Uint0) Then
                        'Init was successful
                        For i = 0 To 63
                            OutputArray(i) = CByte(0)
                        Next
                        OutputArray(61) = CByte(255) 'This is needed to wake up the FB, for firmwares after November 2007
                        For i = 0 To 11
                            DigOut(i) = 0
                        Next
                        FUSB_Send(Uint0, OutputArray)
                        If (FUSB_Receive(Uint0, InputArray)) Then
                            lblFirmwareVersion.Text = InputArray(63).ToString & "." & InputArray(62).ToString
                        End If
    
                        KeepAliveBit = 1
    
                        'Start the timer for sending data to the FB
                        Timer.Start()
                    Else
                        MsgBox("Failed to initialize that Fusion Brain. Make sure it is connected and that you are specifying the correct USB VID/PID number." & vbCrLf & vbCrLf & "The VID/PID specified was: " & VIDPID)
                    End If
                End If
    
            Catch ex As Exception
                MsgBox("Error: DLL not found. Are you sure the fusion brain drivers are installed and the FusionUSB.dll file is in the same folder as the .exe?" & vbCrLf & vbCrLf & "The reported error is:" & vbCrLf & ex.ToString)
            End Try
        End Sub
    
    
    
    
        Public Sub FB_Disconnect()
            'This gets called from the MyBase.FormClosed event as the app closes
            'Disable timer, Set all outputs to off, send data to the FB one last time, then disconnect from the FB
            Timer.Stop()
    
            For i = 0 To 11
                DigOut(i) = 0
                OutputArray(i) = Convert.ToByte(0 & KeepAliveBit & 0)
            Next
            FUSB_Send(Uint0, OutputArray)
            FUSB_Free(Uint0)
        End Sub
    
    
    
    
    
        Public Sub FB_SendReceive()
            'This sub is called from a Timer_Tick event, which runs at 10Hz.
           
            DigOut(0) = 1
            DigOut(1) = 0
            DigOut(2) = 0
            DigOut(3) = 0
            DigOut(4) = 0
            DigOut(5) = 0
            DigOut(6) = 0
            DigOut(7) = 0
            DigOut(8) = 0
            DigOut(9) = 0
            DigOut(10) = 0
            DigOut(11) = 0
    
    
            For i = 0 To 11
                OutputArray(i) = Convert.ToByte(0 & KeepAliveBit & DigOut(i))
            Next
            FUSB_Send(Uint0, OutputArray)
    
    
    
            'Now, get the info from the fusion brain
            If (FUSB_Receive(Uint0, InputArray)) Then
                'It retured true, meaning it got data. Process it.
                For i = 0 To 9
                    AnalogIn(i) = CInt(InputArray(i * 2 + 12)) 'Uses only the first byte 0-255 (8 bits)
                    'AnalogIn(i) = (CInt(InputArray(i * 2 + 12)) * 4) + CInt(InputArray(i * 2 + 13) / 64) 'Uses both bytes 0-1023 (10 bits)
    
                    'Range of 0-255, so divide by 255 to get a percent, then multiply by 5 to get volts.
                    'AnalogIn(i) = (totalin / 1023) * 5
                Next
            End If
    
    
            'Toggle the keepalive bit for nexttime.
            If KeepAliveBit = 0 Then
                KeepAliveBit = 1
            Else
                KeepAliveBit = 0
            End If
    
        End Sub

  3. #13
    Newbie
    Join Date
    Mar 2008
    Posts
    17
    Thanks MX! Very much appreciated.

    Even after using your code my app is still hanging. Can you please confirm that the code you posted above is all in a separate class (i.e. USBIO) which is referenced from your form (i.e. Form1)?

    Also, did you have to throw in some FUSB_Recieve calls on your form as per a previous post?

    Thanks again for your help.

    Joe

  4. #14
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    9,851
    Try unplugging the device, wait for the windows "duh-ding" and then plug it back in.

    If you call the DLL and do not exit it correctly, it will lock the device stream. So if you were debugging or something and didnt close it properly, you have to close it. Easiest way is to let Windows do it for you by making the device disappear to start fresh and plug it back in.

    It is the limitation of the FusionUSB.dll file. If anyone has ideas on how to improve the dll let me know as I am learning as Im going! But it works as long as you dont hang it.
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

  5. #15
    Newbie
    Join Date
    Mar 2008
    Posts
    17

    Finally...

    OK, finally got it going. Turns out that somewhere along the way I began sending a null value as the VIDPID. It makes me cry thinking about how many hours of my life I've spent chasing down stupid little errors like this... ::

    Thanks again everyone!

    Joe

  6. #16
    Constant Bitrate mx270a's Avatar
    Join Date
    Aug 2003
    Location
    Iowa
    Posts
    167
    Quote Originally Posted by 2k1Toaster View Post
    It is the limitation of the FusionUSB.dll file. If anyone has ideas on how to improve the dll let me know as I am learning as Im going! But it works as long as you dont hang it.
    Where can I find the source code for the DLL?

    -Lance

  7. #17
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    9,851
    Quote Originally Posted by mx270a View Post
    Where can I find the source code for the DLL?

    -Lance
    The source code of the DLL has not been public.

    It is basically a wrapper for Microsoft's winusb.dll to be used by managed code easily.

    But open source means better, so voila: http://www.fusioncontrolcentre.com/T...DLL_Source.rar

    I am pretty sure that was used to build the current DLL, but I have so many copies I cannot remember.
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

  8. #18
    Newbie
    Join Date
    Apr 2008
    Location
    Baghdad, Iraq/Fort Bragg, NC/Saginaw, MI
    Posts
    27
    2k1- I'm at work at the moment, so I can't poke through the source for the dll. But my seguestion would be to add a FUSB_Clear function, that clears any data pending read, and re-initializes the USB device.

    I'm not really farmilure with device management, as most of my device interface code has been ripped from random chunks of code I've found online; but I would guess that the USB bus has a reInitialize function of some sort, to close all active connections to a usb device, and reset it.

    My Servo controller dll works this way- it doesn't matter if I've got an app already using the port or not, any new instances just boot old instances off from the device, and take control of it. It also has a nice PLXUSBClear function, that "resets" the controller.. the reset function might be built into the firmware tho, because it resets inside of 10Hz... I don't think Windows can reinit a USB device inside of 10Hz..

    Thanks
    Mike

  9. #19
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    9,851
    Quote Originally Posted by conjur View Post
    2k1- I'm at work at the moment, so I can't poke through the source for the dll. But my seguestion would be to add a FUSB_Clear function, that clears any data pending read, and re-initializes the USB device.

    I'm not really farmilure with device management, as most of my device interface code has been ripped from random chunks of code I've found online; but I would guess that the USB bus has a reInitialize function of some sort, to close all active connections to a usb device, and reset it.

    My Servo controller dll works this way- it doesn't matter if I've got an app already using the port or not, any new instances just boot old instances off from the device, and take control of it. It also has a nice PLXUSBClear function, that "resets" the controller.. the reset function might be built into the firmware tho, because it resets inside of 10Hz... I don't think Windows can reinit a USB device inside of 10Hz..

    Thanks
    Mike
    Definately an interesting approach. I will see what I can find.
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

Page 2 of 2 FirstFirst 12

Similar Threads

  1. GPS/MapPoint VB Source Code
    By stevieg in forum Software & Software Development
    Replies: 7
    Last Post: 01-28-2011, 04:59 PM
  2. Weird Registration Code and Hardware Code problem
    By lookinco in forum StreetDeck
    Replies: 2
    Last Post: 09-30-2006, 03:25 AM
  3. Replies: 0
    Last Post: 06-11-2006, 06:49 PM
  4. How do I use the Base plugin sources ?
    By guino in forum RR FAQ
    Replies: 1
    Last Post: 04-24-2006, 04:43 PM
  5. .NET code for OBD-II ?
    By Jeep in forum Engine Management, OBD-II, Engine Diagnostics, etc.
    Replies: 6
    Last Post: 05-09-2005, 11:44 AM

Bookmarks

Posting Permissions

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