uploading hex file via teensy loader

paoloboatto

Active member
I'm trying to upload a new firmware to Teensy 4.1 from a c# application. I can reboot the Teensy running teensy_reboot.exe and then load the new hex file running teensy.exe. The problem is that the Loader uses the file located into the Arduino temp folder, no matter what other file name I use under the -file and - path options (as an example I use -file=VRBox.ino -path=C:\Users\paolo\Desktop\tool). I also used the string Arduino uses when compiling the sketch (C:Users\paolo\Desktop\tools\teensy_post_compile -file=VRBox.ino -path=C:\Users\paolo\Desktop\tools -tools=C:\Users\paolo\Desktop\tools -board=TEENSY41 -reboot -port=usb:0/140000/0/2/3 -portlabel=hid#vid_16c0&pid_0478 Bootloader -portprotocol=Teensy) without success (I have copied both the hex file and all the Teensy tools under C:\Users\paolo\Desktop\tools to reduce the path length). The error says that it doesn't find the specified path (that exists) and pid_0478 is not recognized.
Any idea on how to assign the hex file name to the teensy.exe?

Thank you
Paolo
 
Just foud that there was a wrong backslash in the string. Now it works even if it still raises an error regarding pid_0478 option
 
the error is in italian:
"pid_0478" non è riconosciuto come comando interno o esterno, un programma eseguibile o un file batch.
 
unfortunately when downloading the firmware through the Arduino IDE no error is raised. The error is shown when running the command from a DOS prompt.
 
I'm not very familiar with Windows. I mostly use Linux. On Linux, certain characters are special and need quotes so the command line doesn't parse them and instead passes the info directly to the program. Maybe Windows command line is similar?

Maybe if you show a screenshot of the command window with the exact text you entered and the error message, perhaps people here who use Windows will be able to see the problem?
 
Code:
C:\Users\Marc>echo portlabel=hid#vid_16c0&pid_0478
portlabel=hid#vid_16c0
'pid_0478' is not recognized as an internal or external command,
operable program or batch file.

You will need to escape that string before running it as a Windows command - the & symbol must be prefixed with a ^:

Code:
C:\Users\Marc>echo portlabel=hid#vid_16c0^&pid_0478
portlabel=hid#vid_16c0&pid_0478


Marc
 
I have another question: the string contains the USB port details i.e -port=usb:0/140000/0/1. However this port can change and I don't know how to find it under windows. As an example when downloading the firmware onto ES32 the string contains the port name explicitly (i.e. COM6, COM15, etc..) and It's easy to know that name from a C# application.
Paolo
 
Since you are doing a c# application you might be interested in TeensySharp.


Getting a list of currently connected Teensies is a simple as this:

C#:
 var Watcher = new TeensyWatcher();
 foreach (var Teensy in Watcher.ConnectedTeensies)
 {
     Console.WriteLine($"Found {Teensy.Description}");
     Console.WriteLine($"  Serial number: {Teensy.Serialnumber}");
     Console.WriteLine($"  Board type:    {Teensy.BoardType}");
     Console.WriteLine($"  Ports:    ");
     foreach (var port in Teensy.Ports)
     {
         Console.WriteLine($"    - {port}");
     }

     Console.WriteLine();
 }

The TeensyWatcher updates the list of teensies on connection / disconnection, switch to bootloader etc. See the "Print&Watch" example for seeing how to subscribe to the change event of the watcher.

You can also use TeensySharp to upload firmware. Here a very simple example.

C#:
  var teensy = watcher.ConnectedTeensies.FirstOrDefault();
  if (teensy != null)
  {
      var firmware = Path.Combine(Path.GetTempPath(), "MRKIV.hex");
      var result = teensy.UploadAsync(firmware, reboot: true);
      WriteLine(result);
  }

There are also WinForms and WPF examples in the repo
 
Back
Top