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,4 +1,5 @@
using System.Reflection;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -10,18 +11,15 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]
[assembly: AssemblyProduct ("SerialController")]
[assembly: AssemblyCopyright("Copyright © Pawel Walczak 2014")]
[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)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid ("a69bd5ae-f03a-4dcf-856d-4303bf64e2a3")]
// Version information for an assembly consists of the following four values:
//
// Major Version

View File

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

View File

@ -22,23 +22,28 @@ namespace SDRSharp.SerialController
{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)
@ -63,22 +68,54 @@ namespace SDRSharp.SerialController
response += "IF";
response += String.Format("{0:00000000000}", _radio.RadioFrequency);
response += "0000000000000000";
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)) {
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;
}

View File

@ -17,6 +17,10 @@ namespace SDRSharp.SerialController
private SerialPortCtrl _serialPort;
private ProtocolInterface _Protocol;
long _LastRadioFrequency;
DetectorType _LastRadioMode;
public string DisplayName
{
get { return _displayName; }
@ -36,6 +40,8 @@ namespace SDRSharp.SerialController
{
_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 @@ namespace SDRSharp.SerialController
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 @@ namespace SDRSharp.SerialController
return _control.Frequency;
}
set {
_LastRadioFrequency = value;
_controlPanel.addToLogList(value.ToString("N0")+" Hz");
_control.Frequency = value;
}
@ -76,6 +94,7 @@ namespace SDRSharp.SerialController
return _control.DetectorType;
}
set {
_LastRadioMode = value;
_controlPanel.addToLogList(value.ToString());
_control.DetectorType = value;
}

View File

@ -19,10 +19,12 @@ namespace SDRSharp.SerialController
SerialPort _port;
ProtocolInterface _protocol;
string _received;
public SerialPortCtrl(ProtocolInterface protocol)
{
_protocol = protocol;
_received = "";
}
public static string[] GetAllPorts()
@ -82,23 +84,23 @@ namespace SDRSharp.SerialController
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);
}
catch (Exception) {
return;
}
data += System.Text.Encoding.UTF8.GetString(bytes);
}
data = data.Substring(0, data.IndexOf(_protocol.EndMarker));
while (_port.BytesToRead > 0) {
if (_received.Length > _protocol.MaxLen)
_received = "";
string response = _protocol.PktReceiver(data);
if (! string.IsNullOrEmpty(response))
string input = ((char)_port.ReadChar()).ToString();
if (input == _protocol.EndMarker) {
string response = _protocol.PktReceiver(_received);
if (! string.IsNullOrEmpty(response)) {
DataTransmit(response);
}
_received = "";
}
else {
_received += input;
}
}
}
public void DataTransmit(string data)
{