Skip to content

Commit 4ca9a78

Browse files
OAT Communication and OATControl updates
Trying to get support for both Wifi and Serial into OATCommunications
1 parent 35caf44 commit 4ca9a78

File tree

12 files changed

+438
-156
lines changed

12 files changed

+438
-156
lines changed

Software/OATMobile/OATCommunications/ClientAdapters/UdpClientAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OATCommunications.ClientAdapters
88
{
99
class UdpClientAdapter
1010
{
11-
public event EventHandler ClientFound;
11+
public event EventHandler<ClientFoundEventArgs> ClientFound;
1212

1313
private readonly string _hostToFind;
1414
private int _port;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace OATCommunications.CommunicationHandlers
2+
{
3+
public class CommandResponse {
4+
public string Data { get; }
5+
public bool Success { get; }
6+
public string StatusMessage { get; }
7+
8+
public CommandResponse(string data, bool success = true, string statusMessage = "") {
9+
Data = data;
10+
Success = success;
11+
StatusMessage = statusMessage;
12+
}
13+
}
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.IO.Ports;
6+
using OATCommunications.ClientAdapters;
7+
using static OATCommunications.ClientAdapters.UdpClientAdapter;
8+
9+
namespace OATCommunications.CommunicationHandlers
10+
{
11+
public static class CommunicationHandlerFactory
12+
{
13+
static IList<string> _available = new List<string>();
14+
public static void DiscoverDevices()
15+
{
16+
foreach (var port in SerialPort.GetPortNames())
17+
{
18+
_available.Add("Serial : " + port);
19+
}
20+
21+
var searcher = new UdpClientAdapter("OATerScope", 4030);
22+
searcher.ClientFound += OnWifiClientFound;
23+
searcher.StartClientSearch();
24+
}
25+
26+
private static void OnWifiClientFound(object sender, ClientFoundEventArgs e)
27+
{
28+
_available.Add($"WiFi : {e.Name} ({e.Address})");
29+
}
30+
31+
public static IList<String> AvailableDevices { get { return _available; } }
32+
33+
public static ICommunicationHandler ConnectToDevice(string device)
34+
{
35+
if (device.StartsWith("Serial : "))
36+
{
37+
string comPort = device.Substring("Serial : ".Length);
38+
return new SerialCommunicationHandler(comPort);
39+
}
40+
else if (device.StartsWith("WiFi : "))
41+
{
42+
string ipAddress = device.Substring("WiFi : ".Length);
43+
return new TcpCommunicationHandler(ipAddress);
44+
}
45+
46+
return null;
47+
}
48+
49+
}
50+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO.Ports;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace OATCommunications.CommunicationHandlers
9+
{
10+
public class SerialCommunicationHandler : ICommunicationHandler
11+
{
12+
private string _portName;
13+
private SerialPort _port;
14+
public SerialCommunicationHandler(string comPort)
15+
{
16+
_portName = comPort;
17+
_port = new SerialPort(comPort);
18+
}
19+
20+
public bool Connected => _port.IsOpen;
21+
22+
public async Task<CommandResponse> SendBlind(string command)
23+
{
24+
return await SendCommand(command, false);
25+
}
26+
27+
public async Task<CommandResponse> SendCommand(string command)
28+
{
29+
return await SendCommand(command, true);
30+
}
31+
32+
public async Task<CommandResponse> SendCommand(string command, bool needsResponse)
33+
{
34+
if (await EnsurePortIsOpen())
35+
{
36+
try
37+
{
38+
_port.Write(command);
39+
}
40+
catch
41+
{
42+
return new CommandResponse(string.Empty, false, $"Unable to write to {_portName}");
43+
}
44+
45+
if (needsResponse)
46+
{
47+
try
48+
{
49+
var response = _port.ReadTo("#");
50+
return new CommandResponse(response, true);
51+
}
52+
catch
53+
{
54+
return new CommandResponse(string.Empty, false, $"Unable to read response to {command} from {_portName}");
55+
}
56+
}
57+
return new CommandResponse(string.Empty, true);
58+
}
59+
else
60+
{
61+
return new CommandResponse(string.Empty, false, $"Unable to open {_portName}");
62+
}
63+
}
64+
65+
private async Task<bool> EnsurePortIsOpen()
66+
{
67+
if (!_port.IsOpen)
68+
{
69+
try
70+
{
71+
_port.Open();
72+
await Task.Delay(750); // Arduino resets on connection. Give it time to start up.
73+
_port.Write(":I#");
74+
return true;
75+
}
76+
catch
77+
{
78+
return false;
79+
}
80+
}
81+
return true;
82+
}
83+
84+
public void Disconnect()
85+
{
86+
if (_port.IsOpen)
87+
{
88+
_port.Close();
89+
}
90+
}
91+
92+
}
93+
}

Software/OATMobile/OATCommunications/CommunicationHandlers/TcpCommunicationHandler.cs

Lines changed: 122 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,132 @@
55
using System.Text;
66
using System.Threading.Tasks;
77

8-
namespace OATCommunications.CommunicationHandlers {
9-
public class TcpCommunicationHandler : ICommunicationHandler {
10-
private IPAddress _ip;
11-
private int _port;
12-
private TcpClient _client;
13-
14-
public TcpCommunicationHandler(IPAddress ip, int port) {
15-
_ip = ip;
16-
_port = port;
17-
_client = new TcpClient();
18-
}
19-
20-
public async Task<CommandResponse> SendBlind(string command) {
21-
return await SendCommand(command, false);
22-
}
23-
24-
public async Task<CommandResponse> SendCommand(string command) {
25-
return await SendCommand(command, true);
26-
}
27-
28-
public bool Connected
8+
namespace OATCommunications.CommunicationHandlers
9+
{
10+
public class TcpCommunicationHandler : ICommunicationHandler
11+
{
12+
private IPAddress _ip;
13+
private int _port;
14+
private TcpClient _client;
15+
16+
public TcpCommunicationHandler(string spec)
17+
{
18+
string ip = string.Empty;
19+
string port = string.Empty;
20+
21+
var colon = spec.IndexOf(':');
22+
if (colon > 0)
23+
{
24+
try
25+
{
26+
ip = spec.Substring(0, colon);
27+
port = spec.Substring(colon + 1);
28+
_ip = IPAddress.Parse(ip);
29+
_port = int.Parse(port);
30+
_client = new TcpClient();
31+
}
32+
catch
33+
{
34+
35+
}
36+
}
37+
}
38+
39+
public TcpCommunicationHandler(IPAddress ip, int port)
40+
{
41+
_ip = ip;
42+
_port = port;
43+
_client = new TcpClient();
44+
}
45+
46+
public async Task<CommandResponse> SendBlind(string command)
47+
{
48+
return await SendCommand(command, false);
49+
}
50+
51+
public async Task<CommandResponse> SendCommand(string command)
52+
{
53+
return await SendCommand(command, true);
54+
}
55+
56+
public bool Connected
2957
{
3058
get
3159
{
32-
return _client != null && _client.Connected;
60+
return _client != null && _client.Connected;
3361
}
3462
}
3563

36-
private async Task<CommandResponse> SendCommand(string command, bool needsResponse) {
37-
if (!_client.Connected) {
38-
try {
39-
_client = new TcpClient();
40-
_client.Connect(_ip, _port);
41-
}
42-
catch (Exception e) {
43-
Debug.WriteLine($"Failed To connect or create client: \n{e.Message}");
44-
return new CommandResponse("", false, $"Failed To Connect to Client: {e.Message}");
45-
}
46-
}
47-
48-
_client.ReceiveTimeout = 250;
49-
_client.SendTimeout = 250;
50-
51-
string error = String.Empty;
52-
53-
var stream = _client.GetStream();
54-
var bytes = Encoding.ASCII.GetBytes(command);
55-
try {
56-
await stream.WriteAsync(bytes, 0, bytes.Length);
57-
}
58-
catch (Exception e) {
59-
Debug.WriteLine(e.Message);
60-
return new CommandResponse("", false, $"Failed to send message: {e.Message}");
61-
}
62-
63-
Debug.WriteLine($"Sent {command}");
64-
65-
var respString = String.Empty;
66-
67-
if (needsResponse) {
68-
try {
69-
var response = new byte[256];
70-
var respCount = await stream.ReadAsync(response, 0, response.Length);
71-
respString = Encoding.ASCII.GetString(response, 0, respCount).TrimEnd("#".ToCharArray());
72-
Debug.WriteLine($"Received {respString}");
73-
}
74-
catch (Exception e) {
75-
Debug.WriteLine(e.Message);
76-
return new CommandResponse("", false, $"Failed to receive message: {e.Message}");
77-
}
78-
}
79-
80-
stream.Close();
81-
82-
return new CommandResponse(respString);
83-
}
84-
}
85-
86-
public class CommandResponse {
87-
public string Data { get; }
88-
public bool Success { get; }
89-
public string StatusMessage { get; }
90-
91-
public CommandResponse(string data, bool success = true, string statusMessage = "") {
92-
Data = data;
93-
Success = success;
94-
StatusMessage = statusMessage;
95-
}
96-
}
64+
private async Task<CommandResponse> SendCommand(string command, bool needsResponse)
65+
{
66+
if (_client == null)
67+
{
68+
return new CommandResponse(string.Empty, false, $"Configuration error, IP [{_ip}] or port [{_port}] is invalid.");
69+
}
70+
71+
if (!_client.Connected)
72+
{
73+
try
74+
{
75+
_client = new TcpClient();
76+
_client.Connect(_ip, _port);
77+
}
78+
catch (Exception e)
79+
{
80+
Debug.WriteLine($"Failed To connect or create client: \n{e.Message}");
81+
return new CommandResponse("", false, $"Failed To Connect to Client: {e.Message}");
82+
}
83+
}
84+
85+
_client.ReceiveTimeout = 250;
86+
_client.SendTimeout = 250;
87+
88+
string error = String.Empty;
89+
90+
var stream = _client.GetStream();
91+
var bytes = Encoding.ASCII.GetBytes(command);
92+
try
93+
{
94+
await stream.WriteAsync(bytes, 0, bytes.Length);
95+
}
96+
catch (Exception e)
97+
{
98+
Debug.WriteLine(e.Message);
99+
return new CommandResponse("", false, $"Failed to send message: {e.Message}");
100+
}
101+
102+
Debug.WriteLine($"Sent {command}");
103+
104+
var respString = String.Empty;
105+
106+
if (needsResponse)
107+
{
108+
try
109+
{
110+
var response = new byte[256];
111+
var respCount = await stream.ReadAsync(response, 0, response.Length);
112+
respString = Encoding.ASCII.GetString(response, 0, respCount).TrimEnd("#".ToCharArray());
113+
Debug.WriteLine($"Received {respString}");
114+
}
115+
catch (Exception e)
116+
{
117+
Debug.WriteLine(e.Message);
118+
return new CommandResponse("", false, $"Failed to receive message: {e.Message}");
119+
}
120+
}
121+
122+
stream.Close();
123+
124+
return new CommandResponse(respString);
125+
}
126+
127+
public void Disconnect()
128+
{
129+
if (_client != null && _client.Connected)
130+
{
131+
_client.Close();
132+
_client = null;
133+
}
134+
}
135+
}
97136
}

Software/OATMobile/OATCommunications/ICommunicationHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public interface ICommunicationHandler {
1010
Task<CommandResponse> SendBlind(string command);
1111
Task<CommandResponse> SendCommand(string command);
1212
bool Connected { get; }
13+
void Disconnect();
1314
}
1415
}

0 commit comments

Comments
 (0)