Skip to content

Instantly share code, notes, and snippets.

@corytodd
Last active December 28, 2022 05:44
Show Gist options
  • Save corytodd/9876275 to your computer and use it in GitHub Desktop.
Save corytodd/9876275 to your computer and use it in GitHub Desktop.
using System;
using System.IO.Ports;
class PortDataReceived
{
public static void Main()
{
SerialPort portA = new SerialPort("COM7");
portA.BaudRate = 9600;
portA.Parity = Parity.None;
portA.StopBits = StopBits.One;
portA.DataBits = 8;
portA.Handshake = Handshake.None;
portA.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandlerA);
portA.Open();
SerialPort portB = new SerialPort("COM9");
portB.BaudRate = 9600;
portB.Parity = Parity.None;
portB.StopBits = StopBits.One;
portB.DataBits = 8;
portB.Handshake = Handshake.None;
portB.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandlerB);
portB.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
DataObject data = DataObject.Instance;
int keyIn = 0;
do
{
// Check if any key pressed, read it into while-controlling variable
if (Console.KeyAvailable)
keyIn = Console.Read();
// Poll our channel A data
if (!string.IsNullOrEmpty(data.DataA))
{
Console.WriteLine(String.Format("Received data {0} on channel A. This is a(n) {1}", data.DataA, data.TypeA()));
data.DataA = "";
}
// Poll our channel B data
if (!string.IsNullOrEmpty(data.DataB))
{
Console.WriteLine(String.Format("Received data {0} on channel B. This is a(n) {1}", data.DataB, data.TypeB()));
data.DataB = "";
}
data.DoSomethingCool();
data.DoSomethingBoring();
// Stop looping when keyIn is no longer 0
}while (keyIn == 0);
portA.Close();
portB.Close();
}
private static void DataReceivedHandlerA(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
DataObject.Instance.DataA = sp.ReadExisting();
}
private static void DataReceivedHandlerB(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
DataObject.Instance.DataB = sp.ReadExisting();
}
public class DataObject
{
private static volatile DataObject instance;
private static object syncRoot = new Object();
/// <summary>
/// Singleton implementation
/// <see cref="http://msdn.microsoft.com/en-us/library/ff650316.aspx"/>
/// </summary>
public static DataObject Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new DataObject();
}
}
}
return instance;
}
}
// Constructor
private DataObject() { }
// Fields - use these to abstract the process or initialize
private string _dataA = "";
private string _dataB = "";
// Properties
public string DataA {
get { return _dataA; }
set { _dataA = value; }
}
public string DataB
{
get { return _dataB; }
set { _dataB = value; }
}
// Methods
public void DoSomethingCool()
{
// Implement something cool
}
public void DoSomethingBoring()
{
// Implement something boring
}
public string TypeA()
{
int result;
if (int.TryParse(DataA, out result))
return "Integer";
else
return "String";
}
public string TypeB()
{
int result;
if (int.TryParse(DataB, out result))
return "Integer";
else
return "String";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment