Skip to content

Commit

Permalink
Fix for #9
Browse files Browse the repository at this point in the history
  • Loading branch information
valkuc committed Sep 8, 2015
1 parent 45c3206 commit a2c1756
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions XOscillo/Autodetection/Autodetection.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO.Ports;

namespace XOscillo
{
class Autodetection<T> where T : Oscillo, new()
{
private static readonly bool IsMonoRuntime = (Type.GetType("Mono.Runtime") != null);

public T TryMTDetection()
{
DebugConsole.Instance.Show();

string[] ports = SerialPort.GetPortNames();
string[] ports = GetPortNames();
T[] oscillos = new T[ports.Length];
ManualResetEvent[] doneEvents = new ManualResetEvent[ports.Length];
bool[] results = new bool[ports.Length];
Expand Down Expand Up @@ -60,7 +64,7 @@ public T TryMTDetection()

public T TrySTDetection()
{
string[] ports = SerialPort.GetPortNames();
string[] ports = GetPortNames();

T oscillo = new T();

Expand All @@ -81,7 +85,7 @@ public T TrySTDetection()

public T TryManualDetection()
{
string[] ports = SerialPort.GetPortNames();
string[] ports = GetPortNames();

T oscillo = new T();

Expand Down Expand Up @@ -131,5 +135,25 @@ public T Detection()
return TryManualDetection();
}

/// <summary>
/// Retrieve available serial ports.
/// </summary>
/// <returns>Array of serial port names.</returns>
public static string[] GetPortNames()
{
/**
* Under Mono SerialPort.GetPortNames() returns /dev/ttyS* devices,
* but Arduino is detected as ttyACM* or ttyUSB*
* */
if (IsMonoRuntime)
{
var searchPattern = new Regex("ttyACM.+|ttyUSB.+");
return Directory.GetFiles("/dev").Where(f => searchPattern.IsMatch(f)).ToArray();
}
else
{
return SerialPort.GetPortNames();
}
}
}
}

0 comments on commit a2c1756

Please sign in to comment.