Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
improve hamlib compatibility (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
UzixLS committed Feb 27, 2018
1 parent 40ba5cd commit 11d6358
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 36 deletions.
30 changes: 14 additions & 16 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")]
1 change: 1 addition & 0 deletions ProtocolInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace SDRSharp.SerialController
public interface ProtocolInterface
{
string EndMarker { get; }
int MaxLen { get; }
string PktTransmitter(string ChangedProperty);
string PktReceiver(string ReveivedData);
}
Expand Down
51 changes: 44 additions & 7 deletions Protocol_TS50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint, DetectorType> int2mode = new Dictionary<uint, DetectorType> {
{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)
Expand All @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions SerialControllerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -65,6 +82,7 @@ public long RadioFrequency
return _control.Frequency;
}
set {
_LastRadioFrequency = value;
_controlPanel.addToLogList(value.ToString("N0")+" Hz");
_control.Frequency = value;
}
Expand All @@ -76,6 +94,7 @@ public DetectorType RadioMode
return _control.DetectorType;
}
set {
_LastRadioMode = value;
_controlPanel.addToLogList(value.ToString());
_control.DetectorType = value;
}
Expand Down
28 changes: 15 additions & 13 deletions SerialPortCtrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public bool IsOpen {

SerialPort _port;
ProtocolInterface _protocol;
string _received;

public SerialPortCtrl(ProtocolInterface protocol)
{
_protocol = protocol;
_received = "";
}

public static string[] GetAllPorts()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 11d6358

Please sign in to comment.