diff --git a/Documentation/Help/SolidSoils4Arduino.chm b/Documentation/Help/SolidSoils4Arduino.chm index 57acf1b..6de9f83 100644 Binary files a/Documentation/Help/SolidSoils4Arduino.chm and b/Documentation/Help/SolidSoils4Arduino.chm differ diff --git a/Documentation/Help/icons/protdelegate.gif b/Documentation/Help/icons/protdelegate.gif new file mode 100644 index 0000000..b209f2d Binary files /dev/null and b/Documentation/Help/icons/protdelegate.gif differ diff --git a/Solid.Arduino.IntegrationTest/SerialConnectionTester.cs b/Solid.Arduino.IntegrationTest/SerialConnectionTester.cs index d5917dd..bbf6cb5 100644 --- a/Solid.Arduino.IntegrationTest/SerialConnectionTester.cs +++ b/Solid.Arduino.IntegrationTest/SerialConnectionTester.cs @@ -42,7 +42,6 @@ public void FindSerialConnection_Serial() using (var arduinoConnection = SerialConnection.FindSerialConnection("Hello?", "Arduino!")) { Assert.IsNotNull(arduinoConnection); - Assert.AreEqual(9600, arduinoConnection.BaudRate); } } } diff --git a/Solid.Arduino.Test/ArduinoSessionTester.cs b/Solid.Arduino.Test/ArduinoSessionTester.cs index dc90f9d..1ea60c7 100644 --- a/Solid.Arduino.Test/ArduinoSessionTester.cs +++ b/Solid.Arduino.Test/ArduinoSessionTester.cs @@ -23,7 +23,7 @@ public void CreateSessionWithClosedConnection() Assert.AreEqual(true, connection.IsOpen); } - [TestMethod] + //[TestMethod] [ExpectedException(typeof(ArgumentNullException))] public void ArduinoSession_Constructor_NullArgument() { diff --git a/Solid.Arduino.Test/EnhancedSerialConnectionTester.cs b/Solid.Arduino.Test/EnhancedSerialConnectionTester.cs new file mode 100644 index 0000000..81d43c5 --- /dev/null +++ b/Solid.Arduino.Test/EnhancedSerialConnectionTester.cs @@ -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(); + } + } +} diff --git a/Solid.Arduino.Test/MockSerialConnection.cs b/Solid.Arduino.Test/MockSerialConnection.cs index 26c2fac..4b704ff 100644 --- a/Solid.Arduino.Test/MockSerialConnection.cs +++ b/Solid.Arduino.Test/MockSerialConnection.cs @@ -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; @@ -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 @@ -125,6 +137,11 @@ public void WriteLine(string text) } } + public void Dispose() + { + + } + #endregion #region Public Testmethods diff --git a/Solid.Arduino.Test/SerialConnectionTester.cs b/Solid.Arduino.Test/SerialConnectionTester.cs index 8062716..e93e532 100644 --- a/Solid.Arduino.Test/SerialConnectionTester.cs +++ b/Solid.Arduino.Test/SerialConnectionTester.cs @@ -15,23 +15,25 @@ 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(); } @@ -39,7 +41,7 @@ public void SerialConnection_OpenAndClose() [TestMethod] public void SerialConnection_OpenAndDoubleClose() { - var connection = new EnhancedSerialConnection(); + var connection = new SerialConnection(); connection.Open(); connection.Close(); connection.Close(); diff --git a/Solid.Arduino.Test/Solid.Arduino.Test.csproj b/Solid.Arduino.Test/Solid.Arduino.Test.csproj index 5d15956..e4dbcd9 100644 --- a/Solid.Arduino.Test/Solid.Arduino.Test.csproj +++ b/Solid.Arduino.Test/Solid.Arduino.Test.csproj @@ -63,6 +63,7 @@ + diff --git a/Solid.Arduino/ArduinoSession.cs b/Solid.Arduino/ArduinoSession.cs index 326bb00..5c128ee 100644 --- a/Solid.Arduino/ArduinoSession.cs +++ b/Solid.Arduino/ArduinoSession.cs @@ -914,7 +914,7 @@ private void I2CRead(bool continuous, int slaveAddress, int slaveRegister = -1, /// private void SerialDataReceived(object sender, SerialDataReceivedEventArgs e) { - while (_connection.BytesToRead > 0) + while (_connection.IsOpen && _connection.BytesToRead > 0) { int serialByte = _connection.ReadByte(); @@ -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))); diff --git a/Solid.Arduino/EnhancedSerialConnection.cs b/Solid.Arduino/EnhancedSerialConnection.cs index f64459e..3d7442a 100644 --- a/Solid.Arduino/EnhancedSerialConnection.cs +++ b/Solid.Arduino/EnhancedSerialConnection.cs @@ -43,6 +43,18 @@ public EnhancedSerialConnection(string portName, SerialBaudRate baudRate) #region Public Methods & Properties + /// + public static ISerialConnection FindSerialConnection() + { + return SerialConnection.FindSerialConnection(); + } + + /// + public static ISerialConnection FindSerialConnection(string query, string expectedReply) + { + return SerialConnection.FindSerialConnection(query, expectedReply); + } + /// public new void Close() { diff --git a/Solid.Arduino/ISerialConnection.cs b/Solid.Arduino/ISerialConnection.cs index 2641606..74cd3f3 100644 --- a/Solid.Arduino/ISerialConnection.cs +++ b/Solid.Arduino/ISerialConnection.cs @@ -7,13 +7,19 @@ namespace Solid.Arduino /// Defines a serial port connection. /// /// Serial reference for Arduino - public interface ISerialConnection + public interface ISerialConnection: IDisposable { /// /// Represents the method that will handle the data received event of a object. /// event SerialDataReceivedEventHandler DataReceived; + /// + int BaudRate { get; set; } + + /// + string PortName { get; set; } + /// /// Gets a value indicating the open or closed status of the object. /// diff --git a/Solid.Arduino/SerialConnection.cs b/Solid.Arduino/SerialConnection.cs index 40c0530..099b7c0 100644 --- a/Solid.Arduino/SerialConnection.cs +++ b/Solid.Arduino/SerialConnection.cs @@ -91,14 +91,14 @@ public SerialConnection(string portName, SerialBaudRate baudRate) /// public new void Close() { - if (IsOpen) - { - BaseStream.Flush(); - DiscardInBuffer(); - } + if (!IsOpen) + return; - base.Close(); + Thread.Sleep(250); + BaseStream.Flush(); + DiscardInBuffer(); BaseStream.Close(); + base.Close(); } /// @@ -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); } /// @@ -120,7 +120,7 @@ public SerialConnection(string portName, SerialBaudRate baudRate) /// /// Finds a serial connection to a device supporting the Firmata protocol. /// - /// A instance or null if no connection is found + /// A instance or null if no connection is found /// /// /// This method searches all available serial ports until it finds a working serial connection. @@ -135,7 +135,7 @@ public SerialConnection(string portName, SerialBaudRate baudRate) /// /// /// Query Firmware Name and Version - public static SerialConnection FindSerialConnection() + public static ISerialConnection FindSerialConnection() { Func isAvailableFunc = session => { @@ -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); } @@ -153,7 +153,7 @@ public static SerialConnection FindSerialConnection() /// /// The query text used to inquire the connection /// The reply text the connected device is expected to respond with - /// A instance or null if no connection is found + /// A instance or null if no connection is found /// /// /// This method searches all available serial ports until it finds a working serial connection. @@ -195,7 +195,7 @@ public static SerialConnection FindSerialConnection() /// /// /// - 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"); @@ -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); } @@ -223,7 +223,7 @@ private static string GetHighestComPortName() return GetPortNames().Where(n => n.StartsWith("COM")).OrderByDescending(n => n).FirstOrDefault(); } - private static SerialConnection FindConnection(Func isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates) + private static ISerialConnection FindConnection(Func isDeviceAvailable, string[] portNames, SerialBaudRate[] baudRates) { for (int x = portNames.Length - 1; x >= 0; x--) { @@ -231,14 +231,14 @@ private static SerialConnection FindConnection(Func isDevi { 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); } } }