Announcement

Collapse
No announcement yet.

Help with .net code for FB

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with .net code for FB

    Using the .Net 2005 Studio and the code that I copied from Arrow, here) I can use the FUSB_Initialize command and I get a unique pointer, I can use the FUSB_Free command and I get a 0 if the device was opened, and a 50 if I click it again. So I know that I am talking to the device. But whenever I send the "USBIO.FUSB_Send(Uint0, OutChunks)" the program just hangs and I have to force it close. If I use FCC.exe I can turn on and off the DigOut ports, the board it tested OK. I'm at work right now so I cant post the code, but it is the same as Arrow's (thank you Arrow. I am using VS 2005 Sp1, XP Pro Sp2. I am a newb to .net, but I figured this is a great opportunity to learn
    06 Galaxy Gray Si #0308
    CarPc Progress -
    10%---------50%---------100%
    -[XXXXXXXXXXXXXXXXXXXXXX]-

  • #2
    Sounds like you are able to establish a pointer, but not actually communicate properly with the board. There is one more step that was not included in my code. Maybe you are missing that. You need to have another routine somewhere that sets each of the onOffBit variables to either 1 or 0 and then calls SendDataBlock(). Maybe without that, the onOffBits are not the correct values and the Brain or drivers don't know what to do.
    Christmas Car Project 2008

    ArrowDash Skin Worklog

    Comment


    • #3
      This should look familiar. Per your advice, on FormLoad I set all the values of OnOffBit(j) to 0. Still the same result. I've attached the code, hoping that you could spot my error. Thanks for your help

      Code:
          
          Dim FlipFlop As Boolean = True    'Keep-alive for brain
          Dim OutChunks(64) As Byte   'Bytes for 64bit data chunk being sent to brain
          Dim OnOffBit(12) As Integer 'Bit to turn on or off an output
          Dim Uint0 As UInt32 = Convert.ToUInt32(0)   'Uint representing zero
          Dim VIDPID As String = "USB\VID_04D8&PID_000C\5&2C65B59D&0&1"
      Code:
        
          Public Sub SendDataBlock()
      
              If Uint0 = 0 Then      'only Init once
                  If USBIO.FUSB_Initialize(Uint0, VIDPID, Uint0) Then  'Initialize Device
                      Debug.WriteLine("Initialized: " & Convert.ToString(Uint0))
                  Else
                      Debug.WriteLine("Error Initializing: " & Convert.ToString(Uint0))
                  End If
              Else
                  Debug.WriteLine("Already Initializing: " & Convert.ToString(Uint0))
              End If
      
              'Clear ByteBlock
              Dim cnt As Integer
              For cnt = 0 To 63
                  OutChunks(cnt) = 0
              Next
      
              'Convert FlipFlop Boolean to an integer
              Dim KeepAlive As Integer = Convert.ToInt32(FlipFlop)
              Dim i As Integer
              For i = 0 To 11
                  OutChunks(i) = Convert.ToByte(0 & KeepAlive & OnOffBit(i))
              Next
              OutChunks(61) = Convert.ToByte(255)     'wake up PIC
      
              USBIO.FUSB_Send(Uint0, OutChunks)
      
              FlipFlop = Not FlipFlop  'Flip keepalive bit
      
          End Sub
      
      
          Private Sub btn0on_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0on.Click
              OnOffBit(0) = 1 'Turn Output on
              SendDataBlock()
          End Sub
      
          Private Sub btn0off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0off.Click
              OnOffBit(0) = 0 'Turn Output off
              SendDataBlock()
          End Sub
      
          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              Dim j As Integer
              For j = 0 To 11 'need to fill a value for all dig outs
                  OnOffBit(j) = 0
              Next
          End Sub
      
          Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
              USBIO.FUSB_Free(Uint0)
          End Sub
      06 Galaxy Gray Si #0308
      CarPc Progress -
      10%---------50%---------100%
      -[XXXXXXXXXXXXXXXXXXXXXX]-

      Comment


      • #4
        It hangs because that DLL is borked. The second (and often first) call to FUSB_Send will hang, as you've said. All will work provided you first do a read first. Waste of time but at least it works. So before you start setting your On/Off bits on the array do a read and all will be ok. One additional cosmetic detail is that your array is actually 65 elements long. Won't hurt anything, just not tidy. Apparently in VB you declare last index of the array not length.

        HTH
        Also, I still don't know what that deal with this flippity floppy thing is, just set your keep-alive bit when you want the output on (together with bit 0 set to 1) and don't set it when turning output off. Otherwise if you get your order switched somehow and don't set the keepalive bit when the button is supposed to stay on, it won't.

        Comment


        • #5
          AtomicMonkey - That was it, if I do a USBIO.FUSB_Receive first and all the USBIO.FUSB_Send commands complete without any problems. Thanks for the tip on the keep-alive. Its a good thing to able to talk to the brain. Thanks
          06 Galaxy Gray Si #0308
          CarPc Progress -
          10%---------50%---------100%
          -[XXXXXXXXXXXXXXXXXXXXXX]-

          Comment


          • #6
            It hangs because that DLL is borked.
            Best line ever.
            2006 Lancer Evolution IX MR In-Dash PC Project - WIP

            Planning:
            [----------] 100%
            Purchasing:
            [----------] 90%
            Installation/Fab/Assembly (Revised v2):
            [----------] 90%

            Comment


            • #7
              Hmm... I flippity floppity that bit all day long and my stuff works like a charm.
              Christmas Car Project 2008

              ArrowDash Skin Worklog

              Comment


              • #8
                Actually, If i don't flippity floppity that bit, the brain works like it's supposed to and shuts itself off.
                Christmas Car Project 2008

                ArrowDash Skin Worklog

                Comment


                • #9
                  Originally posted by Arrow View Post
                  Actually, If i don't flippity floppity that bit, the brain works like it's supposed to and shuts itself off.


                  That is the supposed function of the Fusion Brain.
                  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

                  Comment


                  • #10
                    Toaster I realize that is the function, what I'm saying this flipping does not appear necessary for the the function to function. The timer will shut the output off with or without the keep-alive set (matter of timing), though w/o it timer will not reset on subsequent same state write, but if always sending 3 + timer for ON and 0 + timer for OFF does the same exact thing: output will shut off after timer expired with no state confirmation sent.
                    Ok, my apologies for spreading crap information. Appears the timer in fact will not reset if u sent same value before it expires.

                    Comment


                    • #11
                      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.

                      Comment


                      • #12
                        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

                        Comment


                        • #13
                          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

                          Comment


                          • #14
                            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

                            Comment


                            • #15
                              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

                              Comment

                              Working...
                              X