mirror of
https://github.com/UzixLS/sdrsharp-catcontroller.git
synced 2025-07-18 14:51:28 +03:00
refactor protocol
This commit is contained in:
17
ProtocolInterface.cs
Normal file
17
ProtocolInterface.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
86
Protocol_TS50.cs
Normal file
86
Protocol_TS50.cs
Normal file
@ -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<DetectorType, uint> mode2int = new Dictionary<DetectorType, uint> {
|
||||
{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<uint, DetectorType> int2mode = new Dictionary<uint, DetectorType> {
|
||||
{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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -101,8 +101,9 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="SerialPktProcessor.cs" />
|
||||
<Compile Include="SerialPort.cs" />
|
||||
<Compile Include="ProtocolInterface.cs" />
|
||||
<Compile Include="Protocol_TS50.cs" />
|
||||
<Compile Include="SerialPortCtrl.cs" />
|
||||
<!--<Reference Include="SDRSharp.PanView, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>../Common/SDRSharp.PanView.dll</HintPath>
|
||||
@ -115,6 +116,7 @@
|
||||
</Compile>
|
||||
<Compile Include="SerialControllerPlugin.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SerialRadioInterface.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="SerialControllerPanel.resx">
|
||||
|
@ -3,6 +3,7 @@ using System.Windows.Forms;
|
||||
|
||||
using SDRSharp.Radio;
|
||||
|
||||
|
||||
namespace SDRSharp.SerialController
|
||||
{
|
||||
public partial class SerialControllerPanel : UserControl
|
||||
@ -25,7 +26,7 @@ namespace SDRSharp.SerialController
|
||||
}
|
||||
|
||||
public void addToLogList(String log) {
|
||||
lbLog.Items.Add(log);
|
||||
lbLog.Items.Add(DateTime.Now.ToString("[HH:mm:ss] ") + log);
|
||||
// scroll to bottom
|
||||
lbLog.SelectedIndex = lbLog.Items.Count - 1;
|
||||
lbLog.SelectedIndex = -1;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using SDRSharp.Common;
|
||||
@ -7,14 +8,14 @@ using SDRSharp.Radio;
|
||||
|
||||
namespace SDRSharp.SerialController
|
||||
{
|
||||
public class SerialControllerPlugin: ISharpPlugin
|
||||
public class SerialControllerPlugin: ISharpPlugin,SerialRadioInterface
|
||||
{
|
||||
private const string _displayName = "SerialController";
|
||||
|
||||
private ISharpControl _control;
|
||||
private SerialControllerPanel _controlPanel;
|
||||
private SerialPortCtrl _serialPort;
|
||||
private SerialPktProcessor _serialPktProcessor;
|
||||
private ProtocolInterface _Protocol;
|
||||
|
||||
public string DisplayName
|
||||
{
|
||||
@ -34,42 +35,51 @@ namespace SDRSharp.SerialController
|
||||
public void Initialize(ISharpControl control)
|
||||
{
|
||||
_control = control;
|
||||
|
||||
_serialPktProcessor = new SerialPktProcessor();
|
||||
_serialPktProcessor.OnFrequencyChange += UpdateFrequency;
|
||||
_serialPktProcessor.OnGetFrequency += GetFrequency;
|
||||
_serialPktProcessor.OnModeChange += UpdateDemodulation;
|
||||
_serialPktProcessor.OnGetMode += GetDemodulation;
|
||||
|
||||
_serialPort = new SerialPortCtrl(_serialPktProcessor);
|
||||
_serialPort.separator = _serialPktProcessor.separator;
|
||||
|
||||
_control.PropertyChanged += PropertyChangedHandler;
|
||||
_Protocol = new Protocol_TS50(this);
|
||||
_serialPort = new SerialPortCtrl(_Protocol);
|
||||
_controlPanel = new SerialControllerPanel(_serialPort);
|
||||
_controlPanel.readSettings();
|
||||
}
|
||||
|
||||
void UpdateFrequency(object sender, long freq) {
|
||||
_control.Frequency = freq;
|
||||
_controlPanel.addToLogList(freq.ToString("N0")+" Hz");
|
||||
}
|
||||
|
||||
long GetFrequency() {
|
||||
return _control.Frequency;
|
||||
}
|
||||
|
||||
void UpdateDemodulation(object sender, DetectorType mode) {
|
||||
_control.DetectorType = mode;
|
||||
_controlPanel.addToLogList(mode.ToString());
|
||||
}
|
||||
|
||||
DetectorType GetDemodulation() {
|
||||
return _control.DetectorType;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
_serialPort.closePort();
|
||||
_controlPanel.saveSettings();
|
||||
}
|
||||
|
||||
|
||||
void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (_serialPort.IsOpen)
|
||||
{
|
||||
string response = _Protocol.PktTransmitter(e.PropertyName);
|
||||
if (! string.IsNullOrEmpty(response))
|
||||
_serialPort.DataTransmit(response);
|
||||
}
|
||||
}
|
||||
|
||||
public long RadioFrequency
|
||||
{
|
||||
get {
|
||||
return _control.Frequency;
|
||||
}
|
||||
set {
|
||||
_controlPanel.addToLogList(value.ToString("N0")+" Hz");
|
||||
_control.Frequency = value;
|
||||
}
|
||||
}
|
||||
|
||||
public DetectorType RadioMode
|
||||
{
|
||||
get {
|
||||
return _control.DetectorType;
|
||||
}
|
||||
set {
|
||||
_controlPanel.addToLogList(value.ToString());
|
||||
_control.DetectorType = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Created by SharpDevelop.
|
||||
* User: uzix
|
||||
* Date: 02.05.2016
|
||||
* Time: 17:00
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using SDRSharp.Radio;
|
||||
|
||||
|
||||
namespace SDRSharp.SerialController
|
||||
{
|
||||
/// <summary>
|
||||
/// Description of Class1.
|
||||
/// </summary>
|
||||
public class SerialPktProcessor
|
||||
{
|
||||
static Dictionary<DetectorType, uint> mode2int = new Dictionary<DetectorType, uint> {
|
||||
{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 Dictionary<uint, DetectorType> int2mode = new Dictionary<uint, DetectorType> {
|
||||
{1, DetectorType.LSB},
|
||||
{2, DetectorType.USB},
|
||||
{3, DetectorType.CW},
|
||||
{4, DetectorType.NFM},
|
||||
{5, DetectorType.AM}
|
||||
};
|
||||
public readonly char separator = ';';
|
||||
|
||||
public delegate void FrequencyChangeHandler(object sender, long freq);
|
||||
public event FrequencyChangeHandler OnFrequencyChange;
|
||||
|
||||
public delegate long GetFrequencyHandler();
|
||||
public event GetFrequencyHandler OnGetFrequency;
|
||||
|
||||
public delegate void ModeChangeHandler(object sender, DetectorType mode);
|
||||
public event ModeChangeHandler OnModeChange;
|
||||
|
||||
public delegate DetectorType GetModeHandler();
|
||||
public event GetModeHandler OnGetMode;
|
||||
|
||||
public string process(string data)
|
||||
{
|
||||
string response = "";
|
||||
// TS-50 command parse
|
||||
if (data.StartsWith("IF", StringComparison.Ordinal)) {
|
||||
long freq = OnGetFrequency();
|
||||
DetectorType mode = OnGetMode();
|
||||
response += "IF";
|
||||
response += String.Format("{0:00000000000}", freq);
|
||||
response += "0000000000000000";
|
||||
response += mode2int[mode];
|
||||
response += "0000000";
|
||||
response += separator;
|
||||
}
|
||||
if (data.StartsWith("FA", StringComparison.Ordinal)) {
|
||||
long freq;
|
||||
if (long.TryParse(data.Substring(2), out freq)) {
|
||||
OnFrequencyChange(this, freq);
|
||||
}
|
||||
}
|
||||
if (data.StartsWith("MD", StringComparison.Ordinal)) {
|
||||
uint mode;
|
||||
if (uint.TryParse(data.Substring(2), out mode)) {
|
||||
OnModeChange(this, int2mode[mode]);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -11,40 +11,33 @@ using System.Windows.Forms;
|
||||
|
||||
namespace SDRSharp.SerialController
|
||||
{
|
||||
/// <summary>
|
||||
/// Description of SerialPort.
|
||||
/// </summary>
|
||||
public class SerialPortCtrl
|
||||
{
|
||||
public bool IsOpen {
|
||||
get { return _port != null && _port.IsOpen; }
|
||||
}
|
||||
|
||||
char _separator;
|
||||
public char separator {
|
||||
get { return separator; }
|
||||
set { this._separator = value; }
|
||||
}
|
||||
|
||||
SerialPort _port;
|
||||
SerialPktProcessor _pktprocessor;
|
||||
ProtocolInterface _protocol;
|
||||
|
||||
public SerialPortCtrl( SerialPktProcessor pktprocessor )
|
||||
public SerialPortCtrl(ProtocolInterface protocol)
|
||||
{
|
||||
_pktprocessor = pktprocessor;
|
||||
_protocol = protocol;
|
||||
}
|
||||
|
||||
public static string[] GetAllPorts()
|
||||
{
|
||||
try {
|
||||
return SerialPort.GetPortNames();
|
||||
} catch (Exception e) {
|
||||
MessageBox.Show("Cannot read port list:\n"+e.ToString(), "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
catch (Exception e) {
|
||||
MessageBox.Show("Couldn't read port list:\n"+e.ToString(), "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return new string[0];
|
||||
}
|
||||
}
|
||||
|
||||
public bool openPort(string portName) {
|
||||
public bool openPort(string portName)
|
||||
{
|
||||
try {
|
||||
if (_port != null && _port.IsOpen)
|
||||
return false;
|
||||
@ -53,40 +46,44 @@ namespace SDRSharp.SerialController
|
||||
return false;
|
||||
|
||||
_port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
|
||||
_port.DataReceived += new SerialDataReceivedEventHandler( Port_DataReceived );
|
||||
_port.DataReceived += new SerialDataReceivedEventHandler( DataReceivedHandler );
|
||||
|
||||
if (_port != null) {
|
||||
_port.Open();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
MessageBox.Show("Couldn't open port "+portName+":\n"+e.ToString(), "SerialController",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool closePort() {
|
||||
public bool closePort()
|
||||
{
|
||||
if (_port != null) {
|
||||
if (_port.IsOpen) {
|
||||
try {
|
||||
_port.Close();
|
||||
return true;
|
||||
} catch (IOException) {
|
||||
}
|
||||
catch (IOException) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
string data = "";
|
||||
while (data.IndexOf(_separator) < 0) {
|
||||
while (data.IndexOf(_protocol.EndMarker) < 0) {
|
||||
byte[] bytes = new byte[_port.BytesToRead+32];
|
||||
try {
|
||||
_port.Read(bytes, 0, _port.BytesToRead);
|
||||
@ -96,11 +93,16 @@ namespace SDRSharp.SerialController
|
||||
}
|
||||
data += System.Text.Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
data = data.Substring(0, data.IndexOf(_separator));
|
||||
data = data.Substring(0, data.IndexOf(_protocol.EndMarker));
|
||||
|
||||
string response = _pktprocessor.process(data);
|
||||
string response = _protocol.PktReceiver(data);
|
||||
if (! string.IsNullOrEmpty(response))
|
||||
_port.Write(response);
|
||||
DataTransmit(response);
|
||||
}
|
||||
|
||||
public void DataTransmit(string data)
|
||||
{
|
||||
_port.Write(data);
|
||||
}
|
||||
}
|
||||
}
|
19
SerialRadioInterface.cs
Normal file
19
SerialRadioInterface.cs
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Created by SharpDevelop.
|
||||
* User: uzix
|
||||
* Date: 04.01.2017
|
||||
* Time: 16:46
|
||||
*/
|
||||
using System;
|
||||
|
||||
using SDRSharp.Radio;
|
||||
|
||||
|
||||
namespace SDRSharp.SerialController
|
||||
{
|
||||
public interface SerialRadioInterface
|
||||
{
|
||||
long RadioFrequency { get; set; }
|
||||
DetectorType RadioMode { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user