public void Start() { try { _port?.Dispose(); string selectedPort = "COM4"; _port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); _port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);//Home _port.Open(); _port.Write("#STAR\n"); _port.DtrEnable = true; //koka _port.Write("#LED1ON\n"); _port.Write("#LED2ON\n"); GoToBase(); IsStepperReady = true; CurrentDistance = MIN_DISTANCE; } catch (Exception e) { IsStepperReady = false; Logger.Error(e); //throw; } OnStatusChanged(IsStepperReady); }
public bool Connect(string name = null) { if (String.IsNullOrEmpty(name)) { _log.LogDebug($"No serial port specified, checking for first available port"); name = SerialPort.GetPortNames()?.FirstOrDefault(); } if (String.IsNullOrEmpty(name)) { _log.LogDebug("No serial port found, cannot connect"); return(false); } if (_port?.IsOpen == true) { _log.LogDebug($"Port already open, closing and retrying"); _port.Close(); } _port?.Dispose(); _log.LogDebug($"debug connecting to {name}"); _port = new SerialPort(name, 19200); _port.Parity = Parity.None; _port.StopBits = StopBits.Two; _port.ReadBufferSize = 256; _port.ReadTimeout = ReadTimeout; _port.WriteBufferSize = AqualogicProtocolConstants.KEY_FRAME_LENGTH * 2; _port.Handshake = Handshake.None; _port.DataReceived += _port_DataReceived; _port.Open(); return(true); }
/// <inheritdoc/> public virtual void Dispose() { if (disposed) { return; } disposed = true; serialPort?.Dispose(); }
//private static Logger logger = LogManager.GetLogger("port"); public static SerialPort InitializePort1() { logger.Info("Port1Initialize()"); port?.Close(); port?.Dispose(); try { port = new SerialPort(PublicData.port1Name); port.WriteTimeout = 1500; port.ReadTimeout = 1500; logger.Debug($"new SerialPort() {port.PortName}"); } catch (TimeoutException ex) { logger.Error(ex, ex.Message); } catch (Exception ex) { ex.Show($"port1: COM-порт \"{PublicData.port1Name}\" не корректен."); port = new SerialPort(); } try { port.BaudRate = int.Parse(PublicData.port1_rate); } catch { port.BaudRate = 9600; } PublicData.port1_rate = port.BaudRate.ToString(); return(port); }
/// <inheritdoc /> public void Dispose() { _serialPortSearchTimer?.Dispose(); _serialPortHealthTimer?.Dispose(); _serialPort?.Dispose(); _logger = null; }
public void Dispose() { _port?.Dispose(); _port = null; _cts.Cancel(); _cts?.Dispose(); }
public static SerialPort InitializePortGenr() { genr?.Close(); genr?.Dispose(); try { genr = new SerialPort(PublicData.port2Name); genr.WriteTimeout = 1000; } catch (Exception z) { z.Show(PublicData.port2Name); genr = new SerialPort(); } try { genr.BaudRate = int.Parse(PublicData.port2_rate); } catch { genr.BaudRate = 9600; } PublicData.port2_rate = genr.BaudRate.ToString(); return(genr); }
private void DisposeConnections() { m_tcpClient?.Dispose(); m_udpClient?.Dispose(); m_serialClient?.Dispose(); m_modbusConnection?.Dispose(); }
private void Initialize() { if (isDisposed) { throw new ObjectDisposedException(GetType().FullName); } try { serialPort = new SerialPort { PortName = PortName, BaudRate = (int)BaudRate, DataBits = DataBits, Parity = Parity, StopBits = StopBits, Handshake = Handshake, ReadBufferSize = BufferSize, WriteBufferSize = BufferSize, ReadTimeout = ReceiveTimeout, WriteTimeout = SendTimeout }; serialPort.DataReceived += OnDataReceived; serialPort.Open(); StartTime = DateTime.UtcNow; } catch { serialPort?.Dispose(); serialPort = null; throw; } }
public void TearDown() { _handler1?.Dispose(); _handler2?.Dispose(); _port1?.Dispose(); _port2?.Dispose(); }
protected override void OverridableDispose() { _serialPort?.Close(); _serialPort?.Dispose(); Task.Delay(250) .Wait(); // Based on MSDN Documentation, should sleep after calling Close or some functionality will not be determinant. }
private void OnConnect(object sender, RoutedEventArgs e) { try { _port?.Close(); _port?.Dispose(); } catch (Exception) { } _port = new SerialPort(SelectedPort.Name); _port.Open(); Task.Run(() => { try { while (true) { System.Diagnostics.Debug.WriteLine(_port.ReadLine()); } } catch (Exception) { } }); }
public bool ConnectToPort(string str_com_port_name_) { com_port_name = str_com_port_name_; if (IS_COM_PORT_OPENED) { close_com_port(); } serialPort?.Dispose(); // Исключения: // T:System.IO.IOException: // Не удается найти или открыть указанный порт. try { serialPort = new SerialPort(com_port_name); } catch (System.IO.IOException e) { serialPort = null; } if (IS_COM_PORT_CREATED) { serialPort.DataReceived += serialPort_ReceiveData; } return(IS_COM_PORT_CREATED); }
public void closeSerialPort(SerialPort serialPort, Action <string> portStatus) { try { if (serialPort != null && serialPort.IsOpen) { try { serialPort.Close(); } catch { MessageBox.Show("Error"); } } else { MessageBox.Show("Serial port is not opened!"); } } catch (NullReferenceException e) { MessageBox.Show(e.Message.ToString()); } finally { serialPort?.Dispose(); } string status = checkPortStatus(serialPort); portStatus(status); }
private void DisconnectButton_Click(object sender, RoutedEventArgs e) { Console.WriteLine("Disconnect."); serial?.Close(); serial?.Dispose(); serial = null; }
public void Disconnect() { isConnected = false; serialPort?.Close(); serialPort?.Dispose(); serialPort = null; }
private void ShowSerialConfigForm() { if (serialPortConfig.PortIsOpen == false) { var form = new Form_SerialConfig(serialPortConfig); var result = form.ShowDialog(); if (result == DialogResult.OK) { baudComboBox.SelectedIndex = serialPortConfig.BaudRateIndex; stopBitsComboBox.SelectedIndex = serialPortConfig.StopBitsIndex; dataBitsComboBox.SelectedIndex = serialPortConfig.DataBitsIndex; parityComboBox.SelectedIndex = serialPortConfig.ParityIndex; protocolComboBox.SelectedIndex = serialPortConfig.ProtocolIndex; comName = form.ComText; btnConnect_Click(null, null); } } else { master = null; serialPort?.Close(); serialPort?.Dispose(); timerDraw.Enabled = false; timerSave.Enabled = false; labelConnect.Text = "未连接"; pictureBoxConnect.BackColor = Color.Red; serialPortConfig.PortIsOpen = false; labelStatus.Text = "端口关闭"; MessageBox.Show("断开连接!"); } }
/// <summary> /// /// </summary> public void Dispose() { _serialPort?.Dispose(); _cancelTokenSource.Cancel(); _readTask?.Wait(); _cancelTokenSource?.Dispose(); _dataWaitHandle?.Dispose(); }
public void Dispose() { if (_port.IsOpen) { _port.Close(); } _port?.Dispose(); }
public void TearDown() { _device?.Dispose(); _device = null; _port?.Dispose(); _port = null; }
protected virtual void Dispose(bool disposing) { if (disposing) { _serialPort?.Dispose(); _serialPort = null; } }
public bool Open(string portName = null) { try { m_serialPort?.Close(); } catch (Exception e) { Console.WriteLine(e); } m_serialPort?.Dispose(); try { if (portName == null) { portName = m_portName; } else { m_portName = portName; } m_serialPort = new SerialPort(portName); } catch (Exception e) { Console.WriteLine(e); return(false); } m_serialPort.BaudRate = 115200; m_serialPort.Parity = Parity.None; m_serialPort.StopBits = StopBits.One; m_serialPort.DataBits = 8; m_serialPort.Handshake = Handshake.None; m_serialPort.RtsEnable = true; m_serialPort.DtrEnable = true; m_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); try { m_serialPort.Open(); } catch (Exception e) { Console.WriteLine(e); m_serialPort.Dispose(); m_serialPort = null; return(false); } return(true); }
protected virtual void Dispose(bool disposing) { if (!isDisposed) { _serialPort?.Close(); _serialPort?.Dispose(); isDisposed = true; } }
protected override void Dispose(bool disposing) { if (disposing) { GC.SuppressFinalize(this); } serialPort?.Dispose(); }
public void Dispose() { if (serialPort.IsOpen) { serialPort.Close(); } serialPort?.Dispose(); }
private void Stop() { if (serialPort != null) { serialPort?.Close(); serialPort?.Dispose(); serialPort = null; } }
public override void Dispose() { base.Dispose(); if (_readPort != _writePort) { _writePort?.Dispose(); } _readPort?.Dispose(); }
/// <summary> /// SC18IM700への接続をすべてクローズ /// </summary> public static void Dispose() { deviceCount = 0; if (IsValid()) { serialPort.Close(); } serialPort?.Dispose(); serialPort = null; }
protected virtual void Dispose(bool disposing) { if (disposing) { // get rid of managed resources Close(); serialPort?.Dispose(); } // get rid of unmanaged resources }
/// <summary> /// Disconnects the client. /// </summary> /// <returns>An awaitable task.</returns> public Task Disconnect() { logger?.LogTrace("ModbusClient.Disconnect"); if (isDisposed) { throw new ObjectDisposedException(GetType().FullName); } if (isStarted) { var connected = IsConnected; logger?.LogInformation("ModbusClient stopping"); serialPort?.Dispose(); serialPort = null; if (driverModified) { try { var rs485 = GetDriverState(); rs485.Flags = serialDriverFlags; SetDriverState(rs485); driverModified = false; } catch (Exception ex) { logger?.LogError(ex, "ModbusClient.Disconnect failed to reset the serial driver state."); } } isStarted = false; if (connected) { logger?.LogTrace("ModbusClient.Disconnect fire disconnected event."); Task.Run(() => Disconnected?.Invoke(this, EventArgs.Empty)).Forget(); } } return(Task.CompletedTask); }