Skip to content

Instantly share code, notes, and snippets.

@hepe00
Forked from adamkewley/SerialPortExtensions.cs
Created June 9, 2021 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hepe00/7ca277788e23082b9dbded453c813b0f to your computer and use it in GitHub Desktop.
Save hepe00/7ca277788e23082b9dbded453c813b0f to your computer and use it in GitHub Desktop.
Extension methods to make System.IO.Ports.SerialPort easier to use with .NET 4.5 async workflows (Does not support timeouts)
using System.IO.Ports;
namespace AsyncExtensions {
public static class SerialPortExtensions
{
/// <summary>
/// Read a line from the SerialPort asynchronously
/// </summary>
/// <param name="serialPort">The port to read data from</param>
/// <returns>A line read from the input</returns>
public static async Task<string> ReadLineAsync(
this SerialPort serialPort)
{
byte[] buffer = new byte[1];
string ret = string.Empty;
// Read the input one byte at a time, convert the
// byte into a char, add that char to the overall
// response string, once the response string ends
// with the line ending then stop reading
while(true)
{
await serialPort.BaseStream.ReadAsync(buffer, 0, 1);
ret += serialPort.Encoding.GetString(buffer);
if (ret.EndsWith(serialPort.NewLine))
// Truncate the line ending
return ret.Substring(0, ret.Length - serialPort.NewLine.Length);
}
}
/// <summary>
/// Write a line to the SerialPort asynchronously
/// </summary>
/// <param name="serialPort">The port to send text to</param>
/// <param name="str">The text to send</param>
/// <returns></returns>
public static async Task WriteLineAsync(
this SerialPort serialPort, string str)
{
byte[] encodedStr =
serialPort.Encoding.GetBytes(str + serialPort.NewLine);
await serialPort.BaseStream.WriteAsync(encodedStr, 0, encodedStr.Length);
await serialPort.BaseStream.FlushAsync();
}
/// <summary>
/// Write a line to the ICommunicationPortAdaptor asynchronously followed
/// immediately by attempting to read a line from the same port. Useful
/// for COMMAND --> RESPONSE type communication.
/// </summary>
/// <param name="serialPort">The port to process commands through</param>
/// <param name="command">The command to send through the port</param>
/// <returns>The response from the port</returns>
public static async Task<string> SendCommandAsync(
this SerialPort serialPort, string command)
{
await serialPort.WriteLineAsync(command);
return await serialPort.ReadLineAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment