forked from mjrgh/DirectOutput
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from philipellisis/feature/concurrent-com-port
Feature/concurrent com port
- Loading branch information
Showing
7 changed files
with
313 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
using System; | ||
using System.IO.Ports; | ||
using System.IO.Pipes; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using PerCederberg.Grammatica.Runtime; | ||
|
||
public class NamedPipeServer | ||
{ | ||
private SerialPort serialPort; | ||
private bool isRunning = true; | ||
private string comPort = ""; | ||
private const string PipeName = "ComPortServerPipe"; | ||
CancellationToken clientToken = new CancellationToken(); | ||
CancellationToken serverToken = new CancellationToken(); | ||
|
||
public NamedPipeServer(string comPort) | ||
{ | ||
|
||
this.comPort = comPort; | ||
serialPort = new SerialPort(comPort, 2000000, Parity.None, 8, StopBits.One); | ||
serialPort.NewLine = "\r\n"; | ||
serialPort.ReadTimeout = 500; | ||
serialPort.WriteTimeout = 500; | ||
serialPort.Open(); | ||
serialPort.DtrEnable = true; | ||
} | ||
|
||
|
||
public void StartServer() | ||
{ | ||
|
||
Task.Run(async () => | ||
{ | ||
while (isRunning) | ||
{ | ||
var serverStream = new NamedPipeServerStream( | ||
PipeName, | ||
PipeDirection.InOut, | ||
NamedPipeServerStream.MaxAllowedServerInstances, | ||
PipeTransmissionMode.Byte, | ||
PipeOptions.Asynchronous); | ||
|
||
Console.WriteLine("Waiting for client connection..."); | ||
await serverStream.WaitForConnectionAsync(serverToken); | ||
|
||
HandleClientConnectionAsync(serverStream); | ||
} | ||
}); | ||
} | ||
|
||
|
||
|
||
|
||
private async Task HandleClientConnectionAsync(NamedPipeServerStream serverStream) | ||
{ | ||
bool completed = false; | ||
while (isRunning && !completed && serverStream.IsConnected) | ||
{ | ||
try | ||
{ | ||
var request = new byte[1024]; | ||
int bytesRead = await serverStream.ReadAsync(request, 0, request.Length, clientToken); | ||
string requestStr = Encoding.UTF8.GetString(request, 0, bytesRead); | ||
|
||
// Process request | ||
if (requestStr.StartsWith("CONNECT")) | ||
{ | ||
Console.WriteLine("Requesting Connect"); | ||
serialPort.Open(); | ||
serverStream.Write(Encoding.UTF8.GetBytes("OK"), 0, 2); | ||
} | ||
else if (requestStr.StartsWith("STOP_SERVER")) | ||
{ | ||
isRunning = false; | ||
} | ||
else if (requestStr.StartsWith("DISCONNECT")) | ||
{ | ||
serverStream.Disconnect(); | ||
completed = true; | ||
Console.WriteLine("Requesting disconnect"); | ||
} | ||
else if (requestStr.StartsWith("WRITE")) | ||
{ | ||
var bytesToWrite = Convert.FromBase64String(requestStr.Substring(6)); | ||
serialPort.Write(bytesToWrite, 0, bytesToWrite.Length); | ||
serverStream.Write(Encoding.UTF8.GetBytes("OK"), 0, 2); | ||
} | ||
else if (requestStr.StartsWith("READLINE")) | ||
{ | ||
string response = serialPort.ReadLine(); | ||
serverStream.Write(Encoding.UTF8.GetBytes(response), 0, response.Length); | ||
} | ||
else if (requestStr.StartsWith("CHECK")) | ||
{ | ||
string response = serialPort.IsOpen ? "TRUE" : "FALSE"; | ||
serverStream.Write(Encoding.UTF8.GetBytes(response), 0, response.Length); | ||
} | ||
else if (requestStr.StartsWith("COMPORT")) | ||
{ | ||
Console.WriteLine("Requesting com port"); | ||
serverStream.Write(Encoding.UTF8.GetBytes(this.comPort), 0, this.comPort.Length); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
serverStream.Disconnect(); | ||
isRunning = false; | ||
} | ||
finally | ||
{ | ||
//Console.WriteLine("cleaning up, closing ports"); | ||
} | ||
|
||
} | ||
if (isRunning == false) | ||
{ | ||
|
||
serverStream.Disconnect(); | ||
serverStream.Close(); | ||
|
||
} | ||
} | ||
|
||
public void StopServer() | ||
{ | ||
serialPort.Close(); | ||
isRunning = false; | ||
clientToken.ThrowIfCancellationRequested(); | ||
serverToken.ThrowIfCancellationRequested(); | ||
Thread.Sleep(300); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
using System; | ||
using System.IO.Pipes; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
public class PinOneCommunication | ||
{ | ||
private NamedPipeClientStream pipeClient; | ||
private NamedPipeServer server = null; | ||
private string pipeName = "ComPortServerPipe"; | ||
private string COMPort = ""; | ||
|
||
public PinOneCommunication(String COMPort) | ||
{ | ||
this.COMPort = COMPort; | ||
} | ||
|
||
public bool ConnectToServer() | ||
{ | ||
try | ||
{ | ||
pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.None); | ||
pipeClient.Connect(100); | ||
return true; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public bool DisconnectFromServer() | ||
{ | ||
Disconnect(); | ||
if (server !=null) | ||
{ | ||
server.StopServer(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public bool CreateServer() | ||
{ | ||
try | ||
{ | ||
if (!COMPort.IsNullOrEmpty()) | ||
{ | ||
server = new NamedPipeServer(COMPort); | ||
Thread serverThread = new Thread(server.StartServer); | ||
serverThread.IsBackground = true; | ||
serverThread.Start(); | ||
Thread.Sleep(300); | ||
return true; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
public bool isComPortConnected() | ||
{ | ||
SendMessage("CHECK"); | ||
var response = ReadMessage(); | ||
return response == "TRUE"; | ||
} | ||
public void Disconnect() | ||
{ | ||
SendMessage("DISCONNECT"); | ||
} | ||
|
||
public void Write(byte[] bytesToWrite) | ||
{ | ||
string base64Bytes = Convert.ToBase64String(bytesToWrite); | ||
SendMessage($"WRITE {base64Bytes}"); | ||
ReadMessage(); // Expect OK | ||
} | ||
|
||
public string ReadLine() | ||
{ | ||
SendMessage("READLINE"); | ||
return ReadMessage(); | ||
} | ||
|
||
public string GetCOMPort() | ||
{ | ||
SendMessage("COMPORT"); | ||
return ReadMessage(); | ||
} | ||
|
||
private void SendMessage(string message) | ||
{ | ||
try | ||
{ | ||
byte[] request = Encoding.UTF8.GetBytes(message); | ||
pipeClient.Write(request, 0, request.Length); | ||
} | ||
catch (Exception) | ||
{ | ||
if (CreateServer() && ConnectToServer()) | ||
{ | ||
byte[] request = Encoding.UTF8.GetBytes(message); | ||
pipeClient.Write(request, 0, request.Length); | ||
} | ||
else | ||
{ | ||
throw new Exception("Unable to connect to board"); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
private string ReadMessage() | ||
{ | ||
try | ||
{ | ||
var response = new byte[1024]; | ||
int bytesRead = pipeClient.Read(response, 0, response.Length); | ||
return Encoding.UTF8.GetString(response, 0, bytesRead); | ||
} | ||
catch (Exception) | ||
{ | ||
if( CreateServer() && ConnectToServer()) | ||
{ | ||
return ReadMessage(); | ||
|
||
} else | ||
{ | ||
return ""; | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.