public bool InitPort(string name, int speed) { serialPort = new System.IO.Ports.SerialPort(); string[] ports = System.IO.Ports.SerialPort.GetPortNames(); string portName = ""; foreach (var port in ports) { if (port == name) { portName = port; } } #region Create Port object,and open serial port try { serialPort = new System.IO.Ports.SerialPort(portName, speed); if (serialPort.IsOpen) { throw new SystemException("Выбранный порт уже используется.Ошибка!"); } AppDomain.CurrentDomain.ProcessExit += SerialProcessExit; return(true); } catch (Exception e) { PortException.AddException(e.Message); return(false); } #endregion }
public string ReadLine() { try { return(serialPort.ReadLine()); } catch (Exception e) { PortException.AddException(e.Message); return(""); } }
public string[] GetAvailablePorts() { try { return(System.IO.Ports.SerialPort.GetPortNames()); } catch (Exception e) { PortException.AddException(e.Message); return(null); } }
public void SysControl() { Task control = Task.Run(() => { while (IsRun && CheckConnect()) { Thread.Sleep(500); } PortException.AddException("No connection!!!"); Error();//Check!!@@ }); }
public bool SendLine(string message) { try { serialPort.WriteLine(message); return(true); } catch (Exception e) { PortException.AddException(e.Message); return(false); } }
public bool Open() { try { serialPort.Open(); return(true); } catch (Exception e) { PortException.AddException(e.Message); return(false); } }
public bool Close() { if (!serialPort.IsOpen) { return(true); } try { serialPort.Close(); return(true); } catch (Exception e) { PortException.AddException(e.Message); return(false); } }