Skip to content

Commit

Permalink
Integration tests completed for FindSerialConnection methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Henk van Boeijen committed Feb 11, 2015
1 parent cb63365 commit 8d7f9e6
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 30 deletions.
Binary file modified Documentation/Help/SolidSoils4Arduino.chm
Binary file not shown.
Binary file added Documentation/Help/icons/protdelegate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion Solid.Arduino.IntegrationTest/SerialConnectionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public void FindSerialConnection_Serial()
using (var arduinoConnection = SerialConnection.FindSerialConnection("Hello?", "Arduino!"))
{
Assert.IsNotNull(arduinoConnection);
Assert.AreEqual(9600, arduinoConnection.BaudRate);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Solid.Arduino.Test/ArduinoSessionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void CreateSessionWithClosedConnection()
Assert.AreEqual(true, connection.IsOpen);
}

[TestMethod]
//[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ArduinoSession_Constructor_NullArgument()
{
Expand Down
50 changes: 50 additions & 0 deletions Solid.Arduino.Test/EnhancedSerialConnectionTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Solid.Arduino;

namespace Solid.Arduino.Test
{
[TestClass]
public class EnhancedSerialConnectionTester
{
[TestMethod]
public void EnhancedSerialConnection_Constructor_WithoutParameters()
{
var connection = new EnhancedSerialConnection();
Assert.AreEqual(100, connection.ReadTimeout);
Assert.AreEqual(100, connection.WriteTimeout);
Assert.AreEqual(115200, connection.BaudRate);
}

[TestMethod]
public void EnhancedSerialConnection_Constructor_WithParameters()
{
var connection = new EnhancedSerialConnection("COM1", SerialBaudRate.Bps_115200);
Assert.AreEqual(100, connection.ReadTimeout);
Assert.AreEqual(100, connection.WriteTimeout);
Assert.AreEqual(115200, connection.BaudRate);
}

[TestMethod]
public void EnhancedSerialConnection_OpenAndClose()
{
var connection = new EnhancedSerialConnection();
connection.Open();
connection.Close();
}

[TestMethod]
public void EnhancedSerialConnection_OpenAndDoubleClose()
{
var connection = new EnhancedSerialConnection();
connection.Open();
connection.Close();
connection.Close();
}
}
}
19 changes: 18 additions & 1 deletion Solid.Arduino.Test/MockSerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Solid.Arduino.Test
{
internal class MockSerialConnection: ISerialConnection
{
private static readonly ConstructorInfo _serialDataReceivedEventArgsConstructor = typeof(SerialDataReceivedEventArgs)
private static readonly ConstructorInfo SerialDataReceivedEventArgsConstructor = typeof(SerialDataReceivedEventArgs)
.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerialData) }, null);

private bool _isOpen;
Expand All @@ -25,6 +25,18 @@ internal class MockSerialConnection: ISerialConnection

#region ISerialConnection Members

public int BaudRate
{
get { return 9600; }
set {}
}

public string PortName
{
get { return "COM3"; }
set {}
}

public event SerialDataReceivedEventHandler DataReceived;

public bool IsOpen
Expand Down Expand Up @@ -125,6 +137,11 @@ public void WriteLine(string text)
}
}

public void Dispose()
{

}

#endregion

#region Public Testmethods
Expand Down
10 changes: 6 additions & 4 deletions Solid.Arduino.Test/SerialConnectionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ public class SerialConnectionTester
[TestMethod]
public void SerialConnection_Constructor_WithoutParameters()
{
var connection = new EnhancedSerialConnection();
var connection = new SerialConnection();
Assert.AreEqual(100, connection.ReadTimeout);
Assert.AreEqual(100, connection.WriteTimeout);
Assert.AreEqual(115200, connection.BaudRate);
}

[TestMethod]
public void SerialConnection_Constructor_WithParameters()
{
var connection = new EnhancedSerialConnection("COM1", SerialBaudRate.Bps_115200);
var connection = new SerialConnection("COM1", SerialBaudRate.Bps_115200);
Assert.AreEqual(100, connection.ReadTimeout);
Assert.AreEqual(100, connection.WriteTimeout);
Assert.AreEqual(115200, connection.BaudRate);
}

[TestMethod]
public void SerialConnection_OpenAndClose()
{
var connection = new EnhancedSerialConnection();
var connection = new SerialConnection();
connection.Open();
connection.Close();
}

[TestMethod]
public void SerialConnection_OpenAndDoubleClose()
{
var connection = new EnhancedSerialConnection();
var connection = new SerialConnection();
connection.Open();
connection.Close();
connection.Close();
Expand Down
1 change: 1 addition & 0 deletions Solid.Arduino.Test/Solid.Arduino.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensionsTester.cs" />
<Compile Include="SerialConnectionTester.cs" />
<Compile Include="EnhancedSerialConnectionTester.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Solid.Arduino\Solid.Arduino.csproj">
Expand Down
10 changes: 5 additions & 5 deletions Solid.Arduino/ArduinoSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ private void I2CRead(bool continuous, int slaveAddress, int slaveRegister = -1,
/// </summary>
private void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
{
while (_connection.BytesToRead > 0)
while (_connection.IsOpen && _connection.BytesToRead > 0)
{
int serialByte = _connection.ReadByte();

Expand Down Expand Up @@ -956,13 +956,13 @@ private void ProcessAsciiString(int serialByte)

if (_currentStringRequest == null)
{
_awaitedStringsQueue.TryDequeue(out _currentStringRequest);

if (_currentStringRequest == null)
if (!_awaitedStringsQueue.TryDequeue(out _currentStringRequest))
{
// No pending Read/ReadLine/ReadTo requests.
// Handle StringReceived event.
if (c == _connection.NewLine[_connection.NewLine.Length - 1] || serialByte == 0x1A) // NewLine or EOF?
if (c == _connection.NewLine[_connection.NewLine.Length - 1]
|| serialByte == 0x1A
|| serialByte == 0x00) // NewLine, EOF or terminating 0-byte?
{
if (StringReceived != null)
StringReceived(this, new StringEventArgs(new string(_stringBuffer, 0, _stringBufferIndex - 1)));
Expand Down
12 changes: 12 additions & 0 deletions Solid.Arduino/EnhancedSerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public EnhancedSerialConnection(string portName, SerialBaudRate baudRate)

#region Public Methods & Properties

/// <inheritdoc cref="SerialConnection.FindSerialConnection()"/>
public static ISerialConnection FindSerialConnection()
{
return SerialConnection.FindSerialConnection();
}

/// <inheritdoc cref="SerialConnection.FindSerialConnection(string, string)"/>
public static ISerialConnection FindSerialConnection(string query, string expectedReply)
{
return SerialConnection.FindSerialConnection(query, expectedReply);
}

/// <inheritdoc cref="SerialPort.Close"/>
public new void Close()
{
Expand Down
8 changes: 7 additions & 1 deletion Solid.Arduino/ISerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ namespace Solid.Arduino
/// Defines a serial port connection.
/// </summary>
/// <seealso href="http://arduino.cc/en/Reference/Serial">Serial reference for Arduino</seealso>
public interface ISerialConnection
public interface ISerialConnection: IDisposable
{
/// <summary>
/// Represents the method that will handle the data received event of a <see cref="ISerialConnection"/> object.
/// </summary>
event SerialDataReceivedEventHandler DataReceived;

/// <inheritdoc cref="SerialPort.BaudRate"/>
int BaudRate { get; set; }

/// <inheritdoc cref="SerialPort.PortName"/>
string PortName { get; set; }

/// <summary>
/// Gets a value indicating the open or closed status of the <see cref="ISerialConnection"/> object.
/// </summary>
Expand Down
34 changes: 17 additions & 17 deletions Solid.Arduino/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public SerialConnection(string portName, SerialBaudRate baudRate)
/// <inheritdoc cref="SerialPort.Close"/>
public new void Close()
{
if (IsOpen)
{
BaseStream.Flush();
DiscardInBuffer();
}
if (!IsOpen)
return;

base.Close();
Thread.Sleep(250);
BaseStream.Flush();
DiscardInBuffer();
BaseStream.Close();
base.Close();
}

/// <inheritdoc cref="SerialPort.Dispose"/>
Expand All @@ -108,10 +108,10 @@ public SerialConnection(string portName, SerialBaudRate baudRate)
return;

_isDisposed = true;
base.Dispose();
BaseStream.Dispose();
GC.SuppressFinalize(this);
GC.SuppressFinalize(BaseStream);
base.Dispose();
GC.SuppressFinalize(this);
}

/// <inheritdoc cref="ISerialConnection.Write(string)" />
Expand All @@ -120,7 +120,7 @@ public SerialConnection(string portName, SerialBaudRate baudRate)
/// <summary>
/// Finds a serial connection to a device supporting the Firmata protocol.
/// </summary>
/// <returns>A <see cref="SerialConnection"/> instance or <c>null</c> if no connection is found</returns>
/// <returns>A <see cref="ISerialConnection"/> instance or <c>null</c> if no connection is found</returns>
/// <remarks>
/// <para>
/// This method searches all available serial ports until it finds a working serial connection.
Expand All @@ -135,7 +135,7 @@ public SerialConnection(string portName, SerialBaudRate baudRate)
/// </remarks>
/// <seealso cref="IFirmataProtocol"/>
/// <seealso href="http://www.firmata.org/wiki/Protocol#Query_Firmware_Name_and_Version">Query Firmware Name and Version</seealso>
public static SerialConnection FindSerialConnection()
public static ISerialConnection FindSerialConnection()
{
Func<ArduinoSession, bool> isAvailableFunc = session =>
{
Expand All @@ -144,7 +144,7 @@ public static SerialConnection FindSerialConnection()
};

string[] portNames = GetPortNames();
SerialConnection connection = FindConnection(isAvailableFunc, portNames, PopularBaudRates);
ISerialConnection connection = FindConnection(isAvailableFunc, portNames, PopularBaudRates);
return connection ?? FindConnection(isAvailableFunc, portNames, OtherBaudRates);
}

Expand All @@ -153,7 +153,7 @@ public static SerialConnection FindSerialConnection()
/// </summary>
/// <param name="query">The query text used to inquire the connection</param>
/// <param name="expectedReply">The reply text the connected device is expected to respond with</param>
/// <returns>A <see cref="SerialConnection"/> instance or <c>null</c> if no connection is found</returns>
/// <returns>A <see cref="ISerialConnection"/> instance or <c>null</c> if no connection is found</returns>
/// <remarks>
/// <para>
/// This method searches all available serial ports until it finds a working serial connection.
Expand Down Expand Up @@ -195,7 +195,7 @@ public static SerialConnection FindSerialConnection()
/// </code>
/// </example>
/// <seealso cref="IStringProtocol"/>
public static SerialConnection FindSerialConnection(string query, string expectedReply)
public static ISerialConnection FindSerialConnection(string query, string expectedReply)
{
if (string.IsNullOrEmpty(query))
throw new ArgumentException(Messages.ArgumentEx_NotNullOrEmpty, "query");
Expand All @@ -210,7 +210,7 @@ public static SerialConnection FindSerialConnection(string query, string expecte
};

string[] portNames = GetPortNames();
SerialConnection connection = FindConnection(isAvailableFunc, portNames, PopularBaudRates);
ISerialConnection connection = FindConnection(isAvailableFunc, portNames, PopularBaudRates);
return connection ?? FindConnection(isAvailableFunc, portNames, OtherBaudRates);
}

Expand All @@ -223,22 +223,22 @@ private static string GetHighestComPortName()
return GetPortNames().Where(n => n.StartsWith("COM")).OrderByDescending(n => n).FirstOrDefault();
}

private static SerialConnection FindConnection(Func<ArduinoSession, bool> isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates)
private static ISerialConnection FindConnection(Func<ArduinoSession, bool> isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates)
{
for (int x = portNames.Length - 1; x >= 0; x--)
{
foreach (SerialBaudRate rate in baudRates)
{
try
{
using (var connection = new SerialConnection(portNames[x], rate))
using (var connection = new EnhancedSerialConnection(portNames[x], rate))
{
using (var session = new ArduinoSession(connection, 100))
{
Debug.WriteLine("{0}:{1}; ", portNames[x], (int)rate);

if (isDeviceAvailable(session))
return new SerialConnection(portNames[x], rate);
return new EnhancedSerialConnection(portNames[x], rate);
}
}
}
Expand Down

0 comments on commit 8d7f9e6

Please sign in to comment.