Connecting FTDI device to USB Host module -

Hi, I have a sensor that I connected to the USB module.
It is a FTDI device as seen at the image.

I added the following code to my program

using GHI.Usb.Host;
using Microsoft.SPOT;
using System.Threading;

public class Program
{
    public static void Main()
    {
        Controller.UsbSerialConnected += Controller_UsbSerialConnected;

        Controller.Start();

        Thread.Sleep(-1);
    }

    private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
    {
        Debug.Print("UsbSerial connected.");

        usbSerial.DataReceived += usbSerial_DataReceived;
    }

    private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
    {
        for (int i = 0; i < e.Data.Length; i++)
            Debug.Print(e.Data[i].ToString());

        sender.Write(e.Data);
    }
}

But I don’t get the Controller_UsbSerialConnected event to fire up.

Any Ideas?

@ cyberh0me - Hi, I connected my logitech mouse+keyboard (wireless USB) to the HUB and added the keyboard and mouse connected event and still nothing happened, Is the HUB corrupted? Can I check it somehow?

@ cyberh0me - I did, still nothing,
So I switched the USB host (I have three) and still no response (even from another wireless mouse I connected)

@ bioengproject - What device are you using?

@ John - It is connected to this device:

However, I wanted to test USB host itself and connected mouse and keyboards and added the relevant events but still nothing.
I think it may be related to the new framework 4.3, (just like the thread I opened about the ethernet module)

FTDI has many chips but we only support the FT232 chip I believe.

@ Gus - I spoke with the company and they told me that someone already done it !
Anyhow, I switched the board, and now the mouse is getting noticed, Ill check now the device, maybe the problem is with the board

@ bioengproject - OK, it is recognized as unknown device but i get:
one the following line:

serial = new UsbSerial(e.Id, e.InterfaceIndex, e.VendorId, e.ProductId, e.PortNumber, BaseDevice.DeviceType.SerialProlific);

Unknown device connected.
#### Exception System.Exception - 0xffffffff (4) ####
#### Message:
#### GHI.Usb.Host.UsbSerial::NativeConstructor [IP: 0000] ####
#### GHI.Usb.Host.UsbSerial::.ctor [IP: 0092] ####
#### TestUSBHost.Program::Controller_UnknownDeviceConnected [IP: 0021] ####
#### GHI.Usb.Host.Controller::OnUnknownDeviceConnected [IP: 000d] ####
#### GHI.Usb.Host.Controller::OnConnect [IP: 00df] ####
#### GHI.Utilities.InternalEvent::RaiseEvent [IP: 000d] ####
#### GHI.Utilities.InternalEvent::OnEvent [IP: 003e] ####
A first chance exception of type ‘System.Exception’ occurred in GHI.Usb.dll
An unhandled exception of type ‘System.Exception’ occurred in GHI.Usb.dll

        private static void Controller_UnknownDeviceConnected(object sender, Controller.UnknownDeviceConnectedEventArgs e)
        {
            Debug.Print("Unknown device connected.");

            serial = new UsbSerial(e.Id, e.InterfaceIndex, e.VendorId, e.ProductId, e.PortNumber, BaseDevice.DeviceType.SerialProlific);
            serial.DataReceived += serial_DataReceived;
        }

And another thing, it’s working with the original mainboard, so i guess the problem is somewhere at the project itself.

Mega Strange :wall:

So two issues:

  1. The exception above - when I run a test program (just the example from https://www.ghielectronics.com/docs/36/usb-host#317)

  2. When running my program, the event isn’t fired up.

Can I get some answers here?

Not enough power? Does your device use usb for power?

@ Gus - What the difference ? Longer code can cause the usb event not to come up?

@ bioengproject - if it is a FTDI device why are you saying it is a prolific device in your code?

@ Mike - So how should I use it?
Anyhow, I still don’t get why a longer code causes event not come up. I think it has something with the properties of my project,
I’ll check and reply ASAP

BaseDevice.DeviceType.SerialProlific
Use a different Device Type.
Check out the constructor doco in https://www.ghielectronics.com/downloads/man/Library_Documentation_v4.3/html/T_GHI_Usb_Host_BaseDevice_DeviceType.htm
Try the SerialFTDI first, if it’s an FTDI…

When you say longer code, is there a chance you have a tight loop that does not yield (Thread.Sleep()) somewhere in it?

@ Dave McLaughlin - Hi Dave, if it was the situation, non of the events wouldn’t start, but the event of ethernet,networkUp is fired up so…

Ok this is my setup right now, but, I took the code from mouse controller example:
This is the only code of the program:


        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

            Controller.MouseConnected += Controller_MouseConnected;
            Controller.Start();
            Debug.Print("Program Started");
        }
        private static void Controller_MouseConnected(object sender, Mouse mouse)
        {
            Debug.Print("Mouse connected.");
             mouse.ButtonChanged += mouse_ButtonChanged;
            mouse.CursorMoved += mouse_CursorMoved;
            mouse.WheelMoved += mouse_WheelMoved;
        }

        private static void mouse_ButtonChanged(Mouse sender, Mouse.ButtonChangedEventArgs e)
        {
            Debug.Print(e.State.ToString() + " " + e.Which);
        }

        private static void mouse_CursorMoved(Mouse sender, Mouse.CursorMovedEventArgs e)
        {
            Debug.Print(e.Delta.ToString() + " " + e.NewPosition.ToString());
        }

        private static void mouse_WheelMoved(Mouse sender, Mouse.WheelMovedEventArgs e)
        {
            Debug.Print(e.Delta.ToString() + " " + e.NewPosition.ToString());
        }
    }

On the other hand, the diagram is also attached, you may see the SD Card and the USB host.
The event wasn’t fired up.
However, when I deleted from the diagram the SD Card, (Same code and same real setup)
The event fired up. Same happened with ethernet module…

:wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall: :wall:

@ bioengproject - An answer would be nice.

@ bioengproject - its been the weekend, still is in the US, time to get a grip.

Please show your ProgramStarted code when you have all your modules attached in designer. Please also show the ProgramGenerated code.

@ Brett -

public partial class Program
    {
        static UsbSerial usbSerial1;
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Controller.UsbSerialConnected += Controller_UsbSerialConnected;
            Thread.Sleep(100);
            Controller.UnknownDeviceConnected += Controller_UnknownDeviceConnected;
            Thread.Sleep(100);
            Controller.Start();
            Debug.Print("Program Started");

        }
        private static void Controller_UnknownDeviceConnected(object sender, Controller.UnknownDeviceConnectedEventArgs e)
        {
            Debug.Print("Unknown device connected.");
            //    Glide.MessageBoxManager.Show("Serial up", "Notification");
            usbSerial1 = new UsbSerial(e.Id, e.InterfaceIndex, e.VendorId, e.ProductId, e.PortNumber, BaseDevice.DeviceType.SerialFTDI);
            usbSerial1.DataReceived += serial_DataReceived;
        }
        private static void serial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
        {
            for (int i = 0; i < e.Data.Length; i++)
                Debug.Print(e.Data[i].ToString());

            sender.Write(e.Data);
        }
        private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
        {
            Debug.Print("UsbSerial connected.");

            usbSerial.DataReceived += usbSerial_DataReceived;
        }

        private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
        {
            for (int i = 0; i < e.Data.Length; i++)
                Debug.Print(e.Data[i].ToString());

            sender.Write(e.Data);
        }
        

    }
 public partial class Program : Gadgeteer.Program {
        
        /// <summary>The USB Host module using socket 3 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.USBHost usbHost;
        
        /// <summary>The SD Card module using socket 5 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.SDCard sdCard;
        
        /// <summary>This property provides access to the Mainboard API. This is normally not necessary for an end user program.</summary>
        protected new static GHIElectronics.Gadgeteer.FEZSpider Mainboard {
            get {
                return ((GHIElectronics.Gadgeteer.FEZSpider)(Gadgeteer.Program.Mainboard));
            }
            set {
                Gadgeteer.Program.Mainboard = value;
            }
        }
        
        /// <summary>This method runs automatically when the device is powered, and calls ProgramStarted.</summary>
        public static void Main() {
            // Important to initialize the Mainboard first
            Program.Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();
            Program p = new Program();
            p.InitializeModules();
            p.ProgramStarted();
            // Starts Dispatcher
            p.Run();
        }
        
        private void InitializeModules() {
            this.usbHost = new GTM.GHIElectronics.USBHost(3);
            this.sdCard = new GTM.GHIElectronics.SDCard(5);
        }
    }

The problem is, when the SD connected, nothing happens, if it’s disconnected but still shown on the diagram it’s still not working.
If it’s connected to the board but deleted from the diagram it’s working but i get the exception i described before.
if it’s not connected and deleted from the diagram, it’s working, but i get the exception i described before.
The external power source is 24 Volt.
And so I’m like :wall:

@ bioengproject - Before we get to the SD issue, lets resolve serial. Start by updating your device to the latest 4.3 2016 R1 Pre-Release 2 SDK. If it is already running that, reflash it to be safe. Then start a new Micro Framework project, not Gadgeteer, and run exactly the code on https://www.ghielectronics.com/docs/36/usb-host#3300 changing SerialProlific to SerialFTDI in the UsbSerial constructor.

If you get an exception, try changing cables, the power module, the mainboard itself, the USB cables used, as much as you can. Make sure it is powered externally. If you have another USB to serial device, such as http://www.ftdichip.com/Support/Documents/DataSheets/Cables/DS_TTL-232R_CABLES.pdf, try that and see if it works.

If you still get an exception, unfortunately, it seems that that device is not supported using the built in drivers. While manually constructing a UsbSerial object from the parameters of the UnknownDeviceConnected event often works, it is not guaranteed to work every time. If that is the case, your only option of using the device is to write a driver for it yourself using GHI.Usb.Host.RawDevice. That requires knowledge of the low-level USB protocol however.

So we can see if we have one here that we can test, what exact product are you trying to connect? To verify, you are using the FEZ Spider, not the FEZ Spider II?