commit 2a72da65177bef49b8ce1ee79c835ed0371226ec Author: UzixLS Date: Mon May 2 15:18:46 2016 +0300 initial (SerialController-1.2) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..91401b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +obj/ + diff --git a/MagicString.txt b/MagicString.txt new file mode 100644 index 0000000..b6b23e2 --- /dev/null +++ b/MagicString.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..8882da8 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +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("")] + +// 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 +// Minor Version +// Build Number +// Revision +// +// 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")] diff --git a/SDRSharp.SerialController.csproj b/SDRSharp.SerialController.csproj new file mode 100644 index 0000000..8ea448f --- /dev/null +++ b/SDRSharp.SerialController.csproj @@ -0,0 +1,130 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {41C6CC2E-5F1D-4BFC-9676-381AD488FCD6} + Library + Properties + SDRSharp.SerialController + SDRSharp.SerialController + v3.5 + 512 + + False + False + False + OnBuildSuccess + False + False + obj\$(Configuration)\ + 4 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + + + pdbonly + true + bin\Release\ + TRACE + prompt + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + prompt + + + true + ..\Debug\ + DEBUG;TRACE + full + x86 + prompt + true + + + ..\Release\ + TRACE + True + PdbOnly + x86 + prompt + True + + + False + obj\ + Project + + + 4194304 + False + Auto + + + + + + + + + + + + + + + False + ../Common/SDRSharp.Common.dll + + + + False + ../Common/SDRSharp.Radio.dll + + + UserControl + + + SerialControllerPanel.cs + + + + + + + SerialControllerPanel.cs + + + + + \ No newline at end of file diff --git a/SDRSharp.SerialController.csproj.user b/SDRSharp.SerialController.csproj.user new file mode 100644 index 0000000..4fdffd5 --- /dev/null +++ b/SDRSharp.SerialController.csproj.user @@ -0,0 +1,6 @@ + + + + ProjectFiles + + \ No newline at end of file diff --git a/SDRSharp.SerialController.sln b/SDRSharp.SerialController.sln new file mode 100644 index 0000000..742eb61 --- /dev/null +++ b/SDRSharp.SerialController.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +# SharpDevelop 5.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SDRSharp.SerialController", "SDRSharp.SerialController.csproj", "{41C6CC2E-5F1D-4BFC-9676-381AD488FCD6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {41C6CC2E-5F1D-4BFC-9676-381AD488FCD6}.Debug|Any CPU.ActiveCfg = Release|x86 + {41C6CC2E-5F1D-4BFC-9676-381AD488FCD6}.Debug|Any CPU.Build.0 = Release|x86 + {41C6CC2E-5F1D-4BFC-9676-381AD488FCD6}.Release|Any CPU.ActiveCfg = Release|x86 + {41C6CC2E-5F1D-4BFC-9676-381AD488FCD6}.Release|Any CPU.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/SerialControllerPanel.Designer.cs b/SerialControllerPanel.Designer.cs new file mode 100644 index 0000000..4bfcb6d --- /dev/null +++ b/SerialControllerPanel.Designer.cs @@ -0,0 +1,120 @@ +namespace SDRSharp.SerialController +{ + partial class SerialControllerPanel + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SerialControllerPanel)); + 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 + // + this.cbEnable.Location = new System.Drawing.Point(3, 3); + this.cbEnable.Name = "cbEnable"; + this.cbEnable.Size = new System.Drawing.Size(74, 22); + this.cbEnable.TabIndex = 1; + this.cbEnable.Text = "Enable"; + this.cbEnable.UseVisualStyleBackColor = true; + this.cbEnable.Click += new System.EventHandler(this.CbEnableClick); + this.cbEnable.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CbEnableKeyDown); + // + // comboPorts + // + this.comboPorts.FormattingEnabled = true; + this.comboPorts.Location = new System.Drawing.Point(83, 3); + this.comboPorts.Name = "comboPorts"; + this.comboPorts.Size = new System.Drawing.Size(100, 21); + this.comboPorts.TabIndex = 2; + // + // btnRefreshPorts + // + 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, 23); + 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.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 = "This plugin receives AR ONE commands from UniTrunker and sets frequency according" + + "ly. Made by pewusoft, 2015"; + // + // 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.ResumeLayout(false); + + } + + #endregion + + 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; + + + } +} diff --git a/SerialControllerPanel.cs b/SerialControllerPanel.cs new file mode 100644 index 0000000..b5a9af0 --- /dev/null +++ b/SerialControllerPanel.cs @@ -0,0 +1,59 @@ +using System; +using System.Windows.Forms; + +using SDRSharp.Radio; + +namespace SDRSharp.SerialController +{ + public partial class SerialControllerPanel : UserControl + { + readonly SerialPortCtrl _serialPort; + + public SerialControllerPanel(SerialPortCtrl serialPort) + { + InitializeComponent(); + _serialPort = serialPort; + + BtnRefreshPortsClick(this, null); + } + + void BtnRefreshPortsClick(object sender, EventArgs e) { + comboPorts.Items.Clear(); + comboPorts.Items.AddRange(SerialPortCtrl.GetAllPorts()); + if (comboPorts.Items.Count > 0) + comboPorts.SelectedIndex = 0; + } + + public void addToLogList(String log) { + lbLog.Items.Add(log); + // scroll to bottom + lbLog.SelectedIndex = lbLog.Items.Count - 1; + lbLog.SelectedIndex = -1; + } + + 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) + { + cbEnable.Checked = cbEnable.Checked ? _serialPort.openPort(comboPorts.Text) : _serialPort.closePort(); + comboPorts.Enabled = !cbEnable.Checked; + btnRefreshPorts.Enabled = !cbEnable.Checked; + cbLogToFile.Enabled = !cbEnable.Checked; + _serialPort.EnableLogging = cbLogToFile.Checked; + } + void CbEnableKeyDown(object sender, EventArgs e) + { + CbEnableClick(sender, e); + } + } +} diff --git a/SerialControllerPanel.resx b/SerialControllerPanel.resx new file mode 100644 index 0000000..fcf672d --- /dev/null +++ b/SerialControllerPanel.resx @@ -0,0 +1,139 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAABl0RVh0U29m + dHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALHSURBVDhPbZNdSFNhGMctEiqzhG4q8qpuqtt0anYR + EV1kH2BRIFFdqJsfUFKpERZlJFFpmJSUpLGdzdScZY7h1xTLZrpqU5ypS3SbzTmx5tlqbufp/548Q6SL + H7zv8/F//+9z3hNBRGH2pitjkxTqF0lZalt8psqXqFA7sG5DPBVErqgtBDHhQFy68kSCnPPJywyBUq2F + NN3fSdNlo/KmQTp3V78gy1TNo+GMWJuhzMCawCZJbVeiguPvN1ioqn2MajrGqfmTgzotLuofn6OhyXnq + NE/TkfxGHs4MBy/V+SESRN9GUQBWnynKDKG8p+8pOVtDSBKsB8/e0XlrWkcEq/0njTp/0fgPL5WoTaGP + 1hmW/wOBaFEA1r0HLtZRck6tgASPxBWwFRxKUHADF0r0/MComybdPDnnfCKoCyAfJd2fNS7KH/VW4a5O + JHawuAT255mr3mEXfbC6aWqWhwDHBNZLBcGUwrdFV6s+7zl9u3Uz9quXNa+Bw57S+i+BaZzMrvLPAbeI + 3DqpKFpqWAly1YAOX274nZLf6BcpaPSzGFgrFbGBCEuEAJswO0EPYsGW/7Cd9UoCQYeHJ4fHR3bc77lu + OCSTcxOIx0hOJIqUQ9vyKgcSWQ/YIAbZN2X36hl2k3FkhlljTrKWN2IfFZehcuVUGB+k3tB1Y8/qIsUk + Jh9iAmy6djjpMjvpaKGWxyfsQ1H8kkBWcnat5/j1FvYJBZlcFWBxUQBTDrHHUvFmUJhwLYgPpn/MQ6UN + ZiGlQMvj5EW8VALC/txaQoyJaMICcCCkFev8eKaDx641LTT3TZHx2yy1fZ0mrXGKqjtsVFJnplsqEz18 + bSEcyB7b7rAAuw+ay5fWJ2WZ3Nypm++8xZyJnrRYqVI/SvfqLZT7uDvIfjjMLI3VLhd4CVZJQawjQSps + dmAOLjQEsLbvy1a/QnynVEdEEX8BQVqGDn9sKuMAAAAASUVORK5CYII= + + + \ No newline at end of file diff --git a/SerialControllerPlugin.cs b/SerialControllerPlugin.cs new file mode 100644 index 0000000..e4bd7d1 --- /dev/null +++ b/SerialControllerPlugin.cs @@ -0,0 +1,53 @@ +using System; +using System.Windows.Forms; + +using SDRSharp.Common; + +namespace SDRSharp.SerialController +{ + public class SerialControllerPlugin: ISharpPlugin + { + private const string _displayName = "SerialController"; + + private SerialControllerPanel _controlPanel; + private SerialPortCtrl _serialPort; + private ISharpControl _control; + + public string DisplayName + { + get { return _displayName; } + } + + public bool HasGui + { + get { return true; } + } + + public UserControl Gui + { + get { return _controlPanel; } + } + + public void Initialize(ISharpControl control) + { + _control = control; + _serialPort = new SerialPortCtrl(); + _serialPort.OnFrequencyChange += UpdateFrequency; + + _controlPanel = new SerialControllerPanel(_serialPort); + _controlPanel.readSettings(); + } + + void UpdateFrequency(object sender, long freq) { + _control.Frequency = freq; + _controlPanel.addToLogList(freq.ToString("N0")+" Hz"); + + } + + public void Close() + { + _serialPort.closePort(); + _controlPanel.saveSettings(); + } + } +} diff --git a/SerialPort.cs b/SerialPort.cs new file mode 100644 index 0000000..ab81503 --- /dev/null +++ b/SerialPort.cs @@ -0,0 +1,138 @@ +/* + * Author: Pawel Walczak (pewusoft) + * Date: 2015-01-12 20:50 + * + */ +using System; +using System.IO.Ports; +using System.IO; +using System.Windows.Forms; + +namespace SDRSharp.SerialController +{ + /// + /// Description of SerialPort. + /// + public class SerialPortCtrl + { + bool _enableLogging = true; + public bool EnableLogging { + set { this._enableLogging = value; } + get { return this._enableLogging; } + } + + StreamWriter logger; + SerialPort _port; + + public delegate void FrequencyChangeHandler(object sender, long freq); + public event FrequencyChangeHandler OnFrequencyChange; + + public static string[] GetAllPorts() + { + try { + return SerialPort.GetPortNames(); + } catch { + MessageBox.Show("Exception while getting available serial ports", "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error); + return new string[0]; + } + } + + public bool openPort(string portName) { + try { + if (_port != null && _port.IsOpen) { + return false; + } + + if (portName == null || (portName.Trim().Equals(""))) + return false; + + _port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One); + _port.DataReceived += Port_DataReceived; + + + if (_port != null) { + _port.Open(); + if (_enableLogging) { + prepareLogger(); + log("Port " + _port.PortName + " opened"); + } + return true; + } + return false; + } catch (Exception) { + MessageBox.Show("Couldn't open port "+portName, "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; + } + } + + public bool closePort() { + if (_port != null) { + if (_port.IsOpen) { + try { + _port.Close(); + if (_enableLogging) { + log("Port " + _port.PortName + " closed"); + closeLogger(); + } + return true; + } catch (IOException) { + return false; + } + } else { + return false; + } + } + return false; + } + + void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + if (OnFrequencyChange == null) return; + + string data = _port.ReadLine(); + // log commands to file + log("Received on COM port: "+data); + // AR-ONE RF command parse, as simple as can be, but faster than regex + if (data.StartsWith("RF", StringComparison.Ordinal)) { + long freq; + if (long.TryParse(data.Substring("RF".Length), out freq)) { + 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; + } + } + } +} diff --git a/view-refresh.png b/view-refresh.png new file mode 100644 index 0000000..3fd71d6 Binary files /dev/null and b/view-refresh.png differ