Implementing TeensyLoader into a C# app?

Hi,

thank you for your help. I have a quastion about your new repo.

Is there a quick way to read the COM port name?

Code:
foreach (var teensy in watcher.ConnectedTeensies)
{
      portCOM_comboBox.Items.Add(teensy);
}

But this reads all data about connected teensy. I only need COM port number
 
E.g.
Code:
foreach (var board in watcher.ConnectedTeensies.Where(t => t.Ports.Any())) //loop through all connected boards which expose at least one COM port
{                                                                            
   foreach (var port in board.Ports) // loop through all COM ports of the current board                    
   {
       Console.WriteLine(port);
   }
}

The example takes care of the fact that not all Teensies expose a COM port and that a Teensy can expose up to 3 COM ports.
 
Hello,
Firstly, thank you, luni, for all the work with TeensySharp library. A great tool.
I made my first app to update firmware on teensies using .NET framework and windows forms. The problem is that when the app is opened, the the PC grinds into the halt. Task manager showing "WMI Provider Host" and "System Interrupts" using all the the CPU.
Am I doing something wrong? I am using pretty much the win forms example in the git repository.

Code:
var Watcher = new TeensyWatcher();
var Board = PJRC_Board.Teensy_35;
var FlashImage = SharpUploader.GetEmptyFlashImage(Board);

OpenFileDialog v1 = new OpenFileDialog();
v1.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*";
v1.FilterIndex = 0;

if (v1.ShowDialog() == DialogResult.OK)
{
         SharpHexParser.ParseStream(File.OpenText(v1.FileName), FlashImage);
         USB_Device Teensy = Watcher.ConnectedDevices.FirstOrDefault();
         SharpUploader.StartHalfKay(Teensy.Serialnumber);
         int result = SharpUploader.Upload(FlashImage, Board, Teensy.Serialnumber, reboot: true);
}
 
The code you showed does look normal, but of course it is only a tiny snippet. To double check I just tried the WinForms example and it runs without an issue. Can you upload your project somewhere (e.g. GitHub) so that I can have a closer look?

Edit: Just realized that you are using the synchronous functions (I thought I disabled them..) You need to use the async versions as shown in the example.
Edit2: Sorry for the crappy answers so far. It looks like you are using a very old version of the library which doesn't work since a change in the USB descriptors a year or so ago. You need to use the current version from GitHub. The API changed significantly. Here is the relevant code from the updated WinForm example (Upload button)

Code:
private async void btnUpload_Click(object sender, EventArgs e)
{
    var teensy = lbTeensies.SelectedItem as ITeensy;
    if (teensy != null)
    {
        string filename = tbHexfile.Text;
        if (File.Exists(filename))
        {
            var progress = new Progress<int>(v => progressBar.Value = v);
            progressBar.Visible = true;
            var result = await teensy.UploadAsync(filename, progress);
            MessageBox.Show(result.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            progressBar.Visible = false;
            progressBar.Value = 0;
         }
         else MessageBox.Show("File does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
}
 
Last edited:
Yes, I definitely need to update that nuget package....

I look forward to it. Getting github repository into my visual studio project seems to be outside my capabilities. I never managed to get along with github.
 
Hi,
I've used the new version of your library successfully with a Teensy 3.6 - thank you - but I'm having problems with a 4.1 when uploading a HEX file - Upload is returning 1. As a test, I'm simply trying to upload Blink (compiled for 4.1 with Serial + MIDIx4) onto a 4.1 already running the same program. The serial number is correct, I've no problem uploading from the Arduino IDE, and if I press the Program button then my code does upload correctly.
All the best,
Alan
 
I can reproduce that with the "WinForms Uploader" example. Seems to be related to the Serial+MIDIx4 Usb mode. I can upload if I first switch the board to bootloader (using the bootloader button in the example or by pressing the button on the board) and then upload. Can you confirm this?
 
Reason is that the Teensies in MIDI mode don't communicate the board type. Thus the check for compatible firmware fails and uploading errs with the "Upload_FirmwareMismatch" you observed. As workaround I added code to start the bootloader in this case which fixes the upload issue. This is not a big deal when uploading since the bootloader must be started anyway. However, that fix won't work for simply listing the connected Teensies. IIRC @Koromix (tyTools) works around this by maintaining a local database storing the type of those boards together with the s/n once it is known. Might be a solution here as well.
 
Thanks,
I'm not using your code to find the Teensy so that's not a problem,
All the best,
Alan
 
Back
Top