Announcement

Collapse
No announcement yet.

Interfacing custom software with the Fusion Brain

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

  • Interfacing custom software with the Fusion Brain

    The Fusion Brain interfaces with the computer using USB. To get the datastream to start flowing, you must initialize the device first, then you can send and receive data packets, and when you are done with the device you must free it.

    The data packets are 64 bytes in length, and information on the encoding of the byte stream can be found here: http://www.mp3car.com/vbulletin/docu...-protocol.html
    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

  • #2
    C# -- DLL Interface

    1.) Call FUSB_Initialize() --. pFusionUSB is the Uint32 pointer to the USB handle. It is 0 when not initialized. deviceID is the full string of the PID/VID including the hardware ID. instanceNumber should be 0. If it returns true, it was initialized successfully.

    2.) Call FUSB_Send() and FUSB_Receive(). FUSB_Send takes an array of 64 bytes. FUSB_Receive returns an array of bytes 64 long.

    3.) Once you are all done, call FUSB_Free() and pass in the pointer you used to initialize to free it. If it returns 0, then it was freed successfully.


    Code:
    public static class FusionUSB
            {
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern bool FUSB_Initialize(ref UInt32 pFusionUSB, String deviceID, UInt32 instanceNumber);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern bool FUSB_Receive(UInt32 pFusionUSB, byte[] buffer);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern bool FUSB_Send(UInt32 pFusionUSB, byte[] buffer);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern UInt32 FUSB_Free(ref UInt32 pFusionUSB);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern bool FUSB_printf(UInt32 pFusionUSB);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern UInt32 FUSB_GetWinUSBerror(UInt32 pFusionUSB);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern UInt32 FUSB_GetFUSBstatus(UInt32 pFusionUSB);
    
                [System.Runtime.InteropServices.DllImport("FusionUSB.dll")]
                public static extern String FUSB_GetDeviceInstanceID(UInt32 pFusionUSB);
    
            }
    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


    • #3
      VB -- DLL Interface Thanks to mx270a

      I converted the code you posted to VB. For future reference for anyone else, here is what that looks like:

      Code:
      Public Class UCSettingsIO
          <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
      
      
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              Dim thefbinstid As String = FUSB_GetDeviceInstanceID(0)
              MsgBox(thefbinstid)
          End Sub
      End Class
      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


      • #4
        Using FusionUSB with VC++

        Hi I'm having a problem getting the FusionUSB.dll working with my C++ project in VC++, I'm getting the linking error

        error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl FUSB_Initialize(long,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,long)" (__imp_?FUSB_Initialize@@[email protected]?$ [email protected]@std@@[email protected]@2@@std@@[email protected]) referenced in function _main

        Here is my code incase it might help.
        Code:
        #include <iostream>
        using std::cout;
        using std::cin;
        
        #include <windows.h>
        
        #include <stdio.h>
        
        #include <String>
        using std::string;
        
        
        //#define _STATIC_CPPLIB
        #include "FusionUSB.h"
        #pragma comment(lib,"FusionUSB.lib")
        
        _declspec( dllimport ) bool FUSB_Initialize(long pFusionUSB, string deviceID, long instanceNumber);
        
        int main()
        {
        	
        
        	cout << "Hello World";
        	
        	if(FUSB_Initialize( NULL,"",0) )
        		cout << "initialised";
        	
        	return 0;
        }
        C++ isn't my main language infact this is my first major project using it.

        Thanks

        Comment


        • #5
          C++ isn't my language either, I prefer C#. One thing I notice is that you are using long instead of UInt32. Long is 64bits wide, where as Int32 and UInt32 are both 32bits wide.

          Also we have abandoned this old dll. We have a new Fusion_WinUSB.dll that is easier to interface with.
          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


          • #6
            Is there a download for the source of the new Fusion_WinUSB.dll so it will be easier to link with? and would it be possible for a list of the neccessary functions needed to show how to use them similar to the previous post you put up.

            Thanks

            Comment


            • #7
              All values are 0

              Hi I switched over to c# and I have got my program working with the drivers, but when I read in values from my FB and print them out all I get is 0 even though I have 3 temperature sensors connected to the FB.

              Code:
              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Runtime.InteropServices;
              using System.Timers;
              
              namespace ConsoleApplication1
              {
                  class Program
                  {
                      public const int analogueInput = 32;
                      public const int endOfAnalogueInput = 57;
                          
                      public const int digitalInput = 0;
                      public const int endOfDigitalInput = 31;
                      
                      public static class FusionUSB
                      {
              
                          [DllImport("FusionUSB.dll")]
                          public static extern bool FUSB_Initialize(ref UInt32 pFusionUSB, String deviceID, UInt32 instanceNumber);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern bool FUSB_Receive(UInt32 pFusionUSB, byte[] buffer);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern bool FUSB_Send(UInt32 pFusionUSB, byte[] buffer);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern UInt32 FUSB_Free(ref UInt32 pFusionUSB);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern bool FUSB_printf(UInt32 pFusionUSB);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern UInt32 FUSB_GetWinUSBerror(UInt32 pFusionUSB);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern UInt32 FUSB_GetFUSBstatus(UInt32 pFusionUSB);
              
                          [DllImport("FusionUSB.dll")]
                          public static extern String FUSB_GetDeviceInstanceID(UInt32 pFusionUSB);
              
                      }
              
              
                      static void Main(string[] args)
                      {
                          UInt32 instanceNumber = 0;
                          const UInt32 INVALID_HANDLE_VALUE = 0xFFFFFFFF;
                          const UInt32 NULL = 0;
                          UInt32 pFusionUSB = NULL;
                          byte[] ReadBuffer = new byte[64];
                          byte[] WriteBuffer = new byte[64];
                          const UInt32 FUSB_OK = 0;
                          const UInt32 FUSB_DEVICE_NOT_FOUND = 13;
                          const UInt32 MAX_DEVICES = 100;
                          string deviceID = @"USB\VID_0409&PID_005A\5&1ED3900F&0&3\";
                          bool flip_et_flop = true;
                          
                          
              
                          try
                          {
              
                              FusionUSB.FUSB_Initialize(ref pFusionUSB, deviceID, instanceNumber);
              
                              if (pFusionUSB != NULL)
                              {
                                  Console.WriteLine("Connected");
              
                                  FusionUSB.FUSB_Receive(pFusionUSB, ReadBuffer);
                                  
                                  /*Timer clock = new Timer();
                                  clock.Elapsed += new ElapsedEventHandler(Program.OnTimedEvent(ReadBuffer));
                                  clock.Elapsed += new ElapsedEventHandler(clock_Elapsed);
                                  clock.Interval = 100000;
                                  clock.Enabled = true;*/
              
              
                                  //read in analogue inputs
                                  int a = Console.Read();
                                  while (a != 0)
                                  {
                                      for (int i = analogueInput; i <= endOfAnalogueInput; i += 2)
                                      {
                                          Console.WriteLine("Byte " + i + " : " + ReadBuffer[i] + "," + ReadBuffer[i + 1]);
                                      }
                                      a = Console.Read();
                                  }
              
                                  
                              }
                              FusionUSB.FUSB_Free(ref pFusionUSB);
              
                          }
                          catch (DllNotFoundException)
                          {
                              Console.WriteLine("FusionUSB.dll Not Found");
                          }
                      }
              
                      static void clock_Elapsed(object sender, ElapsedEventArgs e)
                      {
                          throw new NotImplementedException();
                      }
                  }
              }
              Has any one any ideas why this is happening?

              Comment


              • #8
                You need to send then receive. If you only call receive, nothing will be returned.
                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


                • #9
                  Hi
                  I figured out after some test statements that nothing was working because the FB was never found, I've tried using the new FusionBrain_WinUSB.dll and the old FusionUSB.dll but neither seem to work. I have the FBv4 and I'm running XP. I have tried connecting two different brains one at a time and neither seems to get picked up by my software even though windows picks them up fine.

                  here is my FusionBrain_WinUSB.dll code
                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using FusionBrain_WinUSB;
                  
                  namespace ConsoleApplication2
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              //holds list of devices found
                              List <string> ver3 = null, ver4 = null;
                  
                              bool deviceFound = FusionBrain_WinUSB.MainUSBClass.FindAllDevices(ref ver3,ref ver4);
                              Console.WriteLine("Device Found : " + deviceFound);
                  
                              bool myDevice = FusionBrain_WinUSB.MainUSBClass.FindMyDevice(0, 4);
                              Console.WriteLine("Device Found : " + myDevice);
                                                      
                          }
                      }
                  }
                  and here is my FusionUSB.dll code
                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Runtime.InteropServices;
                  using System.Timers;
                  
                  namespace ConsoleApplication1
                  {
                      class Program
                      {
                          public const int analogueInput = 32;
                          public const int endOfAnalogueInput = 57;
                              
                          public const int digitalInput = 0;
                          public const int endOfDigitalInput = 31;
                          
                          public static class FusionUSB
                          {
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern bool FUSB_Initialize(ref UInt32 pFusionUSB, String deviceID, UInt32 instanceNumber);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern bool FUSB_Receive(UInt32 pFusionUSB, byte[] buffer);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern bool FUSB_Send(UInt32 pFusionUSB, byte[] buffer);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern UInt32 FUSB_Free(ref UInt32 pFusionUSB);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern bool FUSB_printf(UInt32 pFusionUSB);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern UInt32 FUSB_GetWinUSBerror(UInt32 pFusionUSB);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern UInt32 FUSB_GetFUSBstatus(UInt32 pFusionUSB);
                  
                              [DllImport("FusionUSB.dll")]
                              public static extern String FUSB_GetDeviceInstanceID(UInt32 pFusionUSB);
                  
                          }
                  
                  
                          static void Main(string[] args)
                          {
                              UInt32 instanceNumber = 0;
                              
                              const UInt32 NULL = 0;
                              
                              UInt32 pFusionUSB = NULL;
                              
                              byte[] ReadBuffer = new byte[64];
                              byte[] WriteBuffer = new byte[64];
                                          
                              // from mdx source code       
                              //string deviceID = @"USB\VID_04D8&PID_000C\";
                              
                              //full path from my devices
                              //string deviceID = @"USB\VID_04D8&PID_000E\5&327F1E4E&0&1";
                              
                              //root of vid & pid path from my devices
                              string deviceID = @"USB\VID_04D8&PID_000E\";
                             
                              try
                              {
                                  
                                  bool connected = FusionUSB.FUSB_Initialize(ref pFusionUSB, deviceID, instanceNumber);
                                  
                                  Console.WriteLine("Connected :" + connected );
                  
                                  if (connected)
                                  {
                                      // do stuff (removed to shorten the code)
                  
                                  }
                                  else
                                  {
                                      Console.WriteLine("Fusion Debug");
                                      FusionUSB.FUSB_printf(pFusionUSB);
                                                    
                                  }
                                 
                                 uint released = FusionUSB.FUSB_Free(ref pFusionUSB);
                                 Console.WriteLine("Release code :" + released );
                  
                              }
                              catch (DllNotFoundException)
                              {
                                  Console.WriteLine("FusionUSB.dll Not Found");
                              }
                          }
                  
                          static void clock_Elapsed(object sender, ElapsedEventArgs e)
                          {
                              throw new NotImplementedException();
                          }
                      }
                  }
                  For my FusionUSB.dll program I get the same error codes for all 3 device Ids. Sorry for the long post and thanks again for your help.

                  Comment

                  Working...
                  X