Skip to content

Commit

Permalink
Design improvements, network server changes and better control program
Browse files Browse the repository at this point in the history
  • Loading branch information
drkno committed Aug 31, 2014
1 parent 1fcc222 commit 2aed7a5
Show file tree
Hide file tree
Showing 13 changed files with 748 additions and 59 deletions.
2 changes: 1 addition & 1 deletion PCR1000/IComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IComm : IDisposable
/// <summary>
/// Data received in autoupdate mode.
/// </summary>
event AutoUpdateDataRecv AutoUpdateDataReceived;
event AutoUpdateDataRecv DataReceived;

/// <summary>
/// Gets and sets autoupdate mode.
Expand Down
4 changes: 2 additions & 2 deletions PCR1000/PCR1000.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IComm.cs" />
<Compile Include="PcrComm.cs" />
<Compile Include="PcrSerialComm.cs" />
<Compile Include="PcrControl.cs" />
<Compile Include="PcrDef.cs" />
<Compile Include="PcrNetwork.cs" />
<Compile Include="PcrNetworkComm.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
28 changes: 25 additions & 3 deletions PCR1000/PcrControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class PcrControl
/// </summary>
private bool _pcrStatus;

/// <summary>
/*// <summary>
/// Constructor. Initialises the class.
/// </summary>
/// <param name="port">COM Port to use.</param>
Expand Down Expand Up @@ -110,6 +110,28 @@ public PcrControl(int netport, string nethost)
_pcrRadio.PcrRfAttenuator = false;
_pcrRadio.PcrAutoUpdate = false;
_pcrStatus = false;
}*/

/// <summary>
/// Instantiates a new PCR1000 controller
/// </summary>
/// <param name="communicationChannel">Channel to use to communicate with the radio.</param>
public PcrControl(IComm communicationChannel)
{
_pcrRadio = new PRadInf();
_pcrComm = communicationChannel;
_pcrRadio.PcrVolume = 0;
_pcrRadio.PcrSquelch = 0;
_pcrRadio.PcrFreq = 146000000;
_pcrRadio.PcrMode = PcrDef.PCRMODNFM;
_pcrRadio.PcrFilter = PcrDef.PCRFLTR15;
_pcrRadio.PcrToneSq = "";
_pcrRadio.PcrToneSqFloat = 0.0f;
_pcrRadio.PcrAutoGain = false;
_pcrRadio.PcrNoiseBlank = false;
_pcrRadio.PcrRfAttenuator = false;
_pcrRadio.PcrAutoUpdate = false;
_pcrStatus = false;
}

/// <summary>
Expand Down Expand Up @@ -714,7 +736,7 @@ public bool PcrSetPort(string port)
_pcrComm.PcrClose();
try
{
_pcrComm = new PcrComm(port);
_pcrComm = new PcrSerialComm(port);
_pcrComm.PcrOpen();
_pcrComm.AutoUpdate = _pcrRadio.PcrAutoUpdate;
return true;
Expand Down Expand Up @@ -797,7 +819,7 @@ public bool PcrSetSpeed(int speed)
}
_pcrComm.Send(_pcrRadio.PcrInitSpeed);
_pcrComm.PcrClose();
_pcrComm = new PcrComm(_pcrRadio.PcrPort, speed);
_pcrComm = new PcrSerialComm(_pcrRadio.PcrPort, speed);
// investigate possible responses, i dont think one is given.
// PcrCheckResponse();
_pcrRadio.PcrSpeed = speed;
Expand Down
87 changes: 55 additions & 32 deletions PCR1000/PcrNetwork.cs → PCR1000/PcrNetworkComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Text;
Expand Down Expand Up @@ -56,7 +55,7 @@ public class PcrNetworkClient : IComm
/// <summary>
/// Password to transmit to server (if requested)
/// </summary>
private string _password;
private readonly string _password;

/// <summary>
/// Received message structure.
Expand Down Expand Up @@ -104,9 +103,9 @@ private void ListenThread()
}
}

if (AutoUpdate && AutoUpdateDataReceived != null)
if (AutoUpdate && DataReceived != null)
{
AutoUpdateDataReceived(this, DateTime.Now, datarecv);
DataReceived(this, DateTime.Now, datarecv);
}

_msgSlot2 = _msgSlot1;
Expand Down Expand Up @@ -149,7 +148,7 @@ public void Dispose()
/// <summary>
///
/// </summary>
public event AutoUpdateDataRecv AutoUpdateDataReceived;
public event AutoUpdateDataRecv DataReceived;
public bool AutoUpdate { get; set; }
public object GetRawPort()
{
Expand Down Expand Up @@ -302,12 +301,14 @@ public bool PcrClose()
/// </summary>
public class PcrNetworkServer
{
public int Port { get; protected set; }

/// <summary>
/// The serial port of the PCR1000.
/// </summary>
private readonly SerialPort _serialPort;
public IComm PortComm { get; protected set; }

private readonly TcpListener _tcpListener;
private TcpListener _tcpListener;

private TcpClient tcpClient;

Expand All @@ -317,25 +318,16 @@ public class PcrNetworkServer
/// <summary>
/// Instantiate a new PcrNetwork object to communicate with the PCR1000.
/// </summary>
/// <param name="pcrComm">Method of communication to use to connect to the radio.</param>
/// <param name="netport">Network port to communucate on. Defaults to 4456.</param>
/// <param name="port">COM Port to communicate on. Defaults to COM1.</param>
/// <param name="baud">Baud rate to use. Defaults to 9600.</param>
/// <param name="password">Password to use. Defaults to none.</param>
public PcrNetworkServer(int netport = 4456, string port = "COM1", int baud = 9600, string password = "")
public PcrNetworkServer(IComm pcrComm, int netport = 4456, string password = "")
{
Debug.WriteLine("PcrNetwork Being Created");
_password = password;
if (_password == "") _isAuthenticated = true;
_serialPort = new SerialPort(port, baud, Parity.None, 8, StopBits.One);
_serialPort.DataReceived += SerialPortDataReceived;
_serialPort.DtrEnable = true;
_serialPort.Handshake = Handshake.RequestToSend;
_serialPort.Open();

_tcpListener = new TcpListener(IPAddress.Any, netport);
var listenThread = new Thread(ListenForClients);
listenThread.Start();

Port = netport;
pcrComm.DataReceived += PcrCommOnDataReceived;
Debug.WriteLine("PcrNetwork Created");
}

Expand Down Expand Up @@ -383,7 +375,7 @@ private void ListenForCommands(object obj)
}
else
{
_serialPort.Write(cmd);
//TODO:_serialPort.Write(cmd);
}

#if DEBUG
Expand Down Expand Up @@ -420,22 +412,27 @@ public void SetDebugLogger(bool debug)
/// </summary>
/// <param name="sender">The serial port that called the method.</param>
/// <param name="e">Event arguments.</param>
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)

private void PcrCommOnDataReceived(IComm sender, DateTime recvTime, string data)
{
/*throw new NotImplementedException();
}
private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{*/
#if DEBUG
Debug.WriteLineIf(!_debugLogger, "PcrNetwork Data Recv");
#endif
try
{
var recvBuff = new byte[_serialPort.ReadBufferSize];
/*var recvBuff = new byte[_serialPort.ReadBufferSize];
_serialPort.Read(recvBuff, 0, _serialPort.ReadBufferSize);
var str = _serialPort.Encoding.GetString(recvBuff);
_serialPort.DiscardInBuffer();
_serialPort.DiscardInBuffer();*/

if (tcpClient != null)
{
var stream = tcpClient.GetStream();
stream.Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
//TODO:stream.Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
}

#if DEBUG
Expand All @@ -448,17 +445,43 @@ private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e
}
}

public void Dispose()
public bool Start()
{
Debug.WriteLine("PcrNetwork Dispose");
listenContinue = false;
_serialPort.Dispose();
Debug.WriteLine("PCR::NETS->Start");
try
{
if (listenContinue || !PortComm.PcrOpen())
{
return false;
}

listenContinue = true;
_tcpListener = new TcpListener(IPAddress.Any, Port);
var listenThread = new Thread(ListenForClients);
listenThread.Start();
}
catch (Exception)
{
return false;
}
return true;
}

public void Stop()
public bool Stop()
{
listenContinue = false;
_serialPort.Close();
Debug.WriteLine("PCR::NETS->Stop");
try
{
if (!listenContinue || !PortComm.PcrClose())
{
return false;
}
}
catch (Exception)
{
return false;
}
return true;
}
}
}
10 changes: 5 additions & 5 deletions PCR1000/PcrComm.cs → PCR1000/PcrSerialComm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace PCR1000
/// <summary>
/// PCR1000 COM Port Communications Class
/// </summary>
public class PcrComm : IComm
public class PcrSerialComm : IComm
{
/// <summary>
/// Data received in autoupdate mode.
/// </summary>
public event AutoUpdateDataRecv AutoUpdateDataReceived;
public event AutoUpdateDataRecv DataReceived;

/// <summary>
/// The serial port of the PCR1000.
Expand Down Expand Up @@ -74,7 +74,7 @@ private struct RecvMsg
/// </summary>
/// <param name="port">COM Port to communicate on. Defaults to COM1</param>
/// <param name="baud">Baud rate to use. Defaults to 9600.</param>
public PcrComm(string port = "COM1", int baud = 9600)
public PcrSerialComm(string port = "COM1", int baud = 9600)
{
Debug.WriteLine("PcrComm Being Created");
_serialPort = new SerialPort(port, baud, Parity.None, 8, StopBits.One);
Expand Down Expand Up @@ -128,9 +128,9 @@ private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e
_serialPort.DiscardInBuffer();
str = str.Trim(TrimChars);

if (AutoUpdate && AutoUpdateDataReceived != null)
if (AutoUpdate && DataReceived != null)
{
AutoUpdateDataReceived(this, DateTime.Now, str);
DataReceived(this, DateTime.Now, str);
}

_msgSlot2 = _msgSlot1;
Expand Down
2 changes: 1 addition & 1 deletion PCR1000LibTest/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Form1()
InitializeComponent();
}

readonly PcrControl _pcrControl = new PcrControl("COM1", 9600);
readonly PcrControl _pcrControl = new PcrControl(new PcrSerialComm());

private void button1_Click(object sender, EventArgs e)
{
Expand Down
Loading

0 comments on commit 2aed7a5

Please sign in to comment.