Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 38 of 38

Thread: Implementing TeensyLoader into a C# app?

  1. #26
    Junior Member
    Join Date
    Sep 2021
    Posts
    6
    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

  2. #27
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    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.

  3. #28
    Junior Member
    Join Date
    Sep 2021
    Posts
    6
    Thank you. Everything is working great!

  4. #29
    Junior Member
    Join Date
    Nov 2021
    Location
    UK
    Posts
    4
    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);
    }

  5. #30
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    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 by luni; 12-04-2021 at 10:19 AM.

  6. #31
    Junior Member
    Join Date
    Nov 2021
    Location
    UK
    Posts
    4
    Thanks, luni for a quick reponse. I was using a nuget package. I will try the github one.

  7. #32
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    Yes, I definitely need to update that nuget package....

  8. #33
    Junior Member
    Join Date
    Nov 2021
    Location
    UK
    Posts
    4
    Quote Originally Posted by luni View Post
    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.

  9. #34
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    Here you are https://www.nuget.org/packages/lunOp...ibTeensySharp/ Since the API is rather different from the old version I decided to rename the library to lunOptics.libTeensySharp. So, you need to uninstall the old package first if you still have it installed. Let me know if it works for you.

  10. #35
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    81
    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

  11. #36
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    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?

  12. #37
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,834
    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.

  13. #38
    Member
    Join Date
    Feb 2020
    Location
    Dublin, Ireland
    Posts
    81
    Thanks,
    I'm not using your code to find the Teensy so that's not a problem,
    All the best,
    Alan

Posting Permissions

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