|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: Communication |
| 4 | + * FILE: ComTransport.cs |
| 5 | + * PURPOSE: Wrapper around a serial port implementing IComTransport, with async read support. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Text; |
| 11 | +using System.Threading; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Communication.Interfaces; |
| 14 | + |
| 15 | +namespace Communication |
| 16 | +{ |
| 17 | + /// <inheritdoc /> |
| 18 | + /// <summary> |
| 19 | + /// Wrapper around a serial port implementing IComTransport |
| 20 | + /// </summary> |
| 21 | + /// <seealso cref="Communication.Interfaces.IComTransport" /> |
| 22 | + public sealed class ComTransport : IComTransport |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// The port |
| 26 | + /// </summary> |
| 27 | + private readonly ISerialPort _port; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The buffer |
| 31 | + /// </summary> |
| 32 | + private readonly byte[] _buffer = new byte[4096]; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// The encoding |
| 36 | + /// </summary> |
| 37 | + private readonly Encoding _encoding = Encoding.ASCII; |
| 38 | + |
| 39 | + /// <inheritdoc /> |
| 40 | + public event Action<string>? DataReceived; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Initializes a new instance of the <see cref="ComTransport"/> class. |
| 44 | + /// </summary> |
| 45 | + /// <param name="port">The port.</param> |
| 46 | + public ComTransport(ISerialPort port) |
| 47 | + { |
| 48 | + _port = port; |
| 49 | + _port.DataReceived += OnPortData; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Called when [port data]. |
| 54 | + /// </summary> |
| 55 | + /// <param name="sender">The sender.</param> |
| 56 | + /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> |
| 57 | + private void OnPortData(object? sender, EventArgs e) |
| 58 | + { |
| 59 | + int bytes = _port.Read(_buffer, 0, _buffer.Length); |
| 60 | + if (bytes <= 0) |
| 61 | + return; |
| 62 | + |
| 63 | + string text = _encoding.GetString(_buffer, 0, bytes); |
| 64 | + DataReceived?.Invoke(text); |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public void Write(string text) |
| 69 | + { |
| 70 | + if (string.IsNullOrEmpty(text)) |
| 71 | + return; |
| 72 | + |
| 73 | + _port.Write(text); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Reads the once asynchronous. |
| 78 | + /// </summary> |
| 79 | + /// <param name="timeout">The timeout.</param> |
| 80 | + /// <returns>Message from Port.</returns> |
| 81 | + public Task<string> ReadOnceAsync(TimeSpan timeout) |
| 82 | + { |
| 83 | + var tcs = new TaskCompletionSource<string>( |
| 84 | + TaskCreationOptions.RunContinuationsAsynchronously); |
| 85 | + |
| 86 | + void Handler(string data) |
| 87 | + => tcs.TrySetResult(data); |
| 88 | + |
| 89 | + DataReceived += Handler; |
| 90 | + |
| 91 | + var timer = new Timer(_ => |
| 92 | + tcs.TrySetException(new TimeoutException()), |
| 93 | + null, |
| 94 | + timeout, |
| 95 | + Timeout.InfiniteTimeSpan); |
| 96 | + |
| 97 | + return tcs.Task.ContinueWith(t => |
| 98 | + { |
| 99 | + DataReceived -= Handler; |
| 100 | + timer.Dispose(); |
| 101 | + return t.Result; |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments