Welcome to the new FlexRadio Community! Please review the new Community Rules and other important new Community information on the Message Board.
If you are having a problem, please refer to the product documentation or check the Help Center for known solutions.
Need technical support from FlexRadio? It's as simple as Creating a HelpDesk ticket.

If You Haven't Tried the API Yet...

13

Comments

  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Please excude the wall of text below :)  

    These steps worked for me using Widows 8.1 and Visual Studio 2013 but I think it would work also with VS 2012 - very unsure about earlier versions of VS.

    Vern - you have been mentioning VITA but I'm going to keep this example simple and just explain how to create a new solution in Visual Studio and add the FlexLib and write a simple sample app that watches for the radio property changes and prints them out.  My nest post will describe how I utilized the UDP data from the radio - this just covers the radio state date:

    As Peter mentions - don't pick and choose cs files from the FlexLib - take the entire lot (Cat, FlexLib, TestFlexApi, UiWpfFramework/Util/Vita) and add all of these projects to a new solution along with your new project.  You then choose what parts of FlexLib you use by referencing those classes, calling methods, and subscribing to events.

    Download the FlexLib_API.zip
    -Create a folder to hold the contents of the zip file and unzip it there.
    -Start Visual Studio (I'm using VS 2013 but these instructions should be ok for 2012 and possibly earlier versions)
    -Click File/New Project
    -Select Other Project Types/Visual Studio Solution
    -Once the new solution is opened, go to the Solution Explorer (Right side of IDE), right click and click Add/Existing Project
    -Navigate to the first folder within the folder where you unziped the Flex Lib and select the .csproj file
    -Repeat this step for each of the folders adding each .csproj to your solution

    Note: There is a solution file (.sln file) in the Cat folder however it references a couple of projects that are not included in the zip - 
    the flexVSPInstaller - not sure why the cat project has the SLN so I'm suggesting you create a new solution for your work and add the Flexp
    projects - mainly because it is easier to debug into the FlexLib source that way and they're handy to browse when there in the same
    solution.  You could build them in a seperate project and then just reference the binaries but when you're learning I'd go this route.  I'm
    sure someone will correct me :)

    One you've added all the projects you can right click on the solution (root of the tree in the Solution Explorer pane in Visual Studio.  It will
    not build and you will get a complain that it can't find the Cat.ico file.  For some reason the Cat.csproj is looking for a copy of Cat.ico in the root of the Cat folder and also under the Cat/Cfg/Lib
    just find this Cat.ico reference in the Cat project root using the Solution Explorer and right click/delete and recompile the solution again and it should compile.  Anyone else have this issue - I downloaded the FlexLib zip only a couple of days ago.

    Create a new project for your code - right click on the solution explorer root - top most node in the explorer and select Add/New Project
    Depending on what you want to accomplish choose an appropriate project type - if you want a GUI then select Visual C#/WPF Application, if you have some experience with the older
    Windows Forms Application (Like the old VB style) then select that.  These projects also exist in the Visual Basic language too and you can use it as well.

    Now that you have your new project, navigate to it in the solution explorer (Right hand side of IDE), right click on the Referenecs node and select Add Reference.  A dialog box will open.
    Select the Solution/Project folder and check a box next to FlexLib and UiWpfFamework (even if your project doesn't use WPF, FlexLib has a class in this project that is used elsewhere so you'll
    need to add a reference to it anyway).  

    Once you have your new project created, open the .cs file in your new project - if it is a WPF app, open MainWindow.xaml.cs, if it is a console application, open the Program.cs, if it is a
    Windows Forms Application, open the Form1.cs.

    To keep things simple I'm going to explain how to add the code to a .Net console application.

    Open Program.cs in your new Windows Console Appliation project

    At the top of Program.cs, add the line "using Flex.Smoothlake.FlexLib;"  This will make the classes in the FlexLib available to your .cs file.

    Now, go to the body (between the curly braces) of the Main function in your Program.cs file and start coding - below is the complete contents of my Program.cs file that waits for a radio to be
    discovered, conects to the radio and watches all of the property changes.  You can watch the console window while you play with the radio in SmartSDR to see the values
    change.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Flex.Smoothlake.FlexLib;
    using System.Reflection;

    namespace TestFlexApi
    {
        class Program
        {       
            static void Main(string[] args)
            {
                API.RadioAdded += new API.RadioAddedEventHandler(API_RadioAdded); 
                API.ProgramName = "My Program";
                API.Init();

                Console.WriteLine("Hit return to quit");
                Console.ReadLine();
            }

            private static void API_RadioAdded(Radio radio)
            {
                Console.WriteLine("Found radio: {0}", radio.Model);
                radio.PropertyChanged += radio_PropertyChanged;
                radio.Connect();
            }

            static void radio_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                var radio = sender as Radio;

                if (radio != null)
                {
                    var val = typeof(Radio).GetProperty(e.PropertyName,
                                                    BindingFlags.Public
                                                    | BindingFlags.Instance
                                                    | BindingFlags.IgnoreCase).GetValue(radio, null);

                    Console.WriteLine("Property: {0}, Value: {1}", e.PropertyName, val);
                }            
            }
        }
    }
  • Vern
    Vern Member
    edited January 2015
    Larry & Peter:

    My oh my, gents?  I'm working.....

    Vern
  • Peter K1PGV
    Peter K1PGV Member ✭✭✭
    edited December 2016

    Our goal, Vern, is to keep you busy.  And if not busy, at least confused!!

    :-)


    Peter K1PGV

  • Vern
    Vern Member
    edited January 2015
    "Create a new project for your code - right click on the solution explorer root - top most node in the explorer and select Add/New Project".  Your comment. I do not find the Add/New Project? I am not sure what the 'explorer root - top most node ' is?

    I am using Microsoft Visual Studio Express (Free version) 2013. My only experience in programming is Delphi 6 & 7 with Windows 7. About 35 yrs of programming but never formally trained. Not an excuse!

    Should be a piece of cake to keep me confused....

    Vern
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Hi Vern, in the View menu, click "Solution Explorer" - in the Solution Explorer, right click on the item that starts with the name "Solution".
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Here is an example of creating a slice and requesting DAX audio.  I'm guessing that you should wait for the radio status property to be set to Available before trying to connect - I could be wrong :)  I'm then waiting for the SlicesRemaing property to be set so that I can know if there are still slices available to be created.  When you call Radio.Connect(), FlexLib fires off a bunch of commands to the radio to subscribe to events.  Those come in asynchronisly so you need to wait and I don't see an easy to tell when this is complete so I watch all the radio property changes come in and wait for the one I want.  In this case, if the there are any slices left.  This test app attaches itself to the first radio it discovers, creates a slice, and attaches it to DAX channel number one and then creates an audio stream on that DAX channel. 


    using System;using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Flex.Smoothlake.FlexLib;
    using System.Reflection;
    using System.Windows;

    namespace TestFlexApi
    {
        class Program
        {
            static Radio rig;
            static Slice sl;

            static void Main(string[] args)
            {
                API.RadioAdded += new API.RadioAddedEventHandler(API_RadioAdded); 
                API.ProgramName = "My Program";
                API.Init();

                Console.WriteLine("Hit return to quit");
                Console.ReadLine();
            }

            private static void API_RadioAdded(Radio radio)
            {
                if (rig == null)
                {
                    rig = radio;
                    Console.WriteLine("Found radio: {0}", rig.Model);
                    rig.PropertyChanged += radio_PropertyChanged;
                    rig.SliceAdded += radio_SliceAdded;
                }
            }

            static void radio_SliceAdded(Slice slice)
            {
                if (sl == slice)
                {
                    slice.DAXChannel = 1;
                    var audioStream = rig.CreateAudioStream(slice.DAXChannel);
                    audioStream.RXDataReady += audioStream_RXDataReady;
                    audioStream.RequestAudioStreamFromRadio();
                }
            }

            static void audioStream_RXDataReady(AudioStream audio_stream, float[] rx_data)
            {
                Console.WriteLine("DAX Channel {0}, Data: {1}", audio_stream.DAXChannel,
                    rx_data.Aggregate(string.Empty, (s, i) => s + "
    " + i.ToString()));
            }

            static void radio_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                var radio = sender as Radio;

                if (radio != null)
                {
                    var val = typeof(Radio).GetProperty(e.PropertyName,
                                                    BindingFlags.Public
                                                    | BindingFlags.Instance
                                                    | BindingFlags.IgnoreCase).GetValue(radio, null);

                    if (val != null)
                    {
                        Console.WriteLine("Property: {0}, Value: {1}", e.PropertyName, val);

                        if (e.PropertyName == "Status" && val.ToString().StartsWith("Available"))
                        {
                            radio.Connect();
                        }

                        if (e.PropertyName.Equals("SlicesRemaining"))
                        {
                            if (rig.SlicesRemaining > 0 && sl == null)
                            {
                                sl = rig.CreateSlice(14.2, "ANT1", "USB");
                                sl.RequestSliceFromRadio();
                            }
                        }
                    }
                }            
            }
        }
    }


    I recommend looking at the Radio.cs file in the FlexLib to get a feel for how it works.

    -Larry
  • Vern
    Vern Member
    edited January 2015
    I have violated your suggestions. For now I asked why not start fresh? So I set up a Console style program, It compiles except for the error noted in my program listing. Thus I am unable to run the program. A hint maybe?

    The red lines, underlining some words, shown below, in the pgm listing below, are not part of my C# program. Has something to do with the reflector behavior - I think.

    Tomorrow I will concentrate on your suggestions. Closing in on bed time. I didn't need to say that!

    myConsoleProgram:

    using System;using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks; //ERROR: -----------------------------------------------
    using Flex.Smoothlake.FlexLib; //ERROR: 'Flex' has a blue squiggly line under it.
                                                      // ERROR: land is the only error?
                                                      //ERROR: ------------------------------------------   
    using System.Reflection;
    using Peter_Larry;

    namespace TestFlexApi
    {
        class Program
        {
            static void Main(string[] args)
            {
                API.RadioAdded += new API.RadioAddedEventHandler(API_RadioAdded);
                API.ProgramName = "My Program";
                API.Init();

                Console.WriteLine("Hit return to quit");
                Console.ReadLine();
            }

            private static void API_RadioAdded(Radio radio)
            {
                Console.WriteLine("Found radio: {0}", radio.Model);
                radio.PropertyChanged += radio_PropertyChanged;
                radio.Connect();
            }

            static void radio_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                var radio = sender as Radio;

                if (radio != null)
                {
                    var val = typeof(Radio).GetProperty(e.PropertyName,
                                                    BindingFlags.Public
                                                    | BindingFlags.Instance
                                                    | BindingFlags.IgnoreCase).GetValue(radio, null);

                    Console.WriteLine("Property: {0}, Value: {1}", e.PropertyName, val);
                }
            }
        }
    }
  • James Whiteway
    edited January 2015
    Vern, the problem I see is you have not set a REFERENCE to Flexlib. To do that, you need to first Compile the Flexlib into a dll. Before you open your project, first select Open Project, from the File dropdown menu in Visual Studio. An Open File/Project popup box should appear. Select BROWSE, then go to where you unzipped the Flexlib api zip file. There will be a project file there named flexlib. Select it and click OK. Once the project loads, select Rebuild, from the BUILD menu at the top of Visual Studio. Once the project builds, (it will be fast) go to the FILE menu again, then select Close Project. Then. Select OPEN PROJECT from the File menu and select your project. Once it loads, go to the top of Visual Studio again to the Project menu, from the dropdown select ADD REFERENCE. From the popup box, select BROWSE tab navigate to the Flexlib folder where the unzipped flexlibapi files are and select flexlib.dll. Then select OK. The blue and red underlines "should" go away. And your project should run. There are other parts of the api located in the subfolders from thr zip files. To use them too, you will have to build each one and reference them the same way. Having the Flexlibapi files avaliable, helps figuring out what needs to be passed to and from the dll 's. I hope this helps. james WD5GWY
  • Vern
    Vern Member
    edited January 2015
    GM Larry

    You talked about a solution file named .sln in the Cat folder. I do not have that! Therefore I did not do any of the suggestions in that paragraph. Good or bad on my part? And I have seen no correction on your good suggestion, hi.

    Compiled the .csproj files and it was not happy. It was looking for a CAT item but not the the Cat.ico. I deleted, one by one, any ref to Cat.ico in 3 or 4 files of the CAAT folder with no success. The result of the solution build was:

    "Error 1 Metadatafile 'C:UsersVernDesktopFLEXLib2CatAsyncSocketsV2inDebugAsyncSocketsV2.dll' could not be found".

    I will next create a new project for my code while i await your comments(s).

    TU Vern



  • Vern
    Vern Member
    edited January 2015
    James: On the road again?

    I thought I may be in between a rock and a hard place with the advice coming from a couple of sources. So don't be offended - your not forgotten. After things settle with Larry's effort I will be back.

    TU Vern 
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Vern,drop me an email, I'd like to try a screen sharing session with you. hb9eyq at gmail.
  • James Whiteway
    edited January 2015
    Vern, yep on the road again! Larry's advice is good. What I wrote is what works for me. It is a bit less bloat to just add references to compiled dll's instead of adding the dll's source code directly to a project. James WD5GWY
  • Vern
    Vern Member
    edited January 2015
    OK James: I took the time to TRY to follow your instructions to create the .DLL but failed miserably! Got hung up on the  sentence  "There will be a project file there named flexlib". I see a folder buy that name but I'm wondering if that is what I use. I tried it but was troubled by the results. However I do have a couple Flexlib.dll files in an obscure folder I see after a search. How do I move one to my project to be used.Where do I put it?

    Clear as mud?

    Vern
  • N7BCP
    N7BCP Member ✭✭
    edited January 2020
    Each folder in the flexlib zip you downloaded is a separate project that includes a project file with a file extension of .csproj (stands for C# project), vb projects have a different name. You can view these in notepad if you are curios. It is a manifest of all the project's files. A group of one or more projects is called a solution and solution files have a file extension of .sln. FlexLib is a set of external libraries that are referenced by your personal project and dynamically linked (dll) at runtime. Since flex delivers the source code for the libraries as visual studio projects you can add them to your solution or you could choose to build them separately and only reference the binaries (dlls). My recommendation is to add the flex library projects to your solution and look at the flex code, there is a wealth of information that will help you understand how to use them and they are your best bet for a form of documentation. The code is annotated by flex so there are some good nuggets there. Also, when debugging, it is handy to have the flex source in your solution as you can debug right into them for even more clues as to how they function.
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Here is a link to a Microsoft article on how to work with solutions and projects in visual studio: https://msdn.microsoft.com/en-us/library/vstudio/ff460187(v=vs.100).aspx
  • Vern
    Vern Member
    edited July 2017
    Here is the results of trying to do as you suggest. 5 projects with everything but the .csproj files. I tried to get them in the list but failed.  I didn't find a way of altering the line up of the projects on the display.

    Is this what you had in mind? All associated projects, etc, are handy for review.

    Vern
  • James Whiteway
    edited January 2015
    Vern, I think Larry's suggestions are sound. I should not have posted and confused things for you. James WD5GWY
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Hi Vern, drop me an email and we can arrange a screen sharing session.
  • Vern
    Vern Member
    edited January 2015
    I have tried a couple times to send my email address but my records say it didn't happend. Did you get them?

    Vern
  • N7BCP
    N7BCP Member ✭✭
    edited November 2016
    Negatory, my address is hb9eyq at gmail dot com.
  • Vern
    Vern Member
    edited January 2015
    Larry, you can also find my email address on QRZ.COM.
  • Vern
    Vern Member
    edited January 2015
    It is running showing among other things latitude/longitude/altitude. Now to trace through the pgm for a better understanding.  

    My thanks to you, Larry and ALSO James who is on the road again.  James showed me the way to making and loading the necessary .dll's.

    Vern
  • k3Tim
    k3Tim Member ✭✭✭
    edited December 2016
    Hey Peter,

    Many thanks for posting the above code! With the above and help of my twin (Mr. Windows UI) we (he in actuallity) was able to get it working.

    There was one exception thrown due to an illegal cross threading.  The following line of code:

     CheckForIllegalCrossThreadCalls = false;

    was copied from:

    Form1_Load()

    to:

    API_RadioAdded()

    and then no exception occurred. Running on Visual Studio 2015, C#. As it looks like Form1_Load is not called does anyone know is this due to change in WPF or something else?

      _..--
    k3Tim

  • James Whiteway
    edited December 2015
    It may be a setting you have in Visual Studio conflicting with the illegal crossthreads setting. It really should be set to "true" in order to catch threading problems.
    I have run Peter's code (and used it as a base to start my own program) and it does work. I have run it in VS 2010 and VS 2015. It could be an issue with WPF, if you are using this in a WPF program instead of a WinForms app.
    If you are using Winforms, Form load has to load in order for your program to work.
    james
    WD5GWY

  • k3Tim
    k3Tim Member ✭✭✭
    edited December 2016
    Hi  James

    I'll check that out.  The top level installer / project file did not work since VS 2015 was several editions removed from the project version  used for the Flex API.

    Than ks

    Tim
  • Peter K1PGV
    Peter K1PGV Member ✭✭✭
    edited December 2016
    James us correct. The example I posted us a WinForms app, and a WPF app. Disabling the check for illegal cross-threading updates is expeditious, but not "the right thing to do" for production code. We've discussed this several times here in the forum previously. Glad to hear you've taken the dive into the SPI and FlexLib. It really IS fun, easy, and can be super useful! Peter K1PGV
  • Bill W2PKY
    Bill W2PKY Member ✭✭
    edited December 2016
    Hello Steve, has FRS considered developing a programming seminar to be presented at Ham Fests where FRS is attending? I.E. Orlando
  • Dan -- KC4GO
    Dan -- KC4GO Member
    edited November 2016
    Count me in....

    Dan --- KC4GO
  • Walt
    Walt Member ✭✭
    edited December 2016
    And please POST a video ( and any software files) of this for the rest of us that can't afford to go to these hamfests - or need to hear it a few times over before it sinks in.

    Cheers 
  • Vern
    Vern Member
    edited January 2016
    Peter, I'm a struggler! I am unable to make use of FlexLib -- get it to play along with your sample code (maybe massaged by James). I am getting the 4 FlexLib files to show in the Solution Explorer references (not the CAT file). But it does not compile. Using Visual Studio 2012 (& 2015). I put your file in a blank page. Is it a page? I remove everything on the page, including the using statements and then paste your program onto the page. It now has your using statements, etc. Then I compile. Lots of errors. I try to remove the most errors I can but never get them all. On a scale of 1 to 10 my understanding of C# is 1 or less. Done lots of reading. I have given you only tidbits to think about if you think about it at all. I may be closer than I think to success!!

    Vern  

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.