TS-50 MD command

This commit is contained in:
UzixLS
2016-05-02 19:02:30 +03:00
parent c995b5a3e8
commit 21496719dd
7 changed files with 137 additions and 112 deletions

View File

@ -1,8 +1,17 @@
# sdrsharp-catcontroller
This plugin allow SDR# to be controlled via serial (COM) interface with subset of TS-50 receiver commands.
This plugin allow SDR# to be controlled via serial (COM) interface with set of TS-50 receiver commands.
E.g. it can be coupled with com0com virtual serial port and Fldigi for better convenience.
Supported commands:
* IF - get frequency
* IF - get frequency and mode
* FA - set frequency
* MD - set mode (AM,FM,USB,LSB,CW)
Serial port parameters:
* Speed: 9600
* Data bits: 8
* Stop bits: 1
* Parity: none
This plugin based on code by pewusoft (http://users.vline.pl/~pewusoft/)

View File

@ -101,6 +101,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SerialPktProcessor.cs" />
<Compile Include="SerialPort.cs" />
<!--<Reference Include="SDRSharp.PanView, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>

View File

@ -32,9 +32,7 @@
this.cbEnable = new System.Windows.Forms.CheckBox();
this.comboPorts = new System.Windows.Forms.ComboBox();
this.btnRefreshPorts = new System.Windows.Forms.Button();
this.cbLogToFile = new System.Windows.Forms.CheckBox();
this.lbLog = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cbEnable
@ -61,47 +59,27 @@
this.btnRefreshPorts.Image = ((System.Drawing.Image)(resources.GetObject("btnRefreshPorts.Image")));
this.btnRefreshPorts.Location = new System.Drawing.Point(188, 2);
this.btnRefreshPorts.Name = "btnRefreshPorts";
this.btnRefreshPorts.Size = new System.Drawing.Size(30, 30);
this.btnRefreshPorts.Size = new System.Drawing.Size(24, 24);
this.btnRefreshPorts.TabIndex = 3;
this.btnRefreshPorts.UseVisualStyleBackColor = true;
this.btnRefreshPorts.Click += new System.EventHandler(this.BtnRefreshPortsClick);
//
// cbLogToFile
//
this.cbLogToFile.Location = new System.Drawing.Point(3, 30);
this.cbLogToFile.Name = "cbLogToFile";
this.cbLogToFile.Size = new System.Drawing.Size(104, 24);
this.cbLogToFile.TabIndex = 4;
this.cbLogToFile.Text = "Log to file";
this.cbLogToFile.UseVisualStyleBackColor = true;
//
// lbLog
//
this.lbLog.FormattingEnabled = true;
this.lbLog.Location = new System.Drawing.Point(3, 60);
this.lbLog.Location = new System.Drawing.Point(3, 30);
this.lbLog.Name = "lbLog";
this.lbLog.Size = new System.Drawing.Size(215, 69);
this.lbLog.TabIndex = 5;
//
// label1
//
this.label1.Location = new System.Drawing.Point(3, 132);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(215, 46);
this.label1.TabIndex = 6;
this.label1.Text = "Accept some TS-50 CAT commands via serial interface. " +
"Made by Uzix, 2016. Based on plugin by pewusoft, 2015";
this.lbLog.TabIndex = 4;
//
// SerialControllerPanel
//
this.Controls.Add(this.label1);
this.Controls.Add(this.lbLog);
this.Controls.Add(this.cbLogToFile);
this.Controls.Add(this.btnRefreshPorts);
this.Controls.Add(this.comboPorts);
this.Controls.Add(this.cbEnable);
this.Name = "SerialControllerPanel";
this.Size = new System.Drawing.Size(222, 201);
this.Size = new System.Drawing.Size(222, 131);
this.ResumeLayout(false);
}
@ -111,9 +89,7 @@
private System.Windows.Forms.CheckBox cbEnable;
private System.Windows.Forms.ComboBox comboPorts;
private System.Windows.Forms.Button btnRefreshPorts;
private System.Windows.Forms.CheckBox cbLogToFile;
private System.Windows.Forms.ListBox lbLog;
private System.Windows.Forms.Label label1;
}

View File

@ -33,14 +33,12 @@ namespace SDRSharp.SerialController
public void readSettings() {
comboPorts.Text = Utils.GetStringSetting("serialControlComPort", "");
cbLogToFile.Checked = Utils.GetBooleanSetting("serialControlLogToFile");
cbEnable.Checked = Utils.GetBooleanSetting("serialControlEnable");
CbEnableClick(null,null);
}
public void saveSettings() {
Utils.SaveSetting("serialControlComPort", comboPorts.Text);
Utils.SaveSetting("serialControlLogToFile", cbLogToFile.Checked);
Utils.SaveSetting("serialControlEnable", cbEnable.Checked);
}
void CbEnableClick(object sender, EventArgs e)
@ -52,8 +50,6 @@ namespace SDRSharp.SerialController
cbEnable.Checked = _serialPort.IsOpen;
comboPorts.Enabled = !cbEnable.Checked;
btnRefreshPorts.Enabled = !cbEnable.Checked;
cbLogToFile.Enabled = !cbEnable.Checked;
_serialPort.EnableLogging = cbLogToFile.Checked;
}
void CbEnableKeyDown(object sender, EventArgs e)
{

View File

@ -2,6 +2,8 @@
using System.Windows.Forms;
using SDRSharp.Common;
using SDRSharp.Radio;
namespace SDRSharp.SerialController
{
@ -9,9 +11,10 @@ namespace SDRSharp.SerialController
{
private const string _displayName = "SerialController";
private ISharpControl _control;
private SerialControllerPanel _controlPanel;
private SerialPortCtrl _serialPort;
private ISharpControl _control;
private SerialPktProcessor _serialPktProcessor;
public string DisplayName
{
@ -31,9 +34,15 @@ namespace SDRSharp.SerialController
public void Initialize(ISharpControl control)
{
_control = control;
_serialPort = new SerialPortCtrl();
_serialPort.OnFrequencyChange += UpdateFrequency;
_serialPort.OnGetFrequency += GetFrequency;
_serialPktProcessor = new SerialPktProcessor();
_serialPktProcessor.OnFrequencyChange += UpdateFrequency;
_serialPktProcessor.OnGetFrequency += GetFrequency;
_serialPktProcessor.OnModeChange += UpdateDemodulation;
_serialPktProcessor.OnGetMode += GetDemodulation;
_serialPort = new SerialPortCtrl(_serialPktProcessor);
_serialPort.separator = _serialPktProcessor.separator;
_controlPanel = new SerialControllerPanel(_serialPort);
_controlPanel.readSettings();
@ -48,6 +57,15 @@ namespace SDRSharp.SerialController
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();

81
SerialPktProcessor.cs Normal file
View File

@ -0,0 +1,81 @@
/*
* 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;
}
}
}

View File

@ -8,6 +8,7 @@ using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
namespace SDRSharp.SerialController
{
/// <summary>
@ -15,24 +16,23 @@ namespace SDRSharp.SerialController
/// </summary>
public class SerialPortCtrl
{
bool _enableLogging = true;
public bool EnableLogging {
set { this._enableLogging = value; }
get { return this._enableLogging; }
}
public bool IsOpen {
get { return _port != null && _port.IsOpen; }
}
StreamWriter logger;
char _separator;
public char separator {
get { return separator; }
set { this._separator = value; }
}
SerialPort _port;
SerialPktProcessor _pktprocessor;
public delegate void FrequencyChangeHandler(object sender, long freq);
public event FrequencyChangeHandler OnFrequencyChange;
public delegate long GetFrequencyHandler();
public event GetFrequencyHandler OnGetFrequency;
public SerialPortCtrl( SerialPktProcessor pktprocessor )
{
_pktprocessor = pktprocessor;
}
public static string[] GetAllPorts()
{
@ -57,10 +57,6 @@ namespace SDRSharp.SerialController
if (_port != null) {
_port.Open();
if (_enableLogging) {
prepareLogger();
log("Port " + _port.PortName + " opened");
}
return true;
}
return false;
@ -76,10 +72,6 @@ namespace SDRSharp.SerialController
if (_port.IsOpen) {
try {
_port.Close();
if (_enableLogging) {
log("Port " + _port.PortName + " closed");
closeLogger();
}
return true;
} catch (IOException) {
return false;
@ -94,69 +86,21 @@ namespace SDRSharp.SerialController
void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = "";
while( data.IndexOf(';') < 0 ) {
while (data.IndexOf(_separator) < 0) {
byte[] bytes = new byte[_port.BytesToRead+32];
try {
_port.Read(bytes, 0, _port.BytesToRead);
}
catch (Exception) {
log("Error reading COM port");
return;
}
data += System.Text.Encoding.UTF8.GetString(bytes);
}
data = data.Substring(0, data.IndexOf(';'));
data = data.Substring(0, data.IndexOf(_separator));
// TS-50 command parse
if (data.StartsWith("IF", StringComparison.Ordinal)) {
long freq = OnGetFrequency();
string response = "IF";
response += String.Format("{0:00000000000}", freq);
response += "000000000000000000000000;";
string response = _pktprocessor.process(data);
if (! string.IsNullOrEmpty(response))
_port.Write(response);
}
if (data.StartsWith("FA", StringComparison.Ordinal)) {
long freq;
if (long.TryParse(data.Substring(2), out freq)) {
log("Received on COM port: "+data);
if (OnFrequencyChange == null) return;
OnFrequencyChange(this, freq);
log("Changing frequency to: "+freq.ToString("N0"));
}
}
}
void log(String str) {
if (logger!=null) {
logger.WriteLine("[" + DateTime.Now + "]: " + str.Trim());
}
}
void prepareLogger() {
try {
if (logger != null) {
logger.Close();
}
logger = new StreamWriter(new FileStream("serial.log",
FileMode.Append,
FileAccess.Write,
FileShare.ReadWrite,
1024,
FileOptions.WriteThrough));
logger.AutoFlush = true;
} catch (Exception) {
logger = null;
}
}
void closeLogger() {
try {
if (logger != null) {
logger.Close();
}
logger = null;
} catch (Exception) {
logger = null;
}
}
}
}