Here’s a simple code snippet to read the serial inputs in C#. I had to use this code several times when working with arduino like devices.
Hope you’ll enjoy it.
Catch you next time and keep it bug free !
using System; using System.IO.Ports; class Program { [STAThread] static void Main(string[] args) { new Program(); } // Create the serial port with basic settings // port name, bauds, parity, packet size and stop bits. private SerialPort port = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.Two); private Program() { // Called when data received through serial port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); // Can crash if the port is already used, haven't found a way to detect it... try { // Start to listen port.Open(); } catch(Exception ex) { Console.WriteLine(ex.Message); } // Just avoid console to return Console.ReadKey(); } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Write the incoming data Console.Write(port.ReadExisting()); } }