From e5aa75b7e656616d38e9c3f1a8680c5d1026cfee Mon Sep 17 00:00:00 2001 From: UzixLS Date: Thu, 8 Feb 2018 21:36:42 +0300 Subject: [PATCH] refactor protocol --- ProtocolInterface.cs | 17 ++++++ Protocol_TS50.cs | 86 ++++++++++++++++++++++++++++++ SDRSharp.SerialController.csproj | 6 ++- SerialControllerPanel.cs | 3 +- SerialControllerPlugin.cs | 70 +++++++++++++----------- SerialPktProcessor.cs | 81 ---------------------------- SerialPort.cs => SerialPortCtrl.cs | 54 ++++++++++--------- SerialRadioInterface.cs | 19 +++++++ 8 files changed, 196 insertions(+), 140 deletions(-) create mode 100644 ProtocolInterface.cs create mode 100644 Protocol_TS50.cs delete mode 100644 SerialPktProcessor.cs rename SerialPort.cs => SerialPortCtrl.cs (58%) create mode 100644 SerialRadioInterface.cs diff --git a/ProtocolInterface.cs b/ProtocolInterface.cs new file mode 100644 index 0000000..e390fe9 --- /dev/null +++ b/ProtocolInterface.cs @@ -0,0 +1,17 @@ +/* + * Created by SharpDevelop. + * User: uzix + * Date: 04.01.2017 + * Time: 16:02 + */ +using System; + +namespace SDRSharp.SerialController +{ + public interface ProtocolInterface + { + string EndMarker { get; } + string PktTransmitter(string ChangedProperty); + string PktReceiver(string ReveivedData); + } +} diff --git a/Protocol_TS50.cs b/Protocol_TS50.cs new file mode 100644 index 0000000..55fb23c --- /dev/null +++ b/Protocol_TS50.cs @@ -0,0 +1,86 @@ +/* + * Created by SharpDevelop. + * User: uzix + * Date: 02.05.2016 + * Time: 17:00 + */ +using System; +using System.Collections.Generic; + +using SDRSharp.Radio; + + +namespace SDRSharp.SerialController +{ + public class Protocol_TS50 : ProtocolInterface + { + static readonly Dictionary mode2int = new Dictionary { + {DetectorType.NFM, 4}, + {DetectorType.WFM, 4}, + {DetectorType.AM, 5}, + {DetectorType.DSB, 5}, + {DetectorType.LSB, 1}, + {DetectorType.USB, 2}, + {DetectorType.CW, 3}, + {DetectorType.RAW, 4} + }; + static readonly Dictionary int2mode = new Dictionary { + {1, DetectorType.LSB}, + {2, DetectorType.USB}, + {3, DetectorType.CW}, + {4, DetectorType.NFM}, + {5, DetectorType.AM} + }; + + public string EndMarker { get { return ";"; } } + + SerialRadioInterface _radio; + + public Protocol_TS50(SerialRadioInterface radio) + { + _radio = radio; + } + + public string PktTransmitter(string ChangedProperty) + { + string response = ""; + switch (ChangedProperty) + { + case "Frequency": + response = "FA" + String.Format("{0:00000000000}", _radio.RadioFrequency) + ";"; + break; + case "DetectorType": + response = "MD" + mode2int[_radio.RadioMode] + ";"; + break; + } + return response; + } + + public string PktReceiver(string ReveivedData) + { + string response = ""; + if (ReveivedData.StartsWith("IF", StringComparison.Ordinal)) { + response += "IF"; + response += String.Format("{0:00000000000}", _radio.RadioFrequency); + response += "0000000000000000"; + response += mode2int[_radio.RadioMode]; + response += "0000000"; + response += EndMarker; + } + if (ReveivedData.StartsWith("FA", StringComparison.Ordinal)) { + long freq; + if (long.TryParse(ReveivedData.Substring(2), out freq)) { + _radio.RadioFrequency = freq; + } + } + if (ReveivedData.StartsWith("MD", StringComparison.Ordinal)) { + uint mode; + if (uint.TryParse(ReveivedData.Substring(2), out mode)) { + _radio.RadioMode = int2mode[mode]; + } + } + return response; + } + + } +} diff --git a/SDRSharp.SerialController.csproj b/SDRSharp.SerialController.csproj index 226a807..f42765a 100644 --- a/SDRSharp.SerialController.csproj +++ b/SDRSharp.SerialController.csproj @@ -101,8 +101,9 @@ - - + + +