Console App1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
namespace ConsoleApp_SerialPort
{
class Program
{
#region CLASS functions
public static SerialPort serialPort1;
public static Thread MyThread;
public static void ReadPortFeatures()
{
try
{
Console.WriteLine("Serial port INFO ==============================" );
Console.WriteLine("Serial port open : " + serialPort1.IsOpen);
Console.WriteLine("Serial port Baud : " + serialPort1.BaudRate);
Console.WriteLine("Serial port Name : " + serialPort1.PortName);
Console.WriteLine("Serial port DBits : " + serialPort1.DataBits);
Console.WriteLine("Serial port Parity : " + serialPort1.Parity);
Console.WriteLine("Serial port StopBits: " + serialPort1.StopBits);
Console.WriteLine("===============================================");
}
catch
{
Console.WriteLine("PORT cannot be obtained.. ");
}
}
public static void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = serialPort1.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = serialPort1.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
Console.WriteLine("{0}|{1}",dataLength, nbrDataRead);
// Send data to whom ever interested
// if (NewSerialDataRecieved != null)
// NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
#endregion
#region MAIN()
static void Main(string[] args)
{
// get port names of available ports
string[] ports = SerialPort.GetPortNames();
foreach (string item in ports)
{
Console.WriteLine("||| Ports available for reading on this PC ... ");
Console.WriteLine("\n\t" + item);
Console.WriteLine("================================================");
}
// create the serial port object ..
serialPort1 = new SerialPort("COM4", 115200, Parity.None, 8, StopBits.One);
if (serialPort1 != null)
{
Console.WriteLine("Serial Port has been created .. ");
ReadPortFeatures();
}
// Open port
if (serialPort1.IsOpen == false)
{
serialPort1.Open();
ReadPortFeatures();
string data_rx = serialPort1.ReadExisting();
Console.WriteLine(data_rx);
// port listener subscribed to event of Data received
// .. then call _serialPort_DataReceived()
serialPort1.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
serialPort1.Close();
}
else
{
Console.WriteLine("Port is bussy, cannot be opened now.. ");
return;
}
Console.ReadLine();
}
#endregion MAIN
}
}
Multi channel reading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Globalization;
namespace ConsoleApp_Arduino
{
public class Program
{
#region GLOBALS
public double ti = 0; // time instance
public double wi = 0; // value ..
public static int k = 0;
#endregion
//
// M
// A
// I
// N
// =============================================================================
static void Main(string[] args)
{
//|||| SERIAL PORT STREAMING
SerialPort my_com3 = new SerialPort();
my_com3.BaudRate = 115200;
my_com3.PortName = "COM3";
my_com3.Open();
Console.WriteLine("===============================================================================");
while (true)
{
string data_rx = my_com3.ReadLine();
DataParsing(data_rx);
//Console.WriteLine("{0}", data_rx );
}
}//end of main
#region DATA PARSING FUNCTION
public static void DataParsing(string data_rx)
{
sbyte indexOf_startDataCharacter = (sbyte)data_rx.IndexOf("@");
sbyte indexOfA = (sbyte)data_rx.IndexOf("A");
sbyte indexOfB = (sbyte)data_rx.IndexOf("B");
sbyte indexOfC = (sbyte)data_rx.IndexOf("C");
if (indexOfA != -1 && indexOfB !=-1 && indexOf_startDataCharacter !=-1)
{
// @1509A1.5090B0.0618C
try
{
// strings
string s1 = data_rx.Substring(indexOf_startDataCharacter + 1, (indexOfA - indexOf_startDataCharacter)-1);
string s2 = data_rx.Substring(indexOfA+1, (indexOfB - indexOfA)-1);
string s3 = data_rx.Substring(indexOfB + 1, (indexOfC - indexOfB) - 1);
k++;
Console.WriteLine("No: {0,5} | Li: {1,10} | ti: {2,10}| f(ti): {3,10} || t': {4,8} | G(f(t)): {5,8} --|||", k, s1, s2, s3,
Convert.ToDecimal(s2, CultureInfo.InvariantCulture),
Convert.ToDecimal(s3, CultureInfo.InvariantCulture));
}
catch
{
Console.WriteLine("DATA PACAKGE NOT as expected ... ");
}
}
#endregion
}
}
}