Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 999 Bytes

File metadata and controls

46 lines (38 loc) · 999 Bytes

Serial Communications

The .NET library seems to be much faster and lighter than the FTDI library for basic stuff. For FTDI usage see FTDI.md

Summary

using System.IO.Ports;
ser = new SerialPort("COM3", 115200);
ser.Open();
Console.WriteLine(ser.ReadLine());
string[] serialPortNames = SerialPort.GetPortNames();

Handling Incoming Data

ser = new SerialPort(com, baud);
ser.Open();
ser.ReadLine();
ser.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
static List<string> lines = new List<string>();
        
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        SerialPort sp = (SerialPort)sender;
        string line = (sp.ReadExisting() + sp.ReadLine()).Trim();
        lines.Add(line);
    }
    catch (System.IO.IOException exc)
    {
        Console.WriteLine("IOException in serial data handler");
        Console.WriteLine(exc);
    }
}