Created
September 8, 2015 13:36
-
-
Save pushrbx/aada0057d2ac583c52fa to your computer and use it in GitHub Desktop.
C# Serial Port wrapper. Works great with arduinos.
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
using System; | |
using System.IO.Ports; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace AwesomeSerialDeviceWrapper | |
{ | |
public class SerialDevice : IDisposable | |
{ | |
private long m_timeSinceLastEvent; | |
//private bool m_hasTimedOut; | |
private readonly SerialPort m_serialComPort; | |
private const int WaitMillisecs = 3; | |
public delegate void DataReceiveHandler(string data); | |
private readonly CancellationTokenSource m_captureCancellationTokenSource; | |
private event DataReceiveHandler OnDataReceived; | |
public SerialDevice() | |
{ | |
m_serialComPort = new SerialPort { BaudRate = 9600, PortName = "COM3" }; | |
m_captureCancellationTokenSource = new CancellationTokenSource(); | |
} | |
public SerialDevice(string portName, int baudRate) | |
{ | |
m_captureCancellationTokenSource = new CancellationTokenSource(); | |
m_serialComPort = new SerialPort { BaudRate = baudRate, PortName = portName}; | |
} | |
~SerialDevice() | |
{ | |
Dispose(false); | |
} | |
public event DataReceiveHandler DataReceived | |
{ | |
add { OnDataReceived += value; } | |
remove | |
{ | |
if (OnDataReceived != null) | |
OnDataReceived -= value; | |
} | |
} | |
private void OpenComPort() | |
{ | |
m_timeSinceLastEvent = DateTime.Now.Ticks; | |
//m_hasTimedOut = false; | |
m_serialComPort.Open(); | |
} | |
private void RaiseDataReceived(string data) | |
{ | |
if (OnDataReceived == null) return; | |
OnDataReceived(data); | |
} | |
public async void BeginCapture() | |
{ | |
OpenComPort(); | |
if (m_serialComPort != null && m_serialComPort.IsOpen) | |
{ | |
await Task.Run(() => StartNewThread(), m_captureCancellationTokenSource.Token); | |
} | |
} | |
public void StopCapture() | |
{ | |
if (!m_captureCancellationTokenSource.IsCancellationRequested) | |
{ | |
m_captureCancellationTokenSource.Cancel(); | |
Thread.Sleep(5); | |
} | |
if (m_serialComPort != null && m_serialComPort.IsOpen) | |
{ | |
m_serialComPort.Close(); | |
} | |
} | |
private void StartNewThread() | |
{ | |
try | |
{ | |
while (m_serialComPort.IsOpen) | |
{ | |
if (m_captureCancellationTokenSource.IsCancellationRequested) | |
{ | |
m_captureCancellationTokenSource.Token.ThrowIfCancellationRequested(); | |
break; | |
} | |
var readall = m_serialComPort.ReadLine(); | |
RaiseDataReceived(readall); | |
Thread.Sleep(WaitMillisecs); | |
} | |
} | |
catch (Exception) | |
{ | |
if (m_serialComPort.IsOpen) | |
{ | |
m_serialComPort.Close(); | |
} | |
} | |
} | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
private void Dispose(bool disposing) | |
{ | |
StopCapture(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment