Spider + Android Phone via usb

Hi everyone, i don’t have much experience with .Net Gadgeteer, so i’m wondering if someone menaged to port FezAdbBridge example to fez Spider?
This example lets you control gadgeeter device from android phone via usb.
As I understand this example is for fez domino board.
Useful link http://code.google.com/p/robotfreak/wiki/FezBridge

I do not remember anyone saying they tried it with spider but It should be no problem.

Welcome to the community.

I cooked up this code but I get exemptions.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

//dodao
using Microsoft.SPOT.Hardware;

//using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBHost;
using GHIElectronics.NETMF.System;
//kraj
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace bridge3
{

public partial class Program
{
    //dodao

    static USBH_RawDevice usb;
    static USBH_RawDevice.Pipe adbInPipe;
    static USBH_RawDevice.Pipe adbOutPipe;
    static Thread adbInThread; // polls the adb for data


    const UInt32 A_SYNC = 0x434e5953;
    const UInt32 A_CNXN = 0x4e584e43;
    const UInt32 A_OPEN = 0x4e45504f;
    const UInt32 A_OKAY = 0x59414b4f;
    const UInt32 A_CLSE = 0x45534c43;
    const UInt32 A_WRTE = 0x45545257;

    private const ushort VALID_VID = 0x18D1;
    private const ushort VALID_PID = 0x4E12;
    private static bool initPhaseOneComplete;
    private static bool initPhaseTwoComplete;
    private static bool isReady;
    private static bool busyWrite;

    private static string hex = "0123456789ABCDEF";
    private static string hostName = "host::fezbridge";
    private static string portName = "tcp:4567";

    static Thread glavni;

    private UInt32[] AdbData = new UInt32[6];

    private struct sAdbMessage
    {
        public UInt32 command;
        public UInt32 arg0;
        public UInt32 arg1;
        public UInt32 dataLength;
        public UInt32 dataCheck;
        public UInt32 magic;

        public int ByteSize
        {
            get { return 24; }
        }

        public byte[] ToArray()
        {
            byte[] data = new byte[ByteSize];
            int offset = 0;

            data[offset++] = (byte)(command); data[offset++] = (byte)(command >> 8); data[offset++] = (byte)(command >> 16); data[offset++] = (byte)(command >> 24);
            data[offset++] = (byte)(arg0); data[offset++] = (byte)(arg0 >> 8); data[offset++] = (byte)(arg0 >> 16); data[offset++] = (byte)(arg0 >> 24);
            data[offset++] = (byte)(arg1); data[offset++] = (byte)(arg1 >> 8); data[offset++] = (byte)(arg1 >> 16); data[offset++] = (byte)(arg1 >> 24);
            data[offset++] = (byte)(dataLength); data[offset++] = (byte)(dataLength >> 8); data[offset++] = (byte)(dataLength >> 16); data[offset++] = (byte)(dataLength >> 24);
            data[offset++] = (byte)(dataCheck); data[offset++] = (byte)(dataCheck >> 8); data[offset++] = (byte)(dataCheck >> 16); data[offset++] = (byte)(dataCheck >> 24);
            data[offset++] = (byte)(magic); data[offset++] = (byte)(magic >> 8); data[offset++] = (byte)(magic >> 16); data[offset++] = (byte)(magic >> 24);
            /*
            string datastring = "Out>> ";

            for (int i = 0; i < data.Length; i++)
            {
              datastring += (ByteToHex(data[i]) + " ");
            }
            Debug.Print(datastring);
             */
            return data;
        }

        public static sAdbMessage Parse(byte[] data)
        {   
            sAdbMessage msg = new sAdbMessage();
            int offset = 0;

            msg.command = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            msg.arg0 = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            msg.arg1 = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            msg.dataLength = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            msg.dataCheck = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            msg.magic = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
            return msg;
        }
    };// private struct sAdbMessage

    static sAdbMessage adbMsg = new sAdbMessage();
    static sAdbMessage adbInMsg = new sAdbMessage();
    static byte[] adbData = new byte[65];

    

    // This method is run when the mainboard is powered up or reset.   
    void ProgramStarted()
    {
        /*******************************************************************************************
        Modules added in the Program.gadgeteer designer view are used by typing 
        their name followed by a period, e.g.  button.  or  camera.
        
        Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
            button.ButtonPressed +=<tab><tab>
        
        If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
            GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
            timer.Tick +=<tab><tab>
            timer.Start();
        *******************************************************************************************/


        // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
        //kod iz Main()



        glavni = new Thread(Glavna);
        glavni.Start();

        Debug.Print("Program Started");
    }//ProgramStarted

    static void Glavna()
    {
        Debug.EnableGCMessages(false);
        Debug.EnableGCMessages(false);

        // Subscribe to USBH event. Izmenjeni event handleri
        USBHostController.DeviceConnectedEvent += DeviceDisconnectedEvent;
        USBHostController.DeviceDisconnectedEvent += DeviceDisconnectedEvent;

        initPhaseOneComplete = false;
        initPhaseTwoComplete = false;
        isReady = false;
        busyWrite = false;

        // Sleep forever
        //Thread.Sleep(Timeout.Infinite);

    }


    static void DeviceDisconnectedEvent(USBH_Device device)
    {
        
        isReady = false;
        Debug.Print("Device disconnected");
        //multicolorLed.TurnRed();
        //print2();

    }



    static void DeviceConnectedEvent(USBH_Device device)
    {   
        if (!initPhaseOneComplete)
        {
            usb = new USBH_RawDevice(device);
            USBH_Descriptors.Configuration cd = usb.GetConfigurationDescriptors(0);
            USBH_Descriptors.Endpoint adbEP = null;
            USBH_Descriptors.Interface adbIF = null;

            Debug.Print("[Device, Port " + usb.PORT_NUMBER + "]");
            Debug.Print("Interface: " + usb.INTERFACE_INDEX);
            Debug.Print("ID: " + usb.ID);
            Debug.Print("Type: " + usb.TYPE);
            Debug.Print("VID: " + usb.VENDOR_ID);
            Debug.Print("PID: " + usb.PRODUCT_ID);
            //multicolorLed.TurnGreen();
            // look for HID class
            for (int i = 0; i < cd.bNumInterfaces; i++)
            {
                Debug.Print("Device connected");
                //print1();

                adbIF = cd.interfaces[i];
                if (adbIF.bInterfaceClass == 255)
                {

                    Debug.Print("  === Interface ===");
                    Debug.Print("  Class: " + cd.interfaces[i].bInterfaceClass);
                    Debug.Print("  SubClass: " + cd.interfaces[i].bInterfaceSubclass);
                    Debug.Print("  Number: " + cd.interfaces[i].bInterfaceNumber);
                    Debug.Print("  Protocol: " + cd.interfaces[i].bInterfaceProtocol);
                    Debug.Print("  Type: " + cd.interfaces[i].bDescriptorType);

                    //stampanje na displeju

                    //display_T35.SimpleGraphics.Clear();
                    //display_T35.SimpleGraphics.DisplayText("=== Interface ===", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 20);
                   // display_T35.SimpleGraphics.DisplayText("  Class: " + cd.interfaces[i].bInterfaceClass, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 35);
                    //display_T35.SimpleGraphics.DisplayText("  SubClass: " + cd.interfaces[i].bInterfaceSubclass, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 50);
                   // display_T35.SimpleGraphics.DisplayText("  Number: " + cd.interfaces[i].bInterfaceNumber, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 65);
                   // display_T35.SimpleGraphics.DisplayText("  Protocol: " + cd.interfaces[i].bInterfaceProtocol, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 80);
                    //display_T35.SimpleGraphics.DisplayText("  Type: " + cd.interfaces[i].bDescriptorType, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 95);
                    Thread.Sleep(2000);

                    for (int ep = 0; ep < adbIF.bNumberEndpoints; ep++)
                    {
                        adbEP = adbIF.endpoints[ep];

                        Debug.Print("   -- Endpoint --");
                        Debug.Print("    Attributes: " + adbIF.endpoints[ep].bmAttributes);
                        Debug.Print("    Address: " + adbIF.endpoints[ep].bEndpointAddress);
                        Debug.Print("    Type: " + adbIF.endpoints[ep].bDescriptorType);
                        Debug.Print("    Interval: " + adbIF.endpoints[ep].bInterval);
                        Debug.Print(" ");

                        //stampanje na displeju

                        //display_T35.SimpleGraphics.Clear();
                        //display_T35.SimpleGraphics.DisplayText("   -- Endpoint --", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 20);
                        //display_T35.SimpleGraphics.DisplayText("    Attributes: " + adbIF.endpoints[ep].bmAttributes, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 35);
                        //display_T35.SimpleGraphics.DisplayText("    Address: " + adbIF.endpoints[ep].bEndpointAddress, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 50);
                       //display_T35.SimpleGraphics.DisplayText("    Type: " + adbIF.endpoints[ep].bDescriptorType, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 65);
                        //display_T35.SimpleGraphics.DisplayText("    Interval: " + adbIF.endpoints[ep].bInterval, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 80);
                        //display_T35.SimpleGraphics.DisplayText(" ", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 95);
                        Thread.Sleep(2000);

                        switch (adbEP.bEndpointAddress)
                        {
                            case 133: // ADB data in
                                adbInPipe = usb.OpenPipe(adbEP);
                                adbInPipe.TransferTimeout = 0;              // recommended for interrupt transfers
                                break;
                            case 5:   // ADB data out
                                adbOutPipe = usb.OpenPipe(adbEP);
                                break;
                        }

                    }
                    initPhaseOneComplete = true;
                }
            }

        }
        else
        {
            initPhaseTwoComplete = true;
        }

        if (initPhaseTwoComplete)
        {
            initPhaseOneComplete = false;
            initPhaseTwoComplete = false;
            isReady = true;
        }

        if (isReady)
        {
            usb.SendSetupTransfer(0x00, 0x09, 0x0001, 0x0000);
            adbInThread = new Thread(AdbListening);      // create the polling thread
            adbInThread.Priority = ThreadPriority.Highest;
            adbInThread.Start();

            SendAdbMessage(A_CNXN, 16777216, 4096, hostName);
        }
    }//DeviceConnectedEvent
    
    static void AdbListening()
    {   

        int count = 0;
        string datastring = "";

        // Read every bInterval
        while (true)
        {
            Thread.Sleep(adbInPipe.PipeEndpoint.bInterval);

            try
            {
                count = adbInPipe.TransferData(adbData, 0, adbData.Length);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
            }

            if (count == 24)
            {
                adbInMsg = sAdbMessage.Parse(adbData);
                switch (adbInMsg.command)
                {
                     
                    case A_CNXN:
                        Debug.Print("In << CNXN " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                        break;
                    case A_OPEN:
                        Debug.Print("In << OPEN " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                        break;
                    case A_OKAY:
                        Debug.Print("In << OKAY " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                        busyWrite = false;
                        break;
                    case A_CLSE:
                        Debug.Print("In << CLSE " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                        SendAdbMessage(A_OKAY, adbInMsg.arg1, adbInMsg.arg0);
                        break;
                    case A_WRTE:
                        Debug.Print("In << WRTE " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                        SendAdbMessage(A_OKAY, adbInMsg.arg1, adbInMsg.arg0);
                        break;
                    default:
                        datastring = ByteArrayToString(adbData);
                        Debug.Print("In << (" + count.ToString() + ") " + datastring);
                        break;
                }
            }
            else if (count > 0)
            {
                adbData[count] = 0;
                datastring = ByteArrayToString(adbData);
                Debug.Print("In << (" + count.ToString() + ") " + datastring);
                switch (datastring)
                {
                    case "device::":
                        SendAdbMessage(A_OPEN, 1, 0, portName);
                        break;
                    default:
                        string[] cmds = datastring.Split(';');
                        for (int i = 0; i < cmds.Length; i++)
                        {
                            string[] cmd = cmds[i].Split(':');
                            if (cmd.Length == 2)
                            {
                                switch (cmd[0])
                                {
                                    case "x":
                                       
                                       // servo1.SetPosition((byte)int.Parse(cmd[1]));
                                        break;
                                    case "y":
                                      
                                      //  servo2.SetPosition((byte)int.Parse(cmd[1]));
                                        break;
                                }
                            }

                        }
                        break;
                }
            }
            else if (count == 0)
            {
                //float value = myRanger.GetDistance_cm();
               // Debug.Print("myRanger reading is: " + value.ToString());
               // SendAdbMessage(A_WRTE, 1, 25, "a0:" + value.ToString() + ";");
                Thread.Sleep(10);
            }

        }
    }//AdbListening()

    public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1)
    {
        SendAdbMessage(st, arg0, arg1, (byte[])null);
    }

    public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1, string str)
    {
        SendAdbMessage(st, arg0, arg1, StringToByteArray(str));
    }

    public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1, byte[] data)
    {
        UInt32 crc = 0;
        UInt32 cnt = 0;

        if (data != null)
        {
            while (cnt < data.Length)
            {
                crc += data[cnt++];
            }
            cnt++;
        }

        Debug.Print("Out >> " + st.ToString() + "," + arg0.ToString() + "," + arg1.ToString());
        adbMsg.command = st;
        adbMsg.arg0 = arg0;
        adbMsg.arg1 = arg1;
        adbMsg.dataLength = cnt;
        adbMsg.dataCheck = crc;
        adbMsg.magic = st ^ 0xffffffff;
        try
        {
            adbOutPipe.TransferData(adbMsg.ToArray(), 0, adbMsg.ByteSize);

            if (data != null)
            {
                byte[] zdata = new byte[data.Length + 1];
                data.CopyTo(zdata, 0);
                adbOutPipe.TransferData(zdata, 0, zdata.Length);
            }
        }
        catch (Exception ex)
        {
            Debug.Print(ex.ToString());
        }

    }//SendAdbMessage

    private static byte[] StringToByteArray(string str)
    {
        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        return enc.GetBytes(str);
    }

    private static string ByteArrayToString(byte[] data)
    {
        return new string(System.Text.Encoding.UTF8.GetChars(data));
    }

    public static string UIn16tToHex(UInt16 number)
    {
        return new string(new char[] { hex[(number & 0xF000) >> 12], hex[(number & 0xF00) >> 8], hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
    }

    public static string ByteToHex(byte number)
    {
        return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
    }

    void print1()//stampanje na displeju
    {
        display_T35.SimpleGraphics.Clear();
        display_T35.SimpleGraphics.DisplayText("Device Connected", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 20);
        display_T35.SimpleGraphics.DisplayText("Device, Port [" + usb.PORT_NUMBER + "]", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 35);
        display_T35.SimpleGraphics.DisplayText("Interface: " + usb.INTERFACE_INDEX, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 50);
        display_T35.SimpleGraphics.DisplayText("ID: " + usb.ID, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 65);
        display_T35.SimpleGraphics.DisplayText("Type: " + usb.TYPE, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 80);
        display_T35.SimpleGraphics.DisplayText("VID: " + usb.VENDOR_ID, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 95);
        display_T35.SimpleGraphics.DisplayText("PID: " + usb.PRODUCT_ID, Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 110);
        Thread.Sleep(2000);
    }

    void print2()//stampanje na displeju
    {
        display_T35.SimpleGraphics.Clear();
        display_T35.SimpleGraphics.DisplayText("Device disconnected", Resources.GetFont(Resources.FontResources.NinaB), GT.Color.White, 50, 20);
        Thread.Sleep(2000);
    }

    void print3()
    {

    
    }

}// clas Program

}//bridge3

two things.

First, edit your post above (use the little pencil looking icon at the top of your post). Then highlight all your code, then hit the 101010 icon at the TOP of your post (not the coloured ones). Then re-submit it… this will allow the code to be formatted better.

Second, giving us a code sample like this, and saying “but I get exemptions(sic)” isn’t very helpful. Tell us about the exceptions you get, and where in your code they come up? Have you stepped into your code and see what goes on? What does work and what doesn’t? And have you tried breaking down your code into usable chunks and proven each one works?

Oh and a third too, it’d be great if you can update your forum identity so we know who you are and so we can stop thinking of you as just a number :wink:


//Program.cs
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.USBHost;
using GHIElectronics.NETMF.System;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace bridge3
{



    public partial class Program
    {
        

        private enum Status
            {
                Idle, Ahead, Back, TurnRight, TurnLeft, Rotate
            }




        private enum RoverDirection
            {
                None = 0,
                Ahead = 1,
                Back = 2,
            }

        static RoverDirection roverDirection;



        static bool pomocna = false;

        static Status currentStatus;
        static Status newStatus;
        static Int16 Xdata;
        static Int16 Ydata;


        static USBH_RawDevice usb;
        static USBH_RawDevice.Pipe adbInPipe;
        static USBH_RawDevice.Pipe adbOutPipe;
        static Thread adbInThread; // polls the adb for data


        const UInt32 A_SYNC = 0x434e5953;
        const UInt32 A_CNXN = 0x4e584e43;
        const UInt32 A_OPEN = 0x4e45504f;
        const UInt32 A_OKAY = 0x59414b4f;
        const UInt32 A_CLSE = 0x45534c43;
        const UInt32 A_WRTE = 0x45545257;

        private const ushort VALID_VID = 0x18D1;
        private const ushort VALID_PID = 0x4E12;
        private static bool initPhaseOneComplete;
        private static bool initPhaseTwoComplete;
        private static bool isReady;
        private static bool busyWrite;

        private static string hex = "0123456789ABCDEF";
        private static string hostName = "host::fezbridge";
        private static string portName = "tcp:4567";

        private UInt32[] AdbData = new UInt32[6];

        private struct sAdbMessage
        {
            public UInt32 command;
            public UInt32 arg0;
            public UInt32 arg1;
            public UInt32 dataLength;
            public UInt32 dataCheck;
            public UInt32 magic;

            public int ByteSize
            {
                get { return 24; }
            }

            public byte[] ToArray()
            {
                byte[] data = new byte[ByteSize];
                int offset = 0;

                data[offset++] = (byte)(command); data[offset++] = (byte)(command >> 8); data[offset++] = (byte)(command >> 16); data[offset++] = (byte)(command >> 24);
                data[offset++] = (byte)(arg0); data[offset++] = (byte)(arg0 >> 8); data[offset++] = (byte)(arg0 >> 16); data[offset++] = (byte)(arg0 >> 24);
                data[offset++] = (byte)(arg1); data[offset++] = (byte)(arg1 >> 8); data[offset++] = (byte)(arg1 >> 16); data[offset++] = (byte)(arg1 >> 24);
                data[offset++] = (byte)(dataLength); data[offset++] = (byte)(dataLength >> 8); data[offset++] = (byte)(dataLength >> 16); data[offset++] = (byte)(dataLength >> 24);
                data[offset++] = (byte)(dataCheck); data[offset++] = (byte)(dataCheck >> 8); data[offset++] = (byte)(dataCheck >> 16); data[offset++] = (byte)(dataCheck >> 24);
                data[offset++] = (byte)(magic); data[offset++] = (byte)(magic >> 8); data[offset++] = (byte)(magic >> 16); data[offset++] = (byte)(magic >> 24);
                return data;
            }

            public static sAdbMessage Parse(byte[] data)
            {
                sAdbMessage msg = new sAdbMessage();
                int offset = 0;

                msg.command = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                msg.arg0 = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                msg.arg1 = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                msg.dataLength = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                msg.dataCheck = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                msg.magic = (UInt32)(data[offset++] + (data[offset++] << 8) + (data[offset++] << 16) + (data[offset++] << 24));
                return msg;
            }
        };

        static sAdbMessage adbMsg = new sAdbMessage();
        static sAdbMessage adbInMsg = new sAdbMessage();
        static byte[] adbData = new byte[65];


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            multicolorLed.BlinkOnce(GT.Color.Orange);
            multicolorLed.GreenBlueSwapped=true;
            Debug.Print("Program Started");
            Debug.EnableGCMessages(false);

            // Subscribe to USBH event.
            USBHostController.DeviceConnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceConnectedEvent);
            USBHostController.DeviceDisconnectedEvent += new USBH_DeviceConnectionEventHandler(USBHostController_DeviceDisconnectedEvent);
            initPhaseOneComplete = false;
            initPhaseTwoComplete = false;
            isReady = false;
            busyWrite = false;

            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }//ProgramStarted


        void USBHostController_DeviceConnectedEvent(USBH_Device device)
        {
            if (!initPhaseOneComplete)
            {
                usb = new USBH_RawDevice(device);
                USBH_Descriptors.Configuration cd = usb.GetConfigurationDescriptors(0);
                USBH_Descriptors.Endpoint adbEP = null;
                USBH_Descriptors.Interface adbIF = null;

                Debug.Print("[Device, Port " + usb.PORT_NUMBER + "]");
                Debug.Print("Interface: " + usb.INTERFACE_INDEX);
                Debug.Print("ID: " + usb.ID);
                Debug.Print("Type: " + usb.TYPE);
                Debug.Print("VID: " + usb.VENDOR_ID);
                Debug.Print("PID: " + usb.PRODUCT_ID);

                // look for HID class
                for (int i = 0; i < cd.bNumInterfaces; i++)
                {

                    adbIF = cd.interfaces[i];
                    string interf = adbIF.bInterfaceClass.ToString();

                    Debug.Print(interf);
                    if (adbIF.bInterfaceClass == 255)
                    {

                        Debug.Print("  === Interface ===");
                        Debug.Print("  Class: " + cd.interfaces[i].bInterfaceClass);
                        Debug.Print("  SubClass: " + cd.interfaces[i].bInterfaceSubclass);
                        Debug.Print("  Number: " + cd.interfaces[i].bInterfaceNumber);
                        Debug.Print("  Protocol: " + cd.interfaces[i].bInterfaceProtocol);
                        Debug.Print("  Type: " + cd.interfaces[i].bDescriptorType);

                        for (int ep = 0; ep < adbIF.bNumberEndpoints; ep++)
                        {
                            adbEP = adbIF.endpoints[ep];

                            Debug.Print("   -- Endpoint --");
                            Debug.Print("    Attributes: " + adbIF.endpoints[ep].bmAttributes);
                            Debug.Print("    Address: " + adbIF.endpoints[ep].bEndpointAddress);
                            Debug.Print("    Type: " + adbIF.endpoints[ep].bDescriptorType);
                            Debug.Print("    Interval: " + adbIF.endpoints[ep].bInterval);
                            Debug.Print(" ");
                            string adbEPbEndp = adbEP.bEndpointAddress.ToString();
                            Debug.Print(adbEPbEndp);
                            switch (adbEP.bEndpointAddress)
                            {
                                case 132: // ADB data in old value 133, 132 for 2.2, 130 for 4.0.4
                                    adbInPipe = usb.OpenPipe(adbEP);
                                    adbInPipe.TransferTimeout = 0;              // recommended for interrupt transfers
                                    break;
                                case 133: // ADB data in old value 133, 132 for 2.2, 130 for 4.0.4
                                    adbInPipe = usb.OpenPipe(adbEP);
                                    adbInPipe.TransferTimeout = 0;
                                    break;
                                case 130: // ADB data in old value 133, 132 for 2.2, 130 for 4.0.4
                                    adbInPipe = usb.OpenPipe(adbEP);
                                    adbInPipe.TransferTimeout = 0;
                                    break;
                                case 3:   // ADB data out old value 5, 3 for 2.2, 2 for 4.0.4
                                    adbOutPipe = usb.OpenPipe(adbEP);
                                    break;
                                case 5:   // ADB data out old value 5, 3 for 2.2, 2 for 4.0.4
                                    adbOutPipe = usb.OpenPipe(adbEP);
                                    break;
                                case 2:   // ADB data out old value 5, 3 for 2.2, 2 for 4.0.4
                                    adbOutPipe = usb.OpenPipe(adbEP);
                                    break;
                            }

                        }
                        initPhaseOneComplete = true;
                    }
                }

            }
            else
            {
                initPhaseTwoComplete = true;
            }

            if (initPhaseTwoComplete)
            {
                initPhaseOneComplete = false;
                initPhaseTwoComplete = false;
                isReady = true;
            }

            if (isReady)
            {
                USBH_Descriptors.Configuration cd = usb.GetConfigurationDescriptors(0);
                usb.SendSetupTransfer(0x00, 0x09, cd.bConfigurationValue, 0x0000);
                adbInThread = new Thread(AdbListening);      // create the polling thread
                adbInThread.Priority = ThreadPriority.Highest;
                adbInThread.Start();

                SendAdbMessage(A_CNXN, 16777216, 4096, hostName);
            }

        }

        void USBHostController_DeviceDisconnectedEvent(USBH_Device device)
            {
                adbInThread.Suspend();
                isReady = false;

            }



        static void menjac()    //direction chooser
        {
            if(Ydata<50)
                {   
                    newStatus = Status.Ahead;
                    roverDirection = RoverDirection.Ahead;
                    multicolorLed.TurnWhite();
                    pomocna = false;
                }

            if(Ydata >120)  
                {
                    newStatus = Status.Back;
                    roverDirection = RoverDirection.Back;
                    multicolorLed.TurnRed();
                    pomocna = false;
                }

            if(Ydata>50 && Ydata<120)  
                {
                    pomocna = true;
                }

            if (Xdata > 60 && Xdata < 110 && pomocna)
                {
                    newStatus = Status.Idle;
                    roverDirection = RoverDirection.None;
                    multicolorLed.TurnOff();
                }


            if(pomocna && Xdata>110)
                {
                    newStatus = Status.TurnRight;
                    multicolorLed.TurnGreen();
                }

            if (pomocna && Xdata<60)
                {
                    newStatus = Status.TurnLeft;
                    multicolorLed.TurnBlue();
                }
            
            Movement();
        }


        static void Movement() //motor driving
        {
            if (currentStatus != newStatus)
            {


                int speed = 50;
                int currentDirection1 = 0;
                int currentDirection2 = 0;

                switch (newStatus)
                {
                    case Status.Idle:
                        currentDirection1 = currentDirection2 = 0;
                        break;

                    case Status.Ahead:
                        currentDirection1 = currentDirection2 = 1;
                        break;

                    case Status.Back:
                        currentDirection1 = currentDirection2 = -1;
                        break;

                    case Status.TurnRight:
                        speed = 100;
                        currentDirection1 = 0;
                        if (roverDirection == RoverDirection.Ahead)
                            currentDirection2 = 1;
                        else if (roverDirection == RoverDirection.Back)
                            currentDirection2 = -1;
                        break;

                    case Status.TurnLeft:
                        speed = 100;
                        currentDirection2 = 0;
                        if (roverDirection == RoverDirection.Ahead)
                            currentDirection1 = 1;
                        else if (roverDirection == RoverDirection.Back)
                            currentDirection1 = -1;
                        break;

                    case Status.Rotate:
                        speed = 100;
                        currentDirection1 = 1;
                        currentDirection2 = -1;
                        break;

                    default:
                        break;
                }

                motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, speed * currentDirection1);
                motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, speed * currentDirection2);

                currentStatus = newStatus;

            }
        }


        static void AdbListening()
        {
            int count = 0;
            string datastring = "";

            // Read every bInterval
            while (true)
            {
                Thread.Sleep(adbInPipe.PipeEndpoint.bInterval);

                try
                {
                    count = adbInPipe.TransferData(adbData, 0, adbData.Length);
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.ToString());
                }

                if (count == 24)
                {
                    adbInMsg = sAdbMessage.Parse(adbData);
                    switch (adbInMsg.command)
                    {
                        case A_CNXN:
                            Debug.Print("In << CNXN " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                            break;
                        case A_OPEN:
                            Debug.Print("In << OPEN " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                            break;
                        case A_OKAY:
                            Debug.Print("In << OKAY " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                            busyWrite = false;
                            break;
                        case A_CLSE:
                            Debug.Print("In << CLSE " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                            SendAdbMessage(A_OKAY, adbInMsg.arg1, adbInMsg.arg0);
                            break;
                        case A_WRTE:
                            Debug.Print("In << WRTE " + adbInMsg.arg0.ToString() + "," + adbInMsg.arg1.ToString());
                            SendAdbMessage(A_OKAY, adbInMsg.arg1, adbInMsg.arg0);
                            break;
                        default:
                            datastring = ByteArrayToString(adbData);
                            Debug.Print("In << (" + count.ToString() + ") " + datastring);
                            break;
                    }
                }
                else if (count > 0)
                {

                    adbData[count] = 0;
                    datastring = ByteArrayToString(adbData);
                    Debug.Print("In << (" + count.ToString() + ") " + datastring);
                    switch (datastring)
                    {
                        case "device::":
                            SendAdbMessage(A_OPEN, 1, 0, portName);
                            break;
                        default:
                            string[] cmds = datastring.Split(';');
                            for (int i = 0; i < cmds.Length; i++)
                            {
                                string[] cmd = cmds[i].Split(':');
                                if (cmd.Length == 2)
                                {
                                    switch (cmd[0])
                                    {
                                        case "x":
                                            Debug.Print("X:"+(cmd[1]));
                                            
                                            if (cmd[1] != "")
                                                {
                                                    Xdata = System.Convert.ToInt16(cmd[1]);
                                                   // menjac();
                                                }

                                            break;
                                        case "y":
                                            Debug.Print("Y:"+(cmd[1]));
                                            
                                            if (cmd[1] != "")
                                                {
                                                    Ydata = System.Convert.ToInt16(cmd[1]);
                                                    menjac();
                                                }

                                            break;

                                           
                                    }
                                }

                            }
                            break;
                    }
                }
                else if (count == 0)
                {
                    // float value = 5;
                    //  Debug.Print("myRanger reading is: " + value.ToString());
                    //  SendAdbMessage(A_WRTE, 1, 25, "a0:" + value.ToString() + ";");
                    Thread.Sleep(10);
                }

            }
        }

        public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1)
        {
            SendAdbMessage(st, arg0, arg1, (byte[])null);
        }

        public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1, string str)
        {
            SendAdbMessage(st, arg0, arg1, StringToByteArray(str));
        }

        public static void SendAdbMessage(UInt32 st, UInt32 arg0, UInt32 arg1, byte[] data)
        {
            UInt32 crc = 0;
            UInt32 cnt = 0;

            if (data != null)
            {
                while (cnt < data.Length)
                {
                    crc += data[cnt++];
                }
                cnt++;
            }

            Debug.Print("Out >> " + st.ToString() + "," + arg0.ToString() + "," + arg1.ToString());
            adbMsg.command = st;
            adbMsg.arg0 = arg0;
            adbMsg.arg1 = arg1;
            adbMsg.dataLength = cnt;
            adbMsg.dataCheck = crc;
            adbMsg.magic = st ^ 0xffffffff;
            try
            {
                adbOutPipe.TransferData(adbMsg.ToArray(), 0, adbMsg.ByteSize);

                if (data != null)
                {
                    byte[] zdata = new byte[data.Length + 1];
                    data.CopyTo(zdata, 0);
                    adbOutPipe.TransferData(zdata, 0, zdata.Length);
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
            }

        }

        private static byte[] StringToByteArray(string str)
        {
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            return enc.GetBytes(str);
        }

        private static string ByteArrayToString(byte[] data)
        {
            return new string(System.Text.Encoding.UTF8.GetChars(data));
        }

        public static string UIn16tToHex(UInt16 number)
        {
            return new string(new char[] { hex[(number & 0xF000) >> 12], hex[(number & 0xF00) >> 8], hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
        }

        public static string ByteToHex(byte number)
        {
            return new string(new char[] { hex[(number & 0xF0) >> 4], hex[number & 0x0F] });
        }

    }
}


//Program.generated.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the Gadgeteer Designer.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace bridge3
{
    public partial class Program : Gadgeteer.Program
    {
        // GTM.Module definitions
      static  Gadgeteer.Modules.GHIElectronics.UsbHost usbHost;
      static Gadgeteer.Modules.GHIElectronics.Display_T35 display_T35;
      static Gadgeteer.Modules.GHIElectronics.MotorControllerL298 motorControllerL298;
      static Gadgeteer.Modules.GHIElectronics.MulticolorLed multicolorLed;

        public static void Main()
        {
            //Important to initialize the Mainboard first
            Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();			

            Program program = new Program();
            program.InitializeModules();
            program.ProgramStarted();
            program.Run(); // Starts Dispatcher
        
        }



        private void InitializeModules()
        {   
            // Initialize GTM.Modules and event handlers here.		
            usbHost = new GTM.GHIElectronics.UsbHost(3);
		
            multicolorLed = new GTM.GHIElectronics.MulticolorLed(8);
		
            display_T35 = new GTM.GHIElectronics.Display_T35(14, 13, 12, 10);
		
            motorControllerL298 = new GTM.GHIElectronics.MotorControllerL298(11);

        }
    }
}

I solved all problems, these are my codes if someone wants to use it.

Please post the code on codeshare. Extra credit for a video showing it in action :slight_smile: