Skip to content

Commit 3ef4cfd

Browse files
author
Wayfarer
committed
- New ISerialPort and IComTransport interfaces introduced
- SystemSerialPort and ComTransport added as implementations - SerialPortConfig record created for port configuration - FakeSerialPort implemented for unit tests - New test project CommunicationTests created with MSTest - Network and COM port tests moved from CommonLibraryTests - INetCom interface moved to Communication.Interfaces - System.IO.Ports referenced as NuGet package - NodeComparer extracted and moved from Pathfinding - File comments and paths updated - CoreLibrary.sln and README.md expanded and adapted accordingly
1 parent e663824 commit 3ef4cfd

25 files changed

Lines changed: 604 additions & 127 deletions

CommonLibraryTests/CommonLibraryTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
<ItemGroup>
8787
<ProjectReference Include="..\CommonControls\CommonControls.csproj" />
8888
<ProjectReference Include="..\CommonFilter\CommonFilter.csproj" />
89-
<ProjectReference Include="..\Communication\Communication.csproj" />
9089
<ProjectReference Include="..\CoreMemoryLog\CoreMemoryLog.csproj" />
9190
<ProjectReference Include="..\ExtendedSystemObjects\ExtendedSystemObjects.csproj" />
9291
<ProjectReference Include="..\FileHandler\FileHandler.csproj" />

CommonLibraryTests/Communication.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.

Communication/ComTransport.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

Communication/Communication.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
<Nullable>enable</Nullable>
1818
</PropertyGroup>
1919

20+
<ItemGroup>
21+
<PackageReference Include="System.IO.Ports" Version="10.0.1" />
22+
</ItemGroup>
23+
2024
</Project>

Communication/FileTransfer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: Communication
4-
* FILE: Communication/FileTransfer.cs
4+
* FILE: FileTransfer.cs
55
* PURPOSE: Does the heavy lifting for File Transfers
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/

Communication/HttpClientManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: Communication
4-
* FILE: Communication/HttpClientManager.cs
4+
* FILE: HttpClientManager.cs
55
* PURPOSE: Handle Http Requests
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: Communication.Interfaces
4+
* FILE: IComTransport.cs
5+
* PURPOSE: Com Port Interface, wraps around my ISerialPort.
6+
* After that the Protocol layer can be implemented on top.
7+
* PROGRAMMER: Peter Geinitz (Wayfarer)
8+
*/
9+
10+
using System;
11+
12+
namespace Communication.Interfaces
13+
{
14+
/// <summary>
15+
/// Minimal implementation of a COM transport interface.
16+
/// </summary>
17+
public interface IComTransport
18+
{
19+
/// <summary>
20+
/// Writes the specified text.
21+
/// </summary>
22+
/// <param name="text">The text.</param>
23+
void Write(string text);
24+
25+
/// <summary>
26+
/// Occurs when [data received].
27+
/// </summary>
28+
event Action<string> DataReceived;
29+
}
30+
31+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: Communication
4-
* FILE: Communication/INetCom.cs
3+
* PROJECT: Communication.Interfaces;
4+
* FILE: INetCom.cs
55
* PURPOSE: Entry Point for File Downloads, defined as Interface
66
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
@@ -15,7 +15,7 @@
1515
using System.Threading;
1616
using System.Threading.Tasks;
1717

18-
namespace Communication;
18+
namespace Communication.Interfaces;
1919

2020
/// <summary>
2121
/// The ICom interface.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: Communication.Interfaces
4+
* FILE: ISerialPort.cs
5+
* PURPOSE: Basic serial port interface
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
using System;
10+
11+
namespace Communication.Interfaces
12+
{
13+
/// <summary>
14+
/// The ISerialPort interface.
15+
/// </summary>
16+
public interface ISerialPort
17+
{
18+
/// <summary>
19+
/// Gets a value indicating whether this instance is open.
20+
/// </summary>
21+
/// <value>
22+
/// <c>true</c> if this instance is open; otherwise, <c>false</c>.
23+
/// </value>
24+
bool IsOpen { get; }
25+
26+
/// <summary>
27+
/// Opens this instance.
28+
/// </summary>
29+
void Open();
30+
31+
/// <summary>
32+
/// Closes this instance.
33+
/// </summary>
34+
void Close();
35+
36+
/// <summary>
37+
/// Reads the specified buffer.
38+
/// </summary>
39+
/// <param name="buffer">The buffer.</param>
40+
/// <param name="offset">The offset.</param>
41+
/// <param name="count">The count.</param>
42+
/// <returns>Actual count of bytes, the actual value is in the buffer</returns>
43+
int Read(byte[] buffer, int offset, int count);
44+
45+
/// <summary>
46+
/// Reads the specified buffer.
47+
/// </summary>
48+
/// <param name="buffer">The buffer.</param>
49+
/// <param name="offset">The offset.</param>
50+
/// <param name="count">The count.</param>
51+
/// <returns>Actual count of bytes, the actual value is in the buffer</returns>
52+
int Read(char[] buffer, int offset, int count);
53+
54+
/// <summary>
55+
/// Writes the specified data.
56+
/// </summary>
57+
/// <param name="data">The data.</param>
58+
void Write(string data);
59+
60+
61+
/// <summary>
62+
/// Writes the specified buffer.
63+
/// </summary>
64+
/// <param name="buffer">The buffer.</param>
65+
/// <param name="offset">The offset.</param>
66+
/// <param name="count">The count.</param>
67+
void Write(byte[] buffer, int offset, int count);
68+
69+
/// <summary>
70+
/// Occurs when [data received].
71+
/// </summary>
72+
event EventHandler DataReceived;
73+
}
74+
}

Communication/NetCom.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Collections.Generic;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
using Communication.Interfaces;
1516

1617
namespace Communication;
1718

0 commit comments

Comments
 (0)