public static AsyncSerial SearchPort(string write, char search, int baud=9600)
{
foreach (string port in SerialPort.GetPortNames())
{
SerialPort temp;
try
{
temp = new SerialPort(port, baud);
try
{
temp.Open();
temp.ReadTimeout = 2500;
temp.Write(write);
var v = temp.ReadChar();
MainForm.Instance.logControl.Add(v.ToString(), LogEntryType.Error);
if (v == search)
{
temp.Close();
return new AsyncSerial(port);
}
}
catch (Exception ex)
{}
finally
{
temp.Close();
}
}
catch (Exception ex)
{}
}
throw new Exception("Port not found.");
}