improve hamlib compatibility (#3)

This commit is contained in:
UzixLS
2018-02-27 21:40:09 +03:00
parent 40ba5cd3f8
commit 11d6358712
5 changed files with 93 additions and 36 deletions

View File

@ -1,27 +1,25 @@
using System.Reflection; using System;
using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information // set of attributes. Change these attribute values to modify the information
// associated with an assembly. // associated with an assembly.
[assembly: AssemblyTitle("SerialController")] [assembly: AssemblyTitle ("SerialController")]
[assembly: AssemblyDescription("Serial port controller for SDR#")] [assembly: AssemblyDescription ("Serial port controller for SDR#")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct("SerialController")] [assembly: AssemblyProduct ("SerialController")]
[assembly: AssemblyCopyright("Copyright © Pawel Walczak 2014")] [assembly: AssemblyCopyright ("")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark ("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture ("")]
// Setting ComVisible to false makes the types in this assembly not visible // 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 // to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. // 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 // 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: // Version information for an assembly consists of the following four values:
// //
// Major Version // Major Version
@ -32,5 +30,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyVersion ("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyFileVersion ("1.0.0.0")]

View File

@ -11,6 +11,7 @@ namespace SDRSharp.SerialController
public interface ProtocolInterface public interface ProtocolInterface
{ {
string EndMarker { get; } string EndMarker { get; }
int MaxLen { get; }
string PktTransmitter(string ChangedProperty); string PktTransmitter(string ChangedProperty);
string PktReceiver(string ReveivedData); string PktReceiver(string ReveivedData);
} }

View File

@ -22,23 +22,28 @@ namespace SDRSharp.SerialController
{DetectorType.LSB, 1}, {DetectorType.LSB, 1},
{DetectorType.USB, 2}, {DetectorType.USB, 2},
{DetectorType.CW, 3}, {DetectorType.CW, 3},
{DetectorType.RAW, 4} {DetectorType.RAW, 8}
}; };
static readonly Dictionary<uint, DetectorType> int2mode = new Dictionary<uint, DetectorType> { static readonly Dictionary<uint, DetectorType> int2mode = new Dictionary<uint, DetectorType> {
{1, DetectorType.LSB}, {1, DetectorType.LSB},
{2, DetectorType.USB}, {2, DetectorType.USB},
{3, DetectorType.CW}, {3, DetectorType.CW},
{4, DetectorType.NFM}, {4, DetectorType.NFM},
{5, DetectorType.AM} {5, DetectorType.AM},
{8, DetectorType.RAW}
}; };
public string EndMarker { get { return ";"; } } public string EndMarker { get { return ";"; } }
public int MaxLen { get { return 255; } }
SerialRadioInterface _radio; SerialRadioInterface _radio;
bool _DetectorSetFailure;
public Protocol_TS50(SerialRadioInterface radio) public Protocol_TS50(SerialRadioInterface radio)
{ {
_radio = radio; _radio = radio;
_DetectorSetFailure = false;
} }
public string PktTransmitter(string ChangedProperty) public string PktTransmitter(string ChangedProperty)
@ -63,22 +68,54 @@ namespace SDRSharp.SerialController
response += "IF"; response += "IF";
response += String.Format("{0:00000000000}", _radio.RadioFrequency); response += String.Format("{0:00000000000}", _radio.RadioFrequency);
response += "0000000000000000"; response += "0000000000000000";
response += mode2int[_radio.RadioMode]; if ( _DetectorSetFailure)
response += 0;
else
response += mode2int[_radio.RadioMode];
response += "0000000"; response += "0000000";
response += EndMarker; 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; long freq;
if (long.TryParse(ReceivedData.Substring(2), out freq)) { if (long.TryParse(ReceivedData.Substring(2), out freq)) {
_radio.RadioFrequency = 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; uint mode;
if (uint.TryParse(ReceivedData.Substring(2), out 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; return response;
} }

View File

@ -17,6 +17,10 @@ namespace SDRSharp.SerialController
private SerialPortCtrl _serialPort; private SerialPortCtrl _serialPort;
private ProtocolInterface _Protocol; private ProtocolInterface _Protocol;
long _LastRadioFrequency;
DetectorType _LastRadioMode;
public string DisplayName public string DisplayName
{ {
get { return _displayName; } get { return _displayName; }
@ -36,6 +40,8 @@ namespace SDRSharp.SerialController
{ {
_control = control; _control = control;
_control.PropertyChanged += PropertyChangedHandler; _control.PropertyChanged += PropertyChangedHandler;
_LastRadioFrequency = _control.Frequency;
_LastRadioMode = _control.DetectorType;
_Protocol = new Protocol_TS50(this); _Protocol = new Protocol_TS50(this);
_serialPort = new SerialPortCtrl(_Protocol); _serialPort = new SerialPortCtrl(_Protocol);
_controlPanel = new SerialControllerPanel(_serialPort); _controlPanel = new SerialControllerPanel(_serialPort);
@ -51,6 +57,17 @@ namespace SDRSharp.SerialController
void PropertyChangedHandler(object sender, PropertyChangedEventArgs e) 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) if (_serialPort.IsOpen)
{ {
string response = _Protocol.PktTransmitter(e.PropertyName); string response = _Protocol.PktTransmitter(e.PropertyName);
@ -65,6 +82,7 @@ namespace SDRSharp.SerialController
return _control.Frequency; return _control.Frequency;
} }
set { set {
_LastRadioFrequency = value;
_controlPanel.addToLogList(value.ToString("N0")+" Hz"); _controlPanel.addToLogList(value.ToString("N0")+" Hz");
_control.Frequency = value; _control.Frequency = value;
} }
@ -76,6 +94,7 @@ namespace SDRSharp.SerialController
return _control.DetectorType; return _control.DetectorType;
} }
set { set {
_LastRadioMode = value;
_controlPanel.addToLogList(value.ToString()); _controlPanel.addToLogList(value.ToString());
_control.DetectorType = value; _control.DetectorType = value;
} }

View File

@ -19,10 +19,12 @@ namespace SDRSharp.SerialController
SerialPort _port; SerialPort _port;
ProtocolInterface _protocol; ProtocolInterface _protocol;
string _received;
public SerialPortCtrl(ProtocolInterface protocol) public SerialPortCtrl(ProtocolInterface protocol)
{ {
_protocol = protocol; _protocol = protocol;
_received = "";
} }
public static string[] GetAllPorts() public static string[] GetAllPorts()
@ -82,22 +84,22 @@ namespace SDRSharp.SerialController
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{ {
string data = ""; while (_port.BytesToRead > 0) {
while (data.IndexOf(_protocol.EndMarker) < 0) { if (_received.Length > _protocol.MaxLen)
byte[] bytes = new byte[_port.BytesToRead+32]; _received = "";
try {
_port.Read(bytes, 0, _port.BytesToRead); string input = ((char)_port.ReadChar()).ToString();
if (input == _protocol.EndMarker) {
string response = _protocol.PktReceiver(_received);
if (! string.IsNullOrEmpty(response)) {
DataTransmit(response);
}
_received = "";
} }
catch (Exception) { else {
return; _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) public void DataTransmit(string data)