mirror of
https://github.com/UzixLS/sdrsharp-catcontroller.git
synced 2026-05-04 09:56:52 +00:00
Compare commits
No commits in common. "master" and "1.4" have entirely different histories.
@ -1,25 +1,27 @@
|
|||||||
using System;
|
using System.Reflection;
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
// General Information about an assembly is controlled through the following
|
// General Information about an assembly is controlled through the following
|
||||||
// set of attributes. Change these attribute values to modify the information
|
// set of attributes. Change these attribute values to modify the information
|
||||||
// associated with an assembly.
|
// associated with an assembly.
|
||||||
[assembly: AssemblyTitle ("SerialController")]
|
[assembly: AssemblyTitle("SerialController")]
|
||||||
[assembly: AssemblyDescription ("Serial port controller for SDR#")]
|
[assembly: AssemblyDescription("Serial port controller for SDR#")]
|
||||||
[assembly: AssemblyConfiguration ("")]
|
[assembly: AssemblyConfiguration("")]
|
||||||
[assembly: AssemblyCompany ("")]
|
[assembly: AssemblyCompany("")]
|
||||||
[assembly: AssemblyProduct ("SerialController")]
|
[assembly: AssemblyProduct("SerialController")]
|
||||||
[assembly: AssemblyCopyright ("")]
|
[assembly: AssemblyCopyright("Copyright © Pawel Walczak 2014")]
|
||||||
[assembly: AssemblyTrademark ("")]
|
[assembly: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture ("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
// Setting ComVisible to false makes the types in this assembly not visible
|
// 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
|
// to COM components. If you need to access a type in this assembly from
|
||||||
// COM, set the ComVisible attribute to true on that type.
|
// 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
|
// 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:
|
// Version information for an assembly consists of the following four values:
|
||||||
//
|
//
|
||||||
// Major Version
|
// Major Version
|
||||||
@ -30,5 +32,5 @@ using System.Runtime.InteropServices;
|
|||||||
// You can specify all the values or you can default the Build and Revision Numbers
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
// by using the '*' as shown below:
|
// by using the '*' as shown below:
|
||||||
// [assembly: AssemblyVersion("1.0.*")]
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
[assembly: AssemblyVersion ("1.0.0.0")]
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
[assembly: AssemblyFileVersion ("1.0.0.0")]
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
|
|||||||
@ -1,18 +0,0 @@
|
|||||||
/*
|
|
||||||
* Created by SharpDevelop.
|
|
||||||
* User: uzix
|
|
||||||
* Date: 04.01.2017
|
|
||||||
* Time: 16:02
|
|
||||||
*/
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace SDRSharp.SerialController
|
|
||||||
{
|
|
||||||
public interface ProtocolInterface
|
|
||||||
{
|
|
||||||
string EndMarker { get; }
|
|
||||||
int MaxLen { get; }
|
|
||||||
string PktTransmitter(string ChangedProperty);
|
|
||||||
string PktReceiver(string ReveivedData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
123
Protocol_TS50.cs
123
Protocol_TS50.cs
@ -1,123 +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
|
|
||||||
{
|
|
||||||
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, 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},
|
|
||||||
{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)
|
|
||||||
{
|
|
||||||
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 ReceivedData)
|
|
||||||
{
|
|
||||||
string response = "";
|
|
||||||
if (ReceivedData.StartsWith("IF", StringComparison.Ordinal)) {
|
|
||||||
response += "IF";
|
|
||||||
response += String.Format("{0:00000000000}", _radio.RadioFrequency);
|
|
||||||
response += "0000000000000000";
|
|
||||||
if ( _DetectorSetFailure)
|
|
||||||
response += 0;
|
|
||||||
else
|
|
||||||
response += mode2int[_radio.RadioMode];
|
|
||||||
response += "0000000";
|
|
||||||
response += EndMarker;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -101,9 +101,8 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ProtocolInterface.cs" />
|
<Compile Include="SerialPktProcessor.cs" />
|
||||||
<Compile Include="Protocol_TS50.cs" />
|
<Compile Include="SerialPort.cs" />
|
||||||
<Compile Include="SerialPortCtrl.cs" />
|
|
||||||
<!--<Reference Include="SDRSharp.PanView, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
|
<!--<Reference Include="SDRSharp.PanView, Version=0.0.0.0, Culture=neutral, processorArchitecture=x86">
|
||||||
<SpecificVersion>False</SpecificVersion>
|
<SpecificVersion>False</SpecificVersion>
|
||||||
<HintPath>../Common/SDRSharp.PanView.dll</HintPath>
|
<HintPath>../Common/SDRSharp.PanView.dll</HintPath>
|
||||||
@ -116,7 +115,6 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="SerialControllerPlugin.cs" />
|
<Compile Include="SerialControllerPlugin.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="SerialRadioInterface.cs" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<EmbeddedResource Include="SerialControllerPanel.resx">
|
<EmbeddedResource Include="SerialControllerPanel.resx">
|
||||||
|
|||||||
@ -3,7 +3,6 @@ using System.Windows.Forms;
|
|||||||
|
|
||||||
using SDRSharp.Radio;
|
using SDRSharp.Radio;
|
||||||
|
|
||||||
|
|
||||||
namespace SDRSharp.SerialController
|
namespace SDRSharp.SerialController
|
||||||
{
|
{
|
||||||
public partial class SerialControllerPanel : UserControl
|
public partial class SerialControllerPanel : UserControl
|
||||||
@ -26,7 +25,7 @@ namespace SDRSharp.SerialController
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addToLogList(String log) {
|
public void addToLogList(String log) {
|
||||||
lbLog.Items.Add(DateTime.Now.ToString("[HH:mm:ss] ") + log);
|
lbLog.Items.Add(log);
|
||||||
// scroll to bottom
|
// scroll to bottom
|
||||||
lbLog.SelectedIndex = lbLog.Items.Count - 1;
|
lbLog.SelectedIndex = lbLog.Items.Count - 1;
|
||||||
lbLog.SelectedIndex = -1;
|
lbLog.SelectedIndex = -1;
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
using SDRSharp.Common;
|
using SDRSharp.Common;
|
||||||
@ -8,19 +7,15 @@ using SDRSharp.Radio;
|
|||||||
|
|
||||||
namespace SDRSharp.SerialController
|
namespace SDRSharp.SerialController
|
||||||
{
|
{
|
||||||
public class SerialControllerPlugin: ISharpPlugin,SerialRadioInterface
|
public class SerialControllerPlugin: ISharpPlugin
|
||||||
{
|
{
|
||||||
private const string _displayName = "SerialController";
|
private const string _displayName = "SerialController";
|
||||||
|
|
||||||
private ISharpControl _control;
|
private ISharpControl _control;
|
||||||
private SerialControllerPanel _controlPanel;
|
private SerialControllerPanel _controlPanel;
|
||||||
private SerialPortCtrl _serialPort;
|
private SerialPortCtrl _serialPort;
|
||||||
private ProtocolInterface _Protocol;
|
private SerialPktProcessor _serialPktProcessor;
|
||||||
|
|
||||||
long _LastRadioFrequency;
|
|
||||||
DetectorType _LastRadioMode;
|
|
||||||
|
|
||||||
|
|
||||||
public string DisplayName
|
public string DisplayName
|
||||||
{
|
{
|
||||||
get { return _displayName; }
|
get { return _displayName; }
|
||||||
@ -39,66 +34,42 @@ namespace SDRSharp.SerialController
|
|||||||
public void Initialize(ISharpControl control)
|
public void Initialize(ISharpControl control)
|
||||||
{
|
{
|
||||||
_control = control;
|
_control = control;
|
||||||
_control.PropertyChanged += PropertyChangedHandler;
|
|
||||||
_LastRadioFrequency = _control.Frequency;
|
_serialPktProcessor = new SerialPktProcessor();
|
||||||
_LastRadioMode = _control.DetectorType;
|
_serialPktProcessor.OnFrequencyChange += UpdateFrequency;
|
||||||
_Protocol = new Protocol_TS50(this);
|
_serialPktProcessor.OnGetFrequency += GetFrequency;
|
||||||
_serialPort = new SerialPortCtrl(_Protocol);
|
_serialPktProcessor.OnModeChange += UpdateDemodulation;
|
||||||
|
_serialPktProcessor.OnGetMode += GetDemodulation;
|
||||||
|
|
||||||
|
_serialPort = new SerialPortCtrl(_serialPktProcessor);
|
||||||
|
_serialPort.separator = _serialPktProcessor.separator;
|
||||||
|
|
||||||
_controlPanel = new SerialControllerPanel(_serialPort);
|
_controlPanel = new SerialControllerPanel(_serialPort);
|
||||||
_controlPanel.readSettings();
|
_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()
|
public void Close()
|
||||||
{
|
{
|
||||||
_serialPort.closePort();
|
_serialPort.closePort();
|
||||||
_controlPanel.saveSettings();
|
_controlPanel.saveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
if (! string.IsNullOrEmpty(response))
|
|
||||||
_serialPort.DataTransmit(response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public long RadioFrequency
|
|
||||||
{
|
|
||||||
get {
|
|
||||||
return _control.Frequency;
|
|
||||||
}
|
|
||||||
set {
|
|
||||||
_LastRadioFrequency = value;
|
|
||||||
_controlPanel.addToLogList(value.ToString("N0")+" Hz");
|
|
||||||
_control.Frequency = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public DetectorType RadioMode
|
|
||||||
{
|
|
||||||
get {
|
|
||||||
return _control.DetectorType;
|
|
||||||
}
|
|
||||||
set {
|
|
||||||
_LastRadioMode = value;
|
|
||||||
_controlPanel.addToLogList(value.ToString());
|
|
||||||
_control.DetectorType = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
81
SerialPktProcessor.cs
Normal file
81
SerialPktProcessor.cs
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
106
SerialPort.cs
Normal file
106
SerialPort.cs
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
{
|
||||||
|
/// <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;
|
||||||
|
|
||||||
|
public SerialPortCtrl( SerialPktProcessor pktprocessor )
|
||||||
|
{
|
||||||
|
_pktprocessor = pktprocessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
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 += new SerialDataReceivedEventHandler( Port_DataReceived );
|
||||||
|
|
||||||
|
if (_port != null) {
|
||||||
|
_port.Open();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
MessageBox.Show("Couldn't open port "+portName+":\n"+e.ToString(), "SerialController",
|
||||||
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool closePort() {
|
||||||
|
if (_port != null) {
|
||||||
|
if (_port.IsOpen) {
|
||||||
|
try {
|
||||||
|
_port.Close();
|
||||||
|
return true;
|
||||||
|
} catch (IOException) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||||
|
{
|
||||||
|
string data = "";
|
||||||
|
while (data.IndexOf(_separator) < 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(_separator));
|
||||||
|
|
||||||
|
string response = _pktprocessor.process(data);
|
||||||
|
if (! string.IsNullOrEmpty(response))
|
||||||
|
_port.Write(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,110 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
{
|
|
||||||
public class SerialPortCtrl
|
|
||||||
{
|
|
||||||
public bool IsOpen {
|
|
||||||
get { return _port != null && _port.IsOpen; }
|
|
||||||
}
|
|
||||||
|
|
||||||
SerialPort _port;
|
|
||||||
ProtocolInterface _protocol;
|
|
||||||
string _received;
|
|
||||||
|
|
||||||
public SerialPortCtrl(ProtocolInterface protocol)
|
|
||||||
{
|
|
||||||
_protocol = protocol;
|
|
||||||
_received = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string[] GetAllPorts()
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return SerialPort.GetPortNames();
|
|
||||||
}
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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 += new SerialDataReceivedEventHandler( DataReceivedHandler );
|
|
||||||
|
|
||||||
if (_port != null) {
|
|
||||||
_port.Open();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
MessageBox.Show("Couldn't open port "+portName+":\n"+e.ToString(), "SerialController",
|
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool closePort()
|
|
||||||
{
|
|
||||||
if (_port != null) {
|
|
||||||
if (_port.IsOpen) {
|
|
||||||
try {
|
|
||||||
_port.Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (IOException) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
|
|
||||||
{
|
|
||||||
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 = "";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_received += input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DataTransmit(string data)
|
|
||||||
{
|
|
||||||
_port.Write(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user