Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operators for reading and writing binary data from a serial port #959

Merged
merged 13 commits into from
Jul 4, 2023
2 changes: 1 addition & 1 deletion Bonsai.Arduino/ArduinoConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.ComponentModel;
using Bonsai.IO;
using Bonsai.IO.Ports;

namespace Bonsai.Arduino
{
Expand Down
4 changes: 2 additions & 2 deletions Bonsai.Arduino/CreateArduino.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Bonsai.IO;
using System;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using Bonsai.IO.Ports;

namespace Bonsai.Arduino
{
Expand Down
2 changes: 1 addition & 1 deletion Bonsai.System/IO/Ports/BaudRateConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel;

namespace Bonsai.IO
namespace Bonsai.IO.Ports
{
/// <summary>
/// Provides a type converter to convert serial baud rates to and from other representations.
Expand Down
7 changes: 7 additions & 0 deletions Bonsai.System/IO/Ports/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Bonsai.IO.Ports
{
internal class Constants
{
public const string XmlNamespace = "clr-namespace:Bonsai.IO.Ports;assembly=Bonsai.System";
}
}
14 changes: 13 additions & 1 deletion Bonsai.System/IO/Ports/CreateSerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
using System.IO.Ports;
using System.Linq;
using System.Reactive.Linq;
using System.Xml.Serialization;

namespace Bonsai.IO
namespace Bonsai.IO.Ports
{
/// <summary>
/// Represents an operator that creates and configures a connection to a system serial port.
/// </summary>
[DefaultProperty(nameof(Name))]
[XmlType(Namespace = Constants.XmlNamespace)]
[Description("Creates and configures a connection to a system serial port.")]
public class CreateSerialPort : Source<SerialPort>, INamedElement
{
Expand Down Expand Up @@ -57,6 +59,16 @@ public string Encoding
set { configuration.Encoding = value; }
}

/// <summary>
/// Gets or sets the new line separator used to delimit reads from the serial port.
/// </summary>
[Description("The new line separator used to delimit reads from the serial port.")]
public string NewLine
{
get { return configuration.NewLine; }
set { configuration.NewLine = value; }
}

/// <summary>
/// Gets or sets the parity bit for the <see cref="SerialPort"/> object.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Bonsai.System/IO/Ports/Legacy/BaudRateConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel;

namespace Bonsai.IO
{
/// <summary>
/// This type is obsolete. Please use the <see cref="Ports.BaudRateConverter"/> operator instead.
/// </summary>
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
public class BaudRateConverter : Ports.BaudRateConverter
{
}
}
190 changes: 190 additions & 0 deletions Bonsai.System/IO/Ports/Legacy/CreateSerialPort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
using System;
using System.ComponentModel;
using System.IO.Ports;
using System.Reactive.Linq;

namespace Bonsai.IO
{
/// <summary>
/// This type is obsolete. Please use the <see cref="Ports.CreateSerialPort"/> operator instead.
/// </summary>
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
[Description("This type is obsolete. Please use the Ports.CreateSerialPort operator instead.")]
public class CreateSerialPort : Source<SerialPort>, INamedElement
{
readonly SerialPortConfiguration configuration = new SerialPortConfiguration();

/// <summary>
/// Gets or sets the optional alias for the serial port connection.
/// </summary>
[Category("Connection")]
[Description("The optional alias for the serial port connection.")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the name of the serial port.
/// </summary>
[Category("Connection")]
[TypeConverter(typeof(SerialPortNameConverter))]
[Description("The name of the serial port.")]
public string PortName
{
get { return configuration.PortName; }
set { configuration.PortName = value; }
}

/// <summary>
/// Gets or sets the serial baud rate.
/// </summary>
[Category("Connection")]
[TypeConverter(typeof(BaudRateConverter))]
[Description("The serial baud rate.")]
public int BaudRate
{
get { return configuration.BaudRate; }
set { configuration.BaudRate = value; }
}

/// <summary>
/// Gets or sets the byte encoding used for pre- and post-transmission conversion of text.
/// </summary>
[TypeConverter(typeof(Ports.SerialPortEncodingConverter))]
[Description("The byte encoding used for pre- and post-transmission conversion of text.")]
public string Encoding
{
get { return configuration.Encoding; }
set { configuration.Encoding = value; }
}

/// <summary>
/// Gets or sets the parity bit for the <see cref="SerialPort"/> object.
/// </summary>
[Description("The parity checking protocol.")]
public Parity Parity
{
get { return configuration.Parity; }
set { configuration.Parity = value; }
}

/// <summary>
/// Gets or sets the byte that replaces invalid bytes in the data stream when a parity error occurs.
/// </summary>
[Description("The byte that replaces invalid bytes in the data stream when a parity error occurs.")]
public byte ParityReplace
{
get { return configuration.ParityReplace; }
set { configuration.ParityReplace = value; }
}

/// <summary>
/// Gets or sets the number of data bits per byte.
/// </summary>
[Description("The number of data bits per byte.")]
public int DataBits
{
get { return configuration.DataBits; }
set { configuration.DataBits = value; }
}

/// <summary>
/// Gets or sets the number of stop bits per byte.
/// </summary>
[Description("The number of stop bits per byte.")]
public StopBits StopBits
{
get { return configuration.StopBits; }
set { configuration.StopBits = value; }
}

/// <summary>
/// Gets or sets the handshaking protocol for serial port transmission of data.
/// </summary>
[Description("The handshaking protocol for serial port transmission of data.")]
public Handshake Handshake
{
get { return configuration.Handshake; }
set { configuration.Handshake = value; }
}

/// <summary>
/// Gets or sets a value indicating whether null bytes are ignored when transmitted
/// between the port and the receive buffer.
/// </summary>
[Description("Indicates whether null bytes are ignored when transmitted between the port and the receive buffer.")]
public bool DiscardNull
{
get { return configuration.DiscardNull; }
set { configuration.DiscardNull = value; }
}

/// <summary>
/// Gets or sets a value indicating whether the Data Terminal Ready (DTR) signal should
/// be enabled during serial communication.
/// </summary>
[Description("Indicates whether the Data Terminal Ready (DTR) signal should be enabled during serial communication.")]
public bool DtrEnable
{
get { return configuration.DtrEnable; }
set { configuration.DtrEnable = value; }
}

/// <summary>
/// Gets or sets a value indicating whether the Request to Send (RTS) signal should be
/// enabled during serial communication.
/// </summary>
[Description("Indicates whether the Request to Send (RTS) signal should be enabled during serial communication.")]
public bool RtsEnable
{
get { return configuration.RtsEnable; }
set { configuration.RtsEnable = value; }
}

/// <summary>
/// Gets or sets the size of the read buffer, in bytes. This is the maximum number of
/// read bytes which can be buffered.
/// </summary>
[Description("The size of the read buffer, in bytes. This is the maximum number of read bytes which can be buffered.")]
public int ReadBufferSize
{
get { return configuration.ReadBufferSize; }
set { configuration.ReadBufferSize = value; }
}

/// <summary>
/// Gets or sets the size of the write buffer, in bytes. This is the maximum number of
/// bytes which can be queued for write.
/// </summary>
[Description("The size of the write buffer, in bytes. This is the maximum number of bytes which can be queued for write.")]
public int WriteBufferSize
{
get { return configuration.WriteBufferSize; }
set { configuration.WriteBufferSize = value; }
}

/// <summary>
/// Gets or sets the number of bytes received into the internal input buffer before
/// the read event is fired.
/// </summary>
[Description("The number of bytes received into the internal input buffer before the read event is fired.")]
public int ReceivedBytesThreshold
{
get { return configuration.ReceivedBytesThreshold; }
set { configuration.ReceivedBytesThreshold = value; }
}

/// <summary>
/// Generates an observable sequence that contains the serial port connection object.
/// </summary>
/// <returns>
/// A sequence containing a single instance of the <see cref="SerialPort"/> class
/// representing the serial connection.
/// </returns>
public override IObservable<SerialPort> Generate()
{
return Observable.Using(
() => Ports.SerialPortManager.ReserveConnection(Name, configuration),
connection => Observable.Return(connection.SerialPort).Concat(Observable.Never(connection.SerialPort)));
}
}
}
14 changes: 14 additions & 0 deletions Bonsai.System/IO/Ports/Legacy/SerialPortConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel;

namespace Bonsai.IO
{
/// <summary>
/// This type is obsolete. Please use the <see cref="Ports.SerialPortConfiguration"/> operator instead.
/// </summary>
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
public class SerialPortConfiguration : Ports.SerialPortConfiguration
{
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Bonsai.IO
Expand All @@ -9,6 +10,7 @@ namespace Bonsai.IO
/// </summary>
[Obsolete]
[XmlRoot("SerialPortConfigurationSettings")]
[EditorBrowsable(EditorBrowsableState.Never)]
public class SerialPortConfigurationCollection : KeyedCollection<string, SerialPortConfiguration>
{
/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Bonsai.System/IO/Ports/Legacy/SerialPortNameConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.ComponentModel;

namespace Bonsai.IO
{
/// <summary>
/// This type is obsolete. Please use the <see cref="Ports.SerialPortNameConverter"/> operator instead.
/// </summary>
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
public class SerialPortNameConverter : Ports.SerialPortNameConverter
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@
namespace Bonsai.IO
{
/// <summary>
/// Represents an operator that reads lines of characters asynchronously from a serial port.
/// This type is obsolete. Please use the <see cref="Ports.SerialReadLine"/> operator instead.
/// </summary>
[DefaultProperty(nameof(PortName))]
[Description("Reads lines of characters asynchronously from a serial port.")]
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
[Description("This type is obsolete. Please use the Ports.SerialReadLine operator instead.")]
public class SerialStringRead : Source<string>
{
/// <summary>
/// Gets or sets the name of the serial port.
/// </summary>
[TypeConverter(typeof(PortNameConverter))]
[TypeConverter(typeof(Ports.PortNameConverter))]
[Description("The name of the serial port.")]
public string PortName { get; set; }

/// <summary>
/// Gets or sets the new line separator used to delimit reads from the serial port.
/// </summary>
[Description("The new line separator used to delimit reads from the serial port.")]
public string NewLine { get; set; } = ObservableSerialPort.DefaultNewLine;
public string NewLine { get; set; }

/// <summary>
/// Reads lines of characters asynchronously from the serial port.
/// Reads a sequence of characters delimited by a new line separator from the serial port.
/// </summary>
/// <returns>
/// A sequence of <see cref="string"/> values representing each of the lines
/// read from the serial port.
/// </returns>
public override IObservable<string> Generate()
{
var newLine = ObservableSerialPort.Unescape(NewLine);
return ObservableSerialPort.ReadLine(PortName, newLine);
var newLine = Ports.SerialPortManager.Unescape(NewLine);
return Ports.ObservableSerialPort.ReadLine(PortName, newLine);
}
}
}
Loading