using RJCP.IO.Ports; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.NetworkInformation; using System.Reflection; using System.Text; using System.Threading.Tasks; internal class Program { public static SerialPortStream CommPort = new SerialPortStream(); static async Task Main(string[] args) { Console.WriteLine("Serial Port Test Program"); CommPortSetup(); for (int i = 0; i < 100; i++) { CommPortOpen(); await Task.Delay(1000); var stopwatch = Stopwatch.StartNew(); CommPortClose(); stopwatch.Stop(); Console.WriteLine($"{i} Elapsed: {stopwatch.ElapsedMilliseconds} ms"); await Task.Delay(1000); } } static void CommPortSetup() { CommPort.PortName = "COM14"; CommPort.BaudRate = 115200; CommPort.DataBits = 8; CommPort.Parity = Parity.None; CommPort.StopBits = StopBits.One; CommPort.Handshake = Handshake.None; CommPort.ReadTimeout = 200; CommPort.WriteTimeout = 200; CommPort.NewLine = "\r\n"; CommPort.ReceivedBytesThreshold = 80; CommPort.ReadBufferSize = 4096; CommPort.DtrEnable = false; CommPort.RtsEnable = false; } static void CommPortOpen() { if (CommPort == null) return; try { //Console.WriteLine("Opening port"); CommPort.Open(); } catch (Exception ex) { Console.WriteLine("Exception during port open: " + ex.ToString()); } } static void CommPortClose() { if (CommPort == null) return; try { //Console.WriteLine("Closing port"); CommPort.Close(); //Console.WriteLine("Port Closed"); } catch (Exception ex) { Console.WriteLine("Exception during port close: " + ex.ToString()); } } }