Windows Communication to Teensy (LC Specifically) via USB Serial...

Brion Sohn

Well-known member
So I am really new to this and I am trying to find information on how to program on the windows side of a program and send data to a Teensy over the USB Serial.. I have never really programed on windows and have looked all over the internet for resources/examples of how to do this properly.. For example what sort of thing would I need to do to check for the teensy being connected to USB Serial.. Most of the Examples I have found are for Hardware UART serial.. So I am a bit confused.. But I guess the main thing is trying to find examples of a Windows Side Program that will Open a com port to the Teensy over USB Serial much like you do to send terminal data in Teensyduino.. but be able to automatically search it out..

Of course you can just say go figure it out.. but that is what I am trying to do through examples.. just can't find the examples..

Thank you in advance.
 
There are a lot of possibilities on the windows side. I personally recommend to use c# and the (free) VisualStudio community edition. Using a modern high level language and IDE is very productive on a PC where you don't need to fiddle around with optimizations etc. But, of course there are other options as well (Python, Java, c++...)

Here a quick "hello world" example showing how to use C# to communicate with a teensy (c# console application).

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();           
        }    
    }
}

And here the sending code on the Teensy

Code:
void setup()
{  
}

void loop()
{ 
   Serial.printf("Test: %d\n", millis());
}
 
@Brion - Excellent question. Many beginners would be asking the same. @luni - Excellent reply, maybe add a snippet for reading incoming characters on a Win PC?

Does anyone have source for a simple Win terminal emulator in C/C++? I wrote mine in Delphi/Pascal so its a bit dated. I employed a COMPORT component, which is the easy route. I still find it very useful when I want to fire up a Teensy application because it loads so much faster than the Arduino IDE.
 
Thank you guys and than you Luni for the example and the embedded link to the searching.. I thought I replies with a thank you a bit ago but for some reason it didn't post.. anyway that is exactly the sort of thing I was looking for but what having an impossible time finding.. the incoming character Snippet that TelephoneBill mentioned would of course be extremely helpful as well but I think I can probably figure that one out without too much issue.. I was thinking that communicating with the teensy was much more complex than it seems it is.
 
If I understand that is a Windows PC console app?

What seems wrong is " while (!Console.KeyAvailable) " should the ! Not be there?
 
Does anyone have source for a simple Win terminal emulator in C/C++? I wrote mine in Delphi/Pascal so its a bit dated. I employed a COMPORT component, which is the easy route. I still find it very useful when I want to fire up a Teensy application because it loads so much faster than the Arduino IDE.
If I need a quick "terminal" I use tyCommander. It also integrates nicely into the Arduino IDE. End of last year Koromix was talking about extending it to a vt100, but it looks like this will take a while.

What seems wrong is " while (!Console.KeyAvailable) " should the ! Not be there?
No that is correct. It loops, reading strings from the com port until you press a key on the PC. Then Console.KeyAvailable becomes true, the loop breaks, the com port is released and the app quits.

Snippet that TelephoneBill mentioned would of course be extremely helpful as well but I think I can probably figure that one out without too much issue..
I can provide a simple example later today.
 
Last edited:
Ok as promised, here a simple snippet showing how to send commands from the PC to the Teensy. Pressing 'L' on the PC keyboard will switch the Teensy LED on, O (letter, not number) will switch it off. ESC quits the PC app.

PC Code (C# console application)

Code:
using System;
using System.IO.Ports;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort("COM6");  // See https://github.com/luni64/TeensySharp if you need to autodetect the teensy port
            port.Open();

            Console.WriteLine("Press L to switch LED on, O to switch it off, ESC to quit");

            var key = Console.ReadKey(true).Key;
            while (key != ConsoleKey.Escape)
            {
                switch (key)
                {
                    case ConsoleKey.L:
                        Console.WriteLine("Switching LED On");
                        port.Write("L");
                        break;

                    case ConsoleKey.O:  
                        Console.WriteLine("Switching LED Off");
                        port.Write("O");
                        break;

                    default:
                        Console.WriteLine("Wrong key. Please use L to switch LED on, O to switch it off, ESC to quit");
                        break;
                }
                key = Console.ReadKey(true).Key;
            }

            port.Close();
        }
    }
}

Teensy code
Code:
void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    while(Serial.available())
    {
        char c = Serial.read();
        switch (c)
        {
        case 'L':
            digitalWriteFast(LED_BUILTIN, HIGH);
            break;

        case 'O': 
            digitalWriteFast(LED_BUILTIN, LOW);
            break;
        }
    }
}
 
Back
Top