From 8b22a5a9a8bb384da2d759ec8f43e8e1cd295e16 Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Thu, 20 May 2021 17:29:21 -0500 Subject: [PATCH] Add csharp JSON deserializer example https://github.com/Hamlib/Hamlib/issues/695 --- bindings/csharp/multicast/README.txt | 22 +++++++ bindings/csharp/multicast/multicast.cs | 68 ++++++++++++++++++++++ bindings/csharp/multicast/multicast.csproj | 12 ++++ bindings/csharp/multicast/test.json | 52 +++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 bindings/csharp/multicast/README.txt create mode 100644 bindings/csharp/multicast/multicast.cs create mode 100644 bindings/csharp/multicast/multicast.csproj create mode 100644 bindings/csharp/multicast/test.json diff --git a/bindings/csharp/multicast/README.txt b/bindings/csharp/multicast/README.txt new file mode 100644 index 000000000..2e4004b96 --- /dev/null +++ b/bindings/csharp/multicast/README.txt @@ -0,0 +1,22 @@ +Works on Windows and Linux + +Requires you get dotnet installed of course + +For Wiundows install Visual Studio or such to get dotnet + +On Ubuntu 20.04 it was this +apt install snap +apt install mono-complete +snap install dotnet-sdk --classic --channel=5.0 +snap alias dotnet-sdk.dotnet dotnet +snap install dotnet-runtime-50 --classic +snap alias dotnet-runtime-50.dotnet dotnet +export DOTNET_ROOT=/snap/dotnet-sdk/current + +Once dotnet is OK + +dotnet build + +You should then be able to run + +./bin/Debug/net5.0/multicast diff --git a/bindings/csharp/multicast/multicast.cs b/bindings/csharp/multicast/multicast.cs new file mode 100644 index 000000000..3846a9508 --- /dev/null +++ b/bindings/csharp/multicast/multicast.cs @@ -0,0 +1,68 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.IO; + +namespace HamlibMultiCast +{ + public class HamlibMulticastClient + { + public class VFO + { + public string Name; + public ulong Freq; + public string Mode; + public int Width; + public bool RX; + public bool TX; + } + public class SpectrumClass + { + public int Length; + public string Data; + public string Type; + public int MinLevel; + public int MaxLevel; + public int MinStrength; + public int MaxStrength; + public double CenterFreq; + public int Span; + public double LowFreq; + public double HighFreq; + } + public string ID; + public List VFOs { get; set; } + public bool Split; + public bool SatMode; + public string Rig; + public string App; + public string Version; + public UInt32 Seq; + public string CRC; + public SpectrumClass Spectrum { get; set; } + } + class Program + { + static int Main(string[] args) + { + Console.WriteLine("HamlibMultiCast Test Json Deserialize"); + ITraceWriter traceWriter = new MemoryTraceWriter(); + try + { + + string json = File.ReadAllText("test.json"); + var client = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { TraceWriter = traceWriter, Converters = { new JavaScriptDateTimeConverter() } }); + Console.WriteLine(traceWriter); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return 1; + } + return 0; + } + } +} + diff --git a/bindings/csharp/multicast/multicast.csproj b/bindings/csharp/multicast/multicast.csproj new file mode 100644 index 000000000..e7a76d33a --- /dev/null +++ b/bindings/csharp/multicast/multicast.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/bindings/csharp/multicast/test.json b/bindings/csharp/multicast/test.json new file mode 100644 index 000000000..e59bb1caa --- /dev/null +++ b/bindings/csharp/multicast/test.json @@ -0,0 +1,52 @@ +{ + "__comment1__": "customizable rig identification -- will allow multiple rigs to be on the multicast", + "ID": "Rig#1", + "VFOs": [ + { + "Name": "VFOA", + "Freq": 14074000, + "Mode": "USB", + "Width": 5000, + "RX": true, + "TX": false + }, + { + "Name": "VFOB", + "Freq": 14076000, + "Mode": "USB", + "Width": 5000, + "RX": false, + "TX": false + }], + + "__comment_spectrum__": "Rigs that have spectrum output may include this data", + "Spectrum": { + "Length": 475, + "__comment_spectrum_data__": "2-char hex bytes so data len=2*Length", + "Data": "00AAFF75BD2AAA...", + "Type": "FIXED|CENTER", + "MinLevel": 0, + "MaxLevel": 140, + "MinStrength": -100, + "MaxStrength": 0, + + "__comment_spectrum_center__": "If Type=CENTER, the following fields will be present:", + "CenterFreq": 14267000, + "Span": 25000, + + "__comment_spectrum_fixed__": "If SpectrumType=FIXED, the following fields will be present:", + "LowFreq": 14000000, + "HighFreq": 14250000 + }, + + "Split": true, + "SatMode": false, + "Rig": "Dummy", + "App": "Hamlib", + "__comment_version__": "protocol version date YYYYMMDD", + "Version": "20210520", + "__comment_seq__": "Seq is 1-up sequence number 32-bit -- wraps around to 1 from 2^32-1", + "Seq": 1, + "__comment_crc__": "32-bit CRC of entire JSON record replacing the CRC value with 0x00000000", + "CRC": "0x00000000" +}