2011-09-08 22:31:32 -03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.IO;
|
2012-03-23 09:52:12 -03:00
|
|
|
|
using System.Linq;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
|
|
|
|
|
namespace ArdupilotMega
|
|
|
|
|
{
|
|
|
|
|
class SerialPort : System.IO.Ports.SerialPort,ICommsSerial
|
|
|
|
|
{
|
2012-02-26 19:13:23 -04:00
|
|
|
|
public new void Open()
|
|
|
|
|
{
|
|
|
|
|
if (base.IsOpen)
|
|
|
|
|
return;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
|
2012-02-26 19:13:23 -04:00
|
|
|
|
base.Open();
|
|
|
|
|
}
|
2011-12-29 06:31:42 -04:00
|
|
|
|
|
|
|
|
|
public void toggleDTR()
|
|
|
|
|
{
|
2012-03-23 09:52:12 -03:00
|
|
|
|
bool open = this.IsOpen;
|
|
|
|
|
|
|
|
|
|
if (!open)
|
|
|
|
|
this.Open();
|
|
|
|
|
|
2012-02-20 07:30:47 -04:00
|
|
|
|
base.DtrEnable = false;
|
|
|
|
|
base.RtsEnable = false;
|
|
|
|
|
|
|
|
|
|
System.Threading.Thread.Sleep(50);
|
|
|
|
|
|
|
|
|
|
base.DtrEnable = true;
|
|
|
|
|
base.RtsEnable = true;
|
|
|
|
|
|
|
|
|
|
System.Threading.Thread.Sleep(50);
|
2012-03-23 09:52:12 -03:00
|
|
|
|
|
|
|
|
|
if (!open)
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new static string[] GetPortNames()
|
|
|
|
|
{
|
|
|
|
|
string[] monoDevs = new string[0];
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists("/dev/"))
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists("/dev/serial/by-id/"))
|
|
|
|
|
monoDevs = Directory.GetFiles("/dev/serial/by-id/", "*");
|
|
|
|
|
monoDevs = Directory.GetFiles("/dev/", "*ACM*");
|
|
|
|
|
monoDevs = Directory.GetFiles("/dev/", "ttyUSB*");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] ports = System.IO.Ports.SerialPort.GetPortNames()
|
|
|
|
|
.Select(p => p.TrimEnd())
|
|
|
|
|
.Select(FixBlueToothPortNameBug)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
string[] allPorts = new string[monoDevs.Length + ports.Length];
|
|
|
|
|
|
|
|
|
|
monoDevs.CopyTo(allPorts, 0);
|
|
|
|
|
ports.CopyTo(allPorts, monoDevs.Length);
|
|
|
|
|
|
|
|
|
|
return allPorts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// .NET bug: sometimes bluetooth ports are enumerated with bogus characters
|
|
|
|
|
// eg 'COM10' becomes 'COM10c' - one workaround is to remove the non numeric
|
|
|
|
|
// char. Annoyingly, sometimes a numeric char is added, which means this
|
|
|
|
|
// does not work in all cases.
|
|
|
|
|
// See http://connect.microsoft.com/VisualStudio/feedback/details/236183/system-io-ports-serialport-getportnames-error-with-bluetooth
|
|
|
|
|
private static string FixBlueToothPortNameBug(string portName)
|
|
|
|
|
{
|
|
|
|
|
if (!portName.StartsWith("COM"))
|
|
|
|
|
return portName;
|
|
|
|
|
var newPortName = "COM"; // Start over with "COM"
|
|
|
|
|
foreach (var portChar in portName.Substring(3).ToCharArray()) // Remove "COM", put the rest in a character array
|
|
|
|
|
{
|
|
|
|
|
if (char.IsDigit(portChar))
|
|
|
|
|
newPortName += portChar.ToString(); // Good character, append to portName
|
|
|
|
|
// else
|
|
|
|
|
//log.WarnFormat("Bad (Non Numeric) character in port name '{0}' - removing", portName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newPortName;
|
2011-12-29 06:31:42 -04:00
|
|
|
|
}
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
}
|