Skip to content

Commit

Permalink
UDPMavlinkShim: autoconnect to mavlink udp 14550
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Feb 22, 2017
1 parent b062fb8 commit e1a4e7c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
29 changes: 8 additions & 21 deletions MainV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ public void doDisconnect(MAVLinkInterface comPort)
public void doConnect(MAVLinkInterface comPort, string portname, string baud)
{
bool skipconnectcheck = false;
log.Info("We are connecting");
log.Info("We are connecting to " + portname + " " + baud );
switch (portname)
{
case "preset":
Expand Down Expand Up @@ -1325,7 +1325,8 @@ public void doConnect(MAVLinkInterface comPort, string portname, string baud)
log.Info("Set Baudrate");
try
{
comPort.BaseStream.BaudRate = int.Parse(baud);
if(baud != "")
comPort.BaseStream.BaudRate = int.Parse(baud);
}
catch (Exception exp)
{
Expand Down Expand Up @@ -1611,13 +1612,13 @@ void loadph_serial()
foreach (var serial in serials)
{
if (serial.Contains(comPort.MAV.SerialString.Substring(comPort.MAV.SerialString.Length - 26, 26)) &&
!Settings.Instance.ContainsKey(comPort.MAV.SerialString))
!Settings.Instance.ContainsKey(comPort.MAV.SerialString.Replace(" ", "")))
{
CustomMessageBox.Show(
"Your board has a Critical service bulletin please see [link;http://discuss.ardupilot.org/t/sb-0000001-critical-service-bulletin-for-beta-cube-2-1/14711;Click here]",
Strings.ERROR);

Settings.Instance[comPort.MAV.SerialString] = true.ToString();
Settings.Instance[comPort.MAV.SerialString.Replace(" ","")] = true.ToString();
}
}
}
Expand All @@ -1633,31 +1634,14 @@ private void CMB_serialport_SelectedIndexChanged(object sender, EventArgs e)
if (comPortName == "UDP" || comPortName == "UDPCl" || comPortName == "TCP" || comPortName == "AUTO")
{
_connectionControl.CMB_baudrate.Enabled = false;
if (comPortName == "TCP")
MainV2.comPort.BaseStream = new TcpSerial();
if (comPortName == "UDP")
MainV2.comPort.BaseStream = new UdpSerial();
if (comPortName == "UDPCl")
MainV2.comPort.BaseStream = new UdpSerialConnect();
if (comPortName == "AUTO")
{
MainV2.comPort.BaseStream = new SerialPort();
return;
}
}
else
{
_connectionControl.CMB_baudrate.Enabled = true;
MainV2.comPort.BaseStream = new SerialPort();
}

try
{
if (!String.IsNullOrEmpty(_connectionControl.CMB_serialport.Text))
comPort.BaseStream.PortName = _connectionControl.CMB_serialport.Text;

MainV2.comPort.BaseStream.BaudRate = int.Parse(_connectionControl.CMB_baudrate.Text);

// check for saved baud rate and restore
if (Settings.Instance[_connectionControl.CMB_serialport.Text + "_BAUD"] != null)
{
Expand Down Expand Up @@ -2752,6 +2736,9 @@ protected override void OnLoad(EventArgs e)
// start listener
UDPVideoShim.Start();

log.Info("start udpmavlinkshim");
UDPMavlinkShim.Start();

try
{
log.Info("Load AltitudeAngel");
Expand Down
1 change: 1 addition & 0 deletions MissionPlanner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@
<Compile Include="Utilities\Proximity.cs" />
<Compile Include="Utilities\Tools.cs" />
<Compile Include="Utilities\ubx_m8p.cs" />
<Compile Include="Utilities\UDPMavlinkShim.cs" />
<Compile Include="Utilities\UDPVideoShim.cs" />
<Compile Include="Wizard\10FlightModes.cs">
<SubType>UserControl</SubType>
Expand Down
85 changes: 85 additions & 0 deletions Utilities/UDPMavlinkShim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using log4net;

namespace MissionPlanner.Utilities
{
public class UDPMavlinkShim
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

private static UdpClient client;

static UDPMavlinkShim()
{
try
{
client = new UdpClient(14550, AddressFamily.InterNetwork);
client.BeginReceive(clientdata, client);
}
catch
{
}
}

private static void clientdata(IAsyncResult ar)
{
var client = ((UdpClient) ar.AsyncState);

if (client == null || client.Client == null)
return;

var port = ((IPEndPoint) client.Client.LocalEndPoint).Port;

//if (client != null)
//client.Close();

try
{
var udpclient = new Comms.UdpSerial(client);

MainV2.instance.BeginInvoke((Action) delegate
{
if (MainV2.comPort.BaseStream.IsOpen)
{
var mav = new MAVLinkInterface();
mav.BaseStream = udpclient;
MainV2.instance.doConnect(mav, "preset", port.ToString());

MainV2.Comports.Add(mav);
}
else
{
MainV2.comPort.BaseStream = udpclient;
MainV2.instance.doConnect(MainV2.comPort, "preset", port.ToString());
}
});
}
catch (Exception ex)
{
log.Error(ex);
}
}

public static void Start()
{
}

public static void Stop()
{
try
{
if (client!= null)
client.Close();
}
catch
{
}
}
}
}

0 comments on commit e1a4e7c

Please sign in to comment.