From 11d6358712ecd4939ae71ac2d3c354fdb2e005c7 Mon Sep 17 00:00:00 2001 From: UzixLS Date: Tue, 27 Feb 2018 21:40:09 +0300 Subject: [PATCH] improve hamlib compatibility (#3) --- Properties/AssemblyInfo.cs | 30 +++++++++++----------- ProtocolInterface.cs | 1 + Protocol_TS50.cs | 51 ++++++++++++++++++++++++++++++++------ SerialControllerPlugin.cs | 19 ++++++++++++++ SerialPortCtrl.cs | 28 +++++++++++---------- 5 files changed, 93 insertions(+), 36 deletions(-) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 8882da8..4aac1e3 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,27 +1,25 @@ -using System.Reflection; +using System; +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("SerialController")] -[assembly: AssemblyDescription("Serial port controller for SDR#")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SerialController")] -[assembly: AssemblyCopyright("Copyright © Pawel Walczak 2014")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - +[assembly: AssemblyTitle ("SerialController")] +[assembly: AssemblyDescription ("Serial port controller for SDR#")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("SerialController")] +[assembly: AssemblyCopyright ("")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - +[assembly: ComVisible (false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("a69bd5ae-f03a-4dcf-856d-4303bf64e2a3")] - +[assembly: Guid ("a69bd5ae-f03a-4dcf-856d-4303bf64e2a3")] // Version information for an assembly consists of the following four values: // // Major Version @@ -32,5 +30,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion ("1.0.0.0")] +[assembly: AssemblyFileVersion ("1.0.0.0")] diff --git a/ProtocolInterface.cs b/ProtocolInterface.cs index e390fe9..4dec66e 100644 --- a/ProtocolInterface.cs +++ b/ProtocolInterface.cs @@ -11,6 +11,7 @@ namespace SDRSharp.SerialController public interface ProtocolInterface { string EndMarker { get; } + int MaxLen { get; } string PktTransmitter(string ChangedProperty); string PktReceiver(string ReveivedData); } diff --git a/Protocol_TS50.cs b/Protocol_TS50.cs index a7c3892..82cdf8d 100644 --- a/Protocol_TS50.cs +++ b/Protocol_TS50.cs @@ -22,23 +22,28 @@ public class Protocol_TS50 : ProtocolInterface {DetectorType.LSB, 1}, {DetectorType.USB, 2}, {DetectorType.CW, 3}, - {DetectorType.RAW, 4} + {DetectorType.RAW, 8} }; static readonly Dictionary int2mode = new Dictionary { {1, DetectorType.LSB}, {2, DetectorType.USB}, {3, DetectorType.CW}, {4, DetectorType.NFM}, - {5, DetectorType.AM} + {5, DetectorType.AM}, + {8, DetectorType.RAW} }; public string EndMarker { get { return ";"; } } - + public int MaxLen { get { return 255; } } + SerialRadioInterface _radio; + bool _DetectorSetFailure; + public Protocol_TS50(SerialRadioInterface radio) { _radio = radio; + _DetectorSetFailure = false; } public string PktTransmitter(string ChangedProperty) @@ -63,22 +68,54 @@ public string PktReceiver(string ReceivedData) response += "IF"; response += String.Format("{0:00000000000}", _radio.RadioFrequency); response += "0000000000000000"; - response += mode2int[_radio.RadioMode]; + if ( _DetectorSetFailure) + response += 0; + else + response += mode2int[_radio.RadioMode]; response += "0000000"; response += EndMarker; } - if (ReceivedData.StartsWith("FA", StringComparison.Ordinal)) { + else if (ReceivedData == "FA") { + response += "FA"; + response += String.Format("{0:00000000000}", _radio.RadioFrequency); + response += EndMarker; + } + else if (ReceivedData.StartsWith("FA", StringComparison.Ordinal)) { long freq; if (long.TryParse(ReceivedData.Substring(2), out freq)) { _radio.RadioFrequency = freq; } } - if (ReceivedData.StartsWith("MD", StringComparison.Ordinal)) { + else if (ReceivedData == "MD") { + response += "MD"; + if (_DetectorSetFailure) + response += 0; + else + response += mode2int[_radio.RadioMode]; + response += EndMarker; + } + else if (ReceivedData.StartsWith("MD", StringComparison.Ordinal)) { uint mode; if (uint.TryParse(ReceivedData.Substring(2), out mode)) { - _radio.RadioMode = int2mode[mode]; + try { + _radio.RadioMode = int2mode[mode]; + _DetectorSetFailure = false; + } + catch { + _DetectorSetFailure = true; + } } } + else if (ReceivedData == "ID") { + response += "ID"; + response += "021"; //XXX: TS-590S value, idk what's should be there for TS-50 + response += EndMarker; + } + else if (ReceivedData == "RX") { + response += "RX"; + response += EndMarker; + } + return response; } diff --git a/SerialControllerPlugin.cs b/SerialControllerPlugin.cs index c2bc7df..5bf13d4 100644 --- a/SerialControllerPlugin.cs +++ b/SerialControllerPlugin.cs @@ -17,6 +17,10 @@ public class SerialControllerPlugin: ISharpPlugin,SerialRadioInterface private SerialPortCtrl _serialPort; private ProtocolInterface _Protocol; + long _LastRadioFrequency; + DetectorType _LastRadioMode; + + public string DisplayName { get { return _displayName; } @@ -36,6 +40,8 @@ public void Initialize(ISharpControl control) { _control = control; _control.PropertyChanged += PropertyChangedHandler; + _LastRadioFrequency = _control.Frequency; + _LastRadioMode = _control.DetectorType; _Protocol = new Protocol_TS50(this); _serialPort = new SerialPortCtrl(_Protocol); _controlPanel = new SerialControllerPanel(_serialPort); @@ -51,6 +57,17 @@ public void Close() void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) { + switch (e.PropertyName) + { + case "Frequency": + if (_LastRadioFrequency == _control.Frequency) + return; + break; + case "DetectorType": + if( _LastRadioMode == _control.DetectorType) + return; + break; + } if (_serialPort.IsOpen) { string response = _Protocol.PktTransmitter(e.PropertyName); @@ -65,6 +82,7 @@ public long RadioFrequency return _control.Frequency; } set { + _LastRadioFrequency = value; _controlPanel.addToLogList(value.ToString("N0")+" Hz"); _control.Frequency = value; } @@ -76,6 +94,7 @@ public DetectorType RadioMode return _control.DetectorType; } set { + _LastRadioMode = value; _controlPanel.addToLogList(value.ToString()); _control.DetectorType = value; } diff --git a/SerialPortCtrl.cs b/SerialPortCtrl.cs index 70c54f2..4cd480c 100644 --- a/SerialPortCtrl.cs +++ b/SerialPortCtrl.cs @@ -19,10 +19,12 @@ public bool IsOpen { SerialPort _port; ProtocolInterface _protocol; + string _received; public SerialPortCtrl(ProtocolInterface protocol) { _protocol = protocol; + _received = ""; } public static string[] GetAllPorts() @@ -82,22 +84,22 @@ public bool closePort() void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { - string data = ""; - while (data.IndexOf(_protocol.EndMarker) < 0) { - byte[] bytes = new byte[_port.BytesToRead+32]; - try { - _port.Read(bytes, 0, _port.BytesToRead); + while (_port.BytesToRead > 0) { + if (_received.Length > _protocol.MaxLen) + _received = ""; + + string input = ((char)_port.ReadChar()).ToString(); + if (input == _protocol.EndMarker) { + string response = _protocol.PktReceiver(_received); + if (! string.IsNullOrEmpty(response)) { + DataTransmit(response); + } + _received = ""; } - catch (Exception) { - return; + else { + _received += input; } - data += System.Text.Encoding.UTF8.GetString(bytes); } - data = data.Substring(0, data.IndexOf(_protocol.EndMarker)); - - string response = _protocol.PktReceiver(data); - if (! string.IsNullOrEmpty(response)) - DataTransmit(response); } public void DataTransmit(string data)