Main Site Documentation

USB Host Serial - event driven data recieved?


#1

I’ve looked thru the USBH_SerialUSB Class details and there’s nothing I can see to do a data driven event handling - am I missing something or misreading things? I am assuming that the alternative is to go back to polling for data with a time-out, are there other options?


#2

There are no events. You have to poll the data.


#3

Thanks for confirming Mike. I was hoping, since the Serial Port is done so well, that the USB might be similar, but this is certainly no show-stopper


#4

The way USB works, there is no interrupts, it is all polling. So if we were to add events, we will basically be cheating and pooling it internally. It is better to leave it to you to optimize to whatever fits your needs best.


#5

This is a great place to start using threads :slight_smile: You can have a thread to block while waiting for serial data, and in your “main” thread you can have an event loop, or use a wait handle to have a separate thread for processing responses, or do it in the very same thread.


#6

This makes me wonder;

How does the UART work internal? Is it some sort of hardware interrupt or is it a low level polling going on?
I mean if its polling in the built in firmware and your application isn’t using serial communication are’t we loosing valuable processor cycles?


#7

Physical UARTs on FEZ are all interrupt driven. No polling.
USB is another story.


#8

Thank you for that Mike.
Glad to hear :slight_smile:


#9

You can still cheat, with a polling thread. Have a look at my (link removed) :wink:

Here’s an excerpt of the running code :

public class Program
    {
        static Phidget1018 IF1018 = new Phidget1018();
        static Phidget1052 Encoder1052 = new Phidget1052();
        static OutputPort Led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
        static InterruptPort Bouton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
        
        public static void Main()
        {
            Bouton.OnInterrupt += Bouton_OnInterrupt;
            IF1018.InputChange += Phidget1018_InputChange;
            IF1018.SensorChange += Phidget1018_SensorChange;

            Encoder1052.PositionChange += Phidget1052_PositionChange;
            Encoder1052.InputChange += Phidget1052_InputChange;
            Thread.Sleep(Timeout.Infinite);
        }

        static void Bouton_OnInterrupt(uint data1, uint data2, System.DateTime time)
        {
            IF1018.Output(7, false);
            Led.Write(false);
        }

        static void Phidget1052_InputChange(object sender, Phidget1052InputChangeEventArgs e)
        {
            Debug.Print("Bouton : " + (e.Value ? "Enfonc�©" : "Rel�¢ch�©"));
        }

        static void Phidget1052_PositionChange(object sender, Phidget1052PositionChangeEventArgs e)
        {
            Debug.Print("Position change event. Position : " + e.Position.ToString() + ", Direction : " + (e.Direction ? "Forward" : "Backward")+", Relative move : "+e.RelativeMove.ToString());
        }