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

Thread: Custom WIN Program for Teensy

  1. #1
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21

    Custom WIN Program for Teensy

    Hi All

    Does anyone know if its possible to create a custom Windows program that can 1. Read analog inputs, 2.write to the Teensy EEPROM??

    Ive searched but havent found anything. It wod seem a bit odd if you cant do this?

    Thanks in advance.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    372
    You can do that if you write the Windows and Teensy programs.

  3. #3
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21
    How do you mean?

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    372
    You write a Windows program to send commands to Teensy and you write a Teensy program to read the Windows commands and react accordingly.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,605
    Here some info and example programs (Win10 / Teensy) showing how to communicate with a PC via USB-Serial https://github.com/TeensyUser/doc/wiki/Serial. And here examples showing how to use the RawHID interface: https://github.com/TeensyUser/doc/wiki/Raw-HID.

  6. #6
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21
    Thanks luni, this looks promising...

  7. #7
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21
    Hi

    I have made some progress. I have managed to write a basic C# program that will turn ON and OFF pin 13 LED on a Teensy 3.5 using the USB Type: Serial. Its a breakthrough, I'm really happy with myself ha ha.
    However, because I'm building a midi project I need my C# program to work when I program Teensy using the USB Type: Serial + Midi in the Teensy Duino platform and currently it doesn't work. Can anyone help me out with this?

    TEENSY Code:
    Code:
    void setup() {
      Serial.begin(9600);
      pinMode(13, OUTPUT);
    }
    
    void loop() {
    
    if (Serial.available()){
      int state = Serial.parseInt();
    if (state == 1){
      digitalWrite(13, HIGH);
      Serial.println("Command completed LED turned ON");
    }
    if (state == 2){
      digitalWrite(13, LOW);
      Serial.println("Command completed LED turned OFF");
    }
    }
    C# Win Application:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            private SerialPort myport;
            
            public Form1()
            {
                InitializeComponent();
                init();
                
            }
    
            private void ON_btn_Click(object sender, EventArgs e)
            {
                myport.WriteLine("1");
    
                ON_btn.Enabled = false;
                OFF_btn.Enabled = true;
            }
    
            private void init()
            {
                try
                {
                    myport = new SerialPort();
                    //myport.BaudRate = 9600;
                    myport.PortName = "COM5";
                    myport.Open();
                }
                catch (Exception)
                {
                    MessageBox.Show("Error!!");
                }
    
                //
                ON_btn.Enabled = true;
                OFF_btn.Enabled = false;
    
            }
    
            private void OFF_btn_Click(object sender, EventArgs e)
            {
                myport.WriteLine("2");
    
                ON_btn.Enabled = true;
                OFF_btn.Enabled = false;
            }
        }
    }

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,881
    Quote Originally Posted by GiGiUK View Post
    Hi

    I have made some progress. I have managed to write a basic C# program that will turn ON and OFF pin 13 LED on a Teensy 3.5 using the USB Type: Serial. Its a breakthrough, I'm really happy with myself ha ha.
    However, because I'm building a midi project I need my C# program to work when I program Teensy using the USB Type: Serial + Midi in the Teensy Duino platform and currently it doesn't work. Can anyone help me out with this?

    TEENSY Code:
    ...
    C# Win Application:
    ...
    Nice it worked ... to start ...

    Quick glance and not sure what "it doesn't work" means?

    Has the COM# changed perhaps and it isn't at the coded "COM5"?

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,722
    I have a teensy sketch which uses MIDI and Serial. When it is running, it uses COM13 (plus there's a "Teensy MIDI" MIDI port as well). But if I run a program which only uses Serial, that uses COM9.
    If you hard-code your C# program to use COM5 for just Serial, it should probably be looking for a different COM port when you compile it for Serial+MIDI.

    Pete

  10. #10
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21
    O defragster, you little beauty and I'm a muppet. COM port changed to COM14...........pfff.

    It seems that after I program the Teensy, it automatically changes COM port, seems bizarre to me.

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,881
    When the USB TYPE changes it becomes a NEW device and gets a NEW COM# association.
    Glad that got it to working!

  12. #12
    Member
    Join Date
    Apr 2020
    Location
    HyŤres, France
    Posts
    21
    Thanks Pete, ill take note of that.

  13. #13
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,316
    If you have only one teensy on your PC, you can program your PC to search for Teensy and find the actual port.
    Have a look into https://github.com/PaulStoffregen/te...y_loader_cli.c to get some idea how it could be done.

  14. #14
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    14,881
    That links to https://www.pjrc.com/teensy/hid_listen.html

    Also the @luni p#5 code above likely has 'find teensy device' code

  15. #15
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,810
    Quote Originally Posted by GiGiUK View Post
    it automatically changes COM port, seems bizarre to me.
    Windows tries to associate a unique COM port number with each USB serial device you own. They're assigned in the order you plug them in. If you and your friend own the same 3 devices, and you plug device A in first, it becomes COM3 on your PC. Then if you plug in device B, it becomes COM4, and device C becomes COM5. The idea is if you unplug then all then reconnect 1 or more, say device C only, it (should) always show up as COM5.

    If your friend with the same 3 devices plugs in device C first, it becomes COM3 on their machine. That association between the USB descriptor info which uniquely identifies that particular device and which COM port it always gets assigned is stored in your local Windows Registry.

    You might expect Teensy would always get the same COM port number. And it should, on the same PC with the same Windows Registry info, but only when all the USB identification remains identical. If you change Tools > USB Type to cause Teensy to implement a different set of interfaces, or if you edit usb_desc.h & usb_desc.c, as far as Windows can tell you have just plugged in a completely different USB device. Windows can't know it's physically the same Teensy, and it most certainly should not guess any such thing. The USB descriptor info says it is now something else. As far as Windows knows, you've just bought a completely different USB product.

    Sadly, this process isn't flawless. I don't know if it's bugs in Windows or something gets corrupted in the Windows Registry, or a result of changes made to other devices causing conflicts, but we have occasionally seen cases where Windows gets rather confused about COM port number assignments. But those are the exception where things go wrong.

    Windows is indeed supposed to assign a new COM port number when it sees a new unique USB device. When you change USB types or edit the descriptors, to Windows it's a completely different USB device.

Posting Permissions

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