Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 15 of 15

Thread: Teensy HID C# project

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    9

    Teensy HID C# project

    Hi,
    we are trying to get Teensy to send usb data to PC. Using Visual Studio and C# it seems we are having issues with some exceptions happening and not being able to connect.
    Is there an example we can use? Has someone done it?

    Thanks

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    Do you want to send the data via Serial or via HID?

  3. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    HID. Serial has latency on windows most of the times.

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    Never had any latency problems with Serial and c# but that of course depends on the requirements. However, I'm not sure if HID will be better (latency is probably more a USB problem).
    Anyway, never did communication over HID it but I can give it a try later today

  5. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    Thanks. I need to send something like 5x 32bit words at 10Khz. Concept is kind of digital scope, no buffering. Real Time.

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,697
    I could be wrong but I think usb 2 has like a millisecond frame, so my guess is you might not be able to send more than 1000 packets per second.

    So question is can you package up more than Set of data per packet.

  7. #7
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    5x 32bit words at 10Khz.
    HID is limited to 64kB /s. 5x32bit @ 10kHz gives 200kB/s that is going to be difficult :-)

  8. #8
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    133
    You might also consider the fact that "real time" display of data does not mean 0ms latency, because a typical display only shows a new frame every 16.667ms (at 60 Hz frame rate).
    Even audio has a latency, although it varies based on the buffer size (and OS too, I guess).

    Even if you use custom hardware, us humans tend not to notice a latency or timing discrepancy of a few milliseconds at all. So, strive for good and practical, not for perfect.

  9. #9
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    As promised, a minimal example which compiles and works. The Teensy firmware repeatedly sends a raw hid frame where the first byte contains a counter to the PC. The PC software waits until it receives a frame and writes the first byte of the frame to the console. Hope that helps.

    Here the c# part (Windows Console App, I use the HID library from Mike O'Brian which you can install from nuget)

    Code:
    using HidLibrary;
    using System;
    using System.Linq;
    
    namespace hid2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var teensy = HidDevices.Enumerate(0x16C0, 0x0486).Where(d => d.Capabilities.Usage == 512).FirstOrDefault();
    
                while (teensy != null)
                {
                    var report = teensy.ReadReport();
                    Console.WriteLine(report.Data[0]);
                }
            }
        }
    }
    and here the Teensy part

    Code:
    #include "Arduino.h"
    
    uint8_t buf[64];
    
    void setup()
    {
       pinMode(LED_BUILTIN, OUTPUT);
    }
    
    byte cnt = 0;
    
    void loop()
    {
       buf[0] = cnt++;
       usb_rawhid_send(buf, 1000);
    
       digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
       delay(500);
    }
    Output:

    Click image for larger version. 

Name:	hid.PNG 
Views:	7 
Size:	8.9 KB 
ID:	17025

  10. #10
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Quote Originally Posted by Nominal Animal View Post
    You might also consider the fact that "real time" display of data does not mean 0ms latency, because a typical display only shows a new frame every 16.667ms (at 60 Hz frame rate).
    Even audio has a latency, although it varies based on the buffer size (and OS too, I guess).

    Even if you use custom hardware, us humans tend not to notice a latency or timing discrepancy of a few milliseconds at all. So, strive for good and practical, not for perfect.
    Nobody can read a 10khz updates...
    Apart from that, you have to add the time the usb-transfer need. there is some latency, too, which depends on how many other devices are connected, the OS you use, maybe the chipset or the speed of your PC.
    Depends how you defines "realtime". For human eyes, 10HZ (- notice: not kHz) is more than enough - even that is too fast to read, not to speak of any human reaction/interaction.

  11. #11
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    133
    If one wants to get into the details of human perception, look at psychovisual and psychoacoustic modeling. Don't let the psycho scare you, is actually just modeling how humans perceive things, and is actively used when designing e.g. video and sound compression. Even "old" MP3 could use (depending on the compression software used!) psychoacoustic modeling to "hide" the quantization noise, by shaping it so that its spectrum follows the sensitivity of human hearing, yielding better sounding recordings using fewer bits.

    Frank B is absolutely right that it all depends on the definition of "realtime". There is even a measurable latency from the point where photons hit your rod or cone cells in your retina, and the corresponding perception in the brain. Simply put, if you keep your total latency under 20ms or so (a common round figure, not a fixed precise value by any means), you have scientific literature to back the claim it is still "realtime". Anything less does not matter to us humans, and is just marketing speak. Anything longer (say, 100ms or more), and it becomes a bit more complicated; depends a lot on the user interface.

  12. #12
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    Thanks. Will give it a try.

  13. #13
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    Quote Originally Posted by luni View Post
    HID is limited to 64kB /s. 5x32bit @ 10kHz gives 200kB/s that is going to be difficult :-)
    I didn't know that limitation.. so UART at 1M baud would be better?

  14. #14
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    570
    I didn't know that limitation.. so UART at 1M baud would be better?
    Yes. BTW there is no such thing as a 1M baudrate for the USB Serial. It will always transmit with the full USB speed.

    Here a (very) basic example how to read serial data from the Teensy in c#

    Code:
    using System;
    using System.IO.Ports;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                SerialPort port = new SerialPort("COM27");  // See https://github.com/luni64/TeensySharp if you need to autodetect the teensy port
    
                port.Open();          
                while (!Console.KeyAvailable)
                {
                    Console.WriteLine(port.ReadLine());
                }            
                port.Close();
    
                port.Dispose();            
            }    
        }
    }
    Teensy test code
    Code:
    #include "Arduino.h"
    
    void setup()
    {  
    }
    
    void loop()
    { 
       Serial.printf("Test: %d\n", millis());
    }

  15. #15
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,065
    Quote Originally Posted by nlambuca View Post
    I didn't know that limitation.. so UART at 1M baud would be better?
    A UART at 1M baud - like Serial1 - would end up near 100,000 bytes/sec.

    Would take non-HID USB, or faster UART rate, which Teensy can do - though not sure which one in use - Serial1 and Serial2 typically have best hardware FIFO support and 2+M Baud could work if the other end can keep up.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •