2010-12-18 18:23:09 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using ArducopterConfigurator.PresentationModels;
|
|
|
|
|
|
|
|
|
|
namespace ArducopterConfigurator
|
|
|
|
|
{
|
2010-12-20 18:14:39 -04:00
|
|
|
|
public interface IComms
|
|
|
|
|
{
|
|
|
|
|
event Action<string> LineOfDataReceived;
|
|
|
|
|
string CommPort { get; set; }
|
2010-12-23 11:59:00 -04:00
|
|
|
|
bool IsConnected { get; }
|
|
|
|
|
IEnumerable<string> ListCommPorts();
|
2010-12-20 18:14:39 -04:00
|
|
|
|
void Send(string send);
|
|
|
|
|
bool Connect();
|
|
|
|
|
bool DisConnect();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a session of communication with the Arducopter
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Looks after connection state etc
|
|
|
|
|
/// </remarks>
|
2010-12-20 18:14:39 -04:00
|
|
|
|
public class CommsSession : IComms
|
2010-12-18 18:23:09 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly SerialPort _sp;
|
|
|
|
|
private BackgroundWorker _bgWorker;
|
|
|
|
|
|
|
|
|
|
public event Action<string> LineOfDataReceived;
|
|
|
|
|
|
|
|
|
|
public CommsSession()
|
|
|
|
|
{
|
|
|
|
|
_sp = new SerialPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CommPort { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
_sp.BaudRate = 115200;
|
|
|
|
|
_sp.PortName = CommPort;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_sp.Open();
|
|
|
|
|
_sp.ReadTimeout = 50000;
|
|
|
|
|
|
|
|
|
|
// start the reading BG thread
|
|
|
|
|
_bgWorker = new BackgroundWorker();
|
|
|
|
|
_bgWorker.DoWork += bgWorker_DoWork;
|
|
|
|
|
_bgWorker.WorkerReportsProgress = true;
|
|
|
|
|
_bgWorker.WorkerSupportsCancellation = true;
|
|
|
|
|
_bgWorker.ProgressChanged += bgWorker_ProgressChanged;
|
|
|
|
|
_bgWorker.RunWorkerAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Error = ex.Message;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool DisConnect()
|
|
|
|
|
{
|
|
|
|
|
_bgWorker.CancelAsync();
|
|
|
|
|
|
|
|
|
|
_sp.Close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Thanks to BG worker, this should be raised on the UI thread
|
2011-01-26 17:17:44 -04:00
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
|
var lineReceived = e.UserState as string;
|
2011-01-26 17:17:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i < lineReceived.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// var c = lineReceived[i];
|
|
|
|
|
// Console.WriteLine("{0}] U+{1:x4} {2}", i, (int)c, (int)c);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
if (LineOfDataReceived != null)
|
2010-12-18 18:23:09 -04:00
|
|
|
|
LineOfDataReceived(lineReceived);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
while (!_bgWorker.CancellationPending)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var line = _sp.ReadLine();
|
|
|
|
|
_bgWorker.ReportProgress(0, line);
|
|
|
|
|
}
|
|
|
|
|
catch(TimeoutException)
|
|
|
|
|
{
|
|
|
|
|
// continue
|
|
|
|
|
}
|
|
|
|
|
catch(System.IO.IOException) // when the port gets killed
|
|
|
|
|
{}
|
|
|
|
|
catch(ObjectDisposedException)
|
|
|
|
|
{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Error { get; set;}
|
|
|
|
|
|
2010-12-23 11:59:00 -04:00
|
|
|
|
|
|
|
|
|
public bool IsConnected
|
|
|
|
|
{
|
|
|
|
|
get { return _sp.IsOpen; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> ListCommPorts()
|
2010-12-18 18:23:09 -04:00
|
|
|
|
{
|
2010-12-23 11:59:00 -04:00
|
|
|
|
return SerialPort.GetPortNames();
|
2010-12-18 18:23:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send(string send)
|
|
|
|
|
{
|
|
|
|
|
if (_sp.IsOpen)
|
|
|
|
|
_sp.Write(send);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-20 18:14:39 -04:00
|
|
|
|
|
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
|
}
|