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.

Connect Raspberry Pi to Flex 6K

Steve - K5FR
Steve - K5FR Member ✭✭
edited June 2019 in SmartSDR API
I'm looking for some example code to connect a RPi 3B+ to my 6700 using either FlexLib or the Ethernet API. For the time being I want to use the RPi programming environment for generating the code (IDLE/NANO). Ultimately, I want to be able to read/set freq, mode, filter, etc.

Answers

  • AE0MW
    AE0MW Member ✭✭
    edited June 2018
    I started on a project like this a year or so ago, but it got pushed by the wayside. There's no FlexLib but the Ethernet API worked quite well.

    My suggestion would be to skip discovery packets and hard code your IP to get things going quickly (I remember discovery frustrating me).
  • Steve - K5FR
    Steve - K5FR Member ✭✭
    edited April 2019
    Thanks for the reply. I want to do the discovery just for the challenge as this whole project is for learning/playing, not trying to accomplish anything in particular (yet).
  • Mark WS7M
    Mark WS7M Member ✭✭
    edited April 2019
    Steve,

    You did not mention what OS you plan to run on your Pi?   I have several Pi doing various things.  All run the Raspian Linux.   

    I have another Linux box that I am currently writing a flex connect system using standard ol C and C++.

    I have a friend Linux pro who bought my flex 6500.  He will be writing some python stuff.

    I do have some C stuff given to me by another ham that I have not extracted the pieces from but it was designed to read flex frequency an control some antennas and tuners.  I could probably get a zip of that to you.  Again it is destined for C and Linux
  • AE0MW
    AE0MW Member ✭✭
    edited June 2018
    Mark, I'd be very interested in that last part. What I did was just some perl scripts but it would be nice to get back into good ole C.
  • Mark WS7M
    Mark WS7M Member ✭✭
    edited June 2018
    Ok let me package it up and I'll get it put somewhere and post a download link
  • Steve - K5FR
    Steve - K5FR Member ✭✭
    edited June 2018
    Using Raspian with the Pixel desktop. I would also be interested in any sample code.
  • Wayne
    Wayne Member ✭✭
    edited June 2018
    Mark, Id be interested also I have an unused Pi3 sitting in the drawer just waiting to do something ....
  • Clay
    Clay Member ✭✭

    Steve,

    About 3 years ago I had a new Raspberry 3B+, a Flex 6000 and a desire to learn more about both. So, I started exploring the Ethernet API, on the Raspberry Pi. It is still a work in progress. But, it has evolved into a stand alone controller with a spectral display, waterfall, 2 slice receivers, band, mode, filter controls, physical VFO tuning knob and controls, keypad etc.

    I have posted a description of the project online:

    http://ab9a.net/flex_control/flex_control.htm

    There is discovery code there, in both Python and C, that will connect to the first Flex Radio it finds on the local network, read parse and print the radio’s connection information along with the status messages returned from the radio.

    I am using the Geany IDE, that comes with the Raspian Linux distribution, for code development. You can copy and paste the discovery code from the website into Geany, save it as some_file_name.py (Python) and run it, or some_file_name.c ( C ) and compile it then run it. Both programs can also be run from the linux terminal and will print the results to the terminal.

    73,

    Clay, AB9A

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Wow Clay, this is fantastic! I was just looking for a Raspberry Pi to play with, and now I think I will start here! I was planning on using FlexLib with C# as I was able to get that working on my laptop. I think porting the code to the Pi will be fairly straightforward.

    Here is what I have, just playing around (first attempt at C#...):

    It uses discovery, but not SmartLink. It does not connect as a GUI Client, and therefore does not get audio or panadapter data. It does connect (bind) to whatever GUI client it finds first, and if there is more than one GUI client connected, it gives you a choice of which one to bind to. It has an option to show when you are out of band for your license class (US Only...) and puts spots on the screen to let you know.

    I don't know anything about UDP, so I have not (yet) played around with audio or waterfall data. If (when) I get a Raspberry Pi, will start down that path.

    Thanks for making this available - I can't wait to see what I can do here.

  • Clay
    Clay Member ✭✭

    Hi Len,

    I can't comment on cross development in C#. Or, the FlexLib. I haven't used either. I've been doing C development directly on the Raspberry Pi, using the Geany IDE. And, I'm only using the Ethernet API commands, not trying to port FlexLib.

    I chose C because I'm familiar with it. And, because the GTK3+ GUI that I'm using on the Pi is also programmed in C.

    What you've done so far is impressive. Keep us updated on your progress.

    73,

    Clay, AB9A

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Hi Clay, Well I ordered a Raspberry Pi 4 yesterday. Hopefully, I can transfer what I learned building my TeensyMaestro over to this platform.

    The TM is coded in C++ using the Arduino library from IW7DMH, but he did not make provision for UDP, other than for discovery packets.

    I have glanced through the Vita-49 documentation, but at this point it is totally overwhelming. Once I get in and play around with this, it will probably make more sense.

    With something new to play with, maybe I will stop some of the scope-creep on my TM! I will release my latest update this coming week (I added a two position mic selection switch that changes mic profile based on switch position).

  • Clay
    Clay Member ✭✭

    Hi Len,

    Congratulations on your new RPI 4. I think you will be blown away by the capability of this little Linux PC vs what you paid for it.

    I’ve read G3WGV’s tutorial at least 5 times and occasionally still refer to it.

    Network Sockets, in any flavor of C, are somewhat arcane. But, the good news is you only need to create 3 of them:

    One UDP Socket for Discovery.

    One UDP Socket for Vita_49 Packet Streaming.

    One TCP Socket for Sending Commands to and Receiving Messages form the Radio.

     Everything else is either reading from, writing to of manipulating data from the Socket Buffers.

    The Discovery C source code on the website:

    Creates a UDP Socket for reading the network discovery packet.

    Extracts the Radio”s IP Address.

    Formats and prints the discovery data buffer.

    Closes the Discovery Socket.

    Creates a TCP Socket.

    Reads and prints the TCP Socket data buffer.

    Closes the TCP Socket.

    The terminal printout should match G3WGV’s tutorial pretty closely.

    If you can get that to compile and run on the RPI 4, you’re off to a good start.

    73,

    Clay, AB9A

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Thanks Clay, I appreciate all of this! I am slowly catching on to C and its variants, and very slowly catching on to networking (sockets, et al). My professional programming dates back to the mid 70s using Fortran, PL/I and COBOL. Somewhere along the line I picked up Visual Basic, and now very recently, I finally started working in C++ and C#. If I ask oddball questions, you'll know why... If you look at my code and wonder what I could possibly be thinking, you'll know why...

    It is insights from people like you and Enzo, IW7DMH that have propelled me into the things I could not have done otherwise, and I really appreciate it! My Pi is scheduled to arrive on Tuesday, so I plan on diving right in. Watch out for goofy questions once I get started. 😁

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Well, I got my Raspberry Pi and got it set up without any issues. I am able to use Visual Studio 19 to build C# forms apps and run them on the Pi. So far so good.

    My app that uses FlexLib (seee the screenshot a few posts back), does not work unless I comment out the Flex API statements (and of course, and references to them).

    These commands work perfectly in the Windows 10 environment, but not in the RaspberryPi environment. I am on version 11 (buster) of Raspian. Here is the error message I get. Has anyone else tried this successfully? I am using mono to execute this code on the Pi.

    Unhandled Exception:

    System.TypeInitializationException: The type initializer for 'Flex.Smoothlake.FlexLib.API' threw an exception. ---> System.EntryPointNotFoundException: QueryPerformanceFrequency assembly:<unknown assembly> type:<unknown type> member:(null)

     at (wrapper managed-to-native) Flex.Util.HiPerfTimer.QueryPerformanceFrequency(long&)

     at Flex.Util.HiPerfTimer..ctor () [0x00018] in <930298692e694df6aa2b5e2e4e223b00>:0 

     at Flex.Smoothlake.FlexLib.API..cctor () [0x00022] in <aab47fc003394bcfbca1e3d0f482baab>:0 

      --- End of inner exception stack trace ---

     at FlexRadioFlexLib.frmMain..ctor () [0x000b6] in <e967be6edde543a59a3d0806b3e2e839>:0 

     at (wrapper remoting-invoke-with-check) FlexRadioFlexLib.frmMain..ctor()

     at FlexRadioFlexLib.Program.Main () [0x0000e] in <e967be6edde543a59a3d0806b3e2e839>:0 

    [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Flex.Smoothlake.FlexLib.API' threw an exception. ---> System.EntryPointNotFoundException: QueryPerformanceFrequency assembly:<unknown assembly> type:<unknown type> member:(null)

     at (wrapper managed-to-native) Flex.Util.HiPerfTimer.QueryPerformanceFrequency(long&)

     at Flex.Util.HiPerfTimer..ctor () [0x00018] in <930298692e694df6aa2b5e2e4e223b00>:0 

     at Flex.Smoothlake.FlexLib.API..cctor () [0x00022] in <aab47fc003394bcfbca1e3d0f482baab>:0 

      --- End of inner exception stack trace ---

     at FlexRadioFlexLib.frmMain..ctor () [0x000b6] in <e967be6edde543a59a3d0806b3e2e839>:0 

     at (wrapper remoting-invoke-with-check) FlexRadioFlexLib.frmMain..ctor()

     at FlexRadioFlexLib.Program.Main () [0x0000e] in <e967be6edde543a59a3d0806b3e2e839>:0 

  • Clay
    Clay Member ✭✭

    Hi Len,

    It appears like your program is trying to use the FlexLib library and can't find it.

    I put "linux" in the community search bar. And, found a number of posts discussing issues with trying to use the FlexLib library in a linux environment.

    73,

    Clay, AB9A

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Oh, geez... I keep looking up Unix and not Linux. I guess that is giving away my age...

    I think it finds FlexLib, but there is something in there that is incompatible. One of the other threads mentioned something about a stopwatch instead of HiPerfTimer which the error message references. The link is a bit over my head, so I will have to digest it a bit more. The FlexLib library works really well on Windows, so hopefully it will be a small adjustment to get it to work on the Pi.

    I did a quickie test C# forms program and it loaded it on the Pi. It discovers the radio and connects to it, but nothing else. I need to learn more about C# and TCP, but I should be able to progress using the API instead of FlexLib. It has a runtime error that does not let it open a UDP socket if another GUI client is already connected to the radio, so I need to figure that out as well.

    Definitely drinking from the fire hose at this point!

  • Clay
    Clay Member ✭✭

    Hi Len,

    According to G3WGV's tutorial, if you have more than one client connected, each client needs to have a unique UDP Port number. Just something to double check.

    Hope that is helpful.

    73,

    Clay, AB9A

  • MarkH
    MarkH Member
    edited November 2021
    removed
  • Mark_W3II
    Mark_W3II Member ✭✭✭

    Hello developers and fellow Flexers,

    The FlexAPI is built for the .NET Framework which only runs on Windows. .NET Standard, .Net Core, .Net5 and .Net6 have variants which run on Intel and ARM based Linux and OSX operating systems. This link provides the available version of .NET and the platforms they support. https://dotnet.microsoft.com/download/dotnet

    So in order to run the FlexApi natively on these non-Windows operating systems you will need to port the FlexAPI to .NET Standard, .Net 5 or 6. I did this port several years ago for my FRStackWebApi project.

    My FRStackWebApi offering provides some of the FRStack V3 functionality and REST apis on RPi, OSX and Linux platforms. So if you installed FRStackWebApi you could use its REST APIs to control your radio. Here is the link for more information https://www.mkcmsoftware.com/download/FRStackWebApiReadme.html

    I am investigating the possibility of making my port of the FlexAPI public but that may not be possible so in the meantime feel free to install FRStackWebApi service / daemon and use the Rest API.

    As mentioned above above you could always use the TCP interface directly. http://wiki.flexradio.com/index.php?title=SmartSDR_TCP/IP_API

    73, Mark W3II

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Thanks for all the info, I really appreciate it! I decided to go back to my original tack of using the Flex TCP API instead of FlexLib. I found a sample program that does a client and server demo. I got rid of the server stuff and modified the client a bit, and now have API communication with the Flex. I ported it over to the Raspberry Pi and it works!

    Once I get a few more controls working, I will start experimenting with an Audio Stream and then a panadapter like Clay did.

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    I made some progress. I got Discovery figured out, so it finds the radio, connects and binds to the first GUI client it sees. A check list box allows selecting another GUI client if one appears. This is all working great.

    Now I am trying to listen for the UDP Vita-49 meter packets, but so far, no luck. I open an available UDP port (4999 in my case) and it continues to show me discovery packets, not meter packets.

    These API commands are sent and accepted by the radio:

          client.SendMessage("CD12 | sub meter 16" + NL); // PA Volts

          client.SendMessage("CD13 | sub meter 22" + NL); // PA Temp

          client.SendMessage("C14 | client udpport 4999" + NL);

    Here is a code snippet:

        private void StartListening()
        {
          if(frmPaestro.FlexIP == null)
          {
            ar_ = udp.BeginReceive(Receive, new object());
          }
          else
          {
            ar_ = udp.BeginReceive(ReceiveMeter, new object());
          }
        }
    
        private void Receive(IAsyncResult ar)
        {
          IPEndPoint ip = new IPEndPoint(IPAddress.Any, PORT_NUMBER);
          byte[] bytes = udp.EndReceive(ar, ref ip);
          string message = Encoding.ASCII.GetString(bytes);
    
          Console.WriteLine("########################################################################");
          Console.WriteLine(message);
          Console.WriteLine("########################################################################");
    
          ParseFlexIP(message);
          ParseFlexModel(message);
          ParseFlexSerial(message);
          ParseFlexVersion(message);
          ParseFlexNickname(message);
          ParseFlexCallsign(message);
    
          StartListening();
        }
    
        private void ReceiveMeter(IAsyncResult ar)
        {
          IPEndPoint ip = new IPEndPoint(IPAddress.Parse(frmPaestro.FlexIP), 4999);
          byte[] bytes = udp.EndReceive(ar, ref ip);
          string message = Encoding.ASCII.GetString(bytes);  // for debugging purposes
    
          Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
          Console.WriteLine(message);
          Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    
          StartListening();
        }
    

    Any idea what I am missing here?

  • Alan
    Alan Member ✭✭✭✭

    Len

    I see you are commanding Flex to use port 4999. I just allow Flex to negotiate its port.

    It been awhile, but I think the default port for meters are port 4992, or 4993.

    Also, I found it easiest to just subscribe to all of the meters, "sub meter all"

    Alan. WA9WUD

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    HI Alan, thanks for the response. 4992 is the discovery port. I did try it, but to no avail. I couldn't get any ports to work (they were busy) until I hit 4996 or 7, so I just plugged in 4999 as I knew it was free.

    Yep, I did subscribe to all meters, but the result was the same. I forgot to mention that the code snippet is C# and I use Visual Studio 2019, community version.

  • Clay
    Clay Member ✭✭

    Hi Len,

    If you are still receiving discovery broadcast packets after you have connected to the radio. Then you haven't closed the discovery socket.

    The sequence is:

    Open UDP socket listening on port 4992.

    Capture the IP address and any other data you need then CLOSE the discovery socket.

    Open a TCP socket on port 4992 and connect to the radio's IP Address.

    Open a UDP socket listening on port 4993 or in your case 4999.

    Over the TCP port send the command "C14 | client udpport 4999"

    Over the TCP socket subscribe to the meters you are interested in.

    Read the Vita-49 packets (hex data) over the UDP socket.

    Close the TCP and UDP sockets before exiting the program.

    Hope this is helpful.

    73,

    Clay

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Thanks Clay, I really appreciate it! I will give that a try and let you know how I fare.

  • Clay
    Clay Member ✭✭
    edited December 2021

    Hi Len,

    I need to make a correction to my last post:

    Open a TCP socket on port 4992 and connect to the radio's IP Address.

    Should read:

    Open a TCP socket and connect to the radio's IP Address on port 4992.

    See the example C discovery source code on the previous website link.

    73,

    Clay, AB9A

  • KD0RC
    KD0RC Member, Super Elmer Moderator

    Thanks Clay. Yep, I am already past that nuance, but I appreciate the clarification. I was in the middle of getting the UDP port-open code working for the meter packets when I had to send my rig in for repair (transverter port output issue), so I won't be able to do anything else until it returns. I need to understand the underlying C# a little better. I don't think in C or its variants, I think in PL/I (I know, I know... I am old). 😄

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.