public void Disconnect() { m_bDoDisconnect = true; ConnectionState = EnConnState.DISCONNECTING; ConnectionStateChange?.Invoke(this, null); return; }
public void Connect(String portName, UInt32 baudRate) { if (m_bConnected) { return; } try { m_serialPort.PortName = portName; m_serialPort.BaudRate = (Int32)baudRate; m_serialPort.DataBits = 8; m_serialPort.StopBits = System.IO.Ports.StopBits.One; m_serialPort.Parity = System.IO.Ports.Parity.None; m_serialPort.Handshake = System.IO.Ports.Handshake.None; m_serialPort.ReadTimeout = 1; m_serialPort.WriteTimeout = 100; m_serialPort.Open(); } catch { System.Windows.Forms.MessageBox.Show("Fehler"); ConnectionState = EnConnState.FAILED_TO_CONNECTED; ConnectionStateChange?.Invoke(this, null); return; } ConnectionState = EnConnState.CONNECTED; ConnectionStateChange?.Invoke(this, null); m_bConnected = true; m_bDoDisconnect = false; if (m_connectorThread != null) { if ((m_connectorThread.ThreadState == ThreadState.Unstarted) || (m_connectorThread.ThreadState == ThreadState.Stopped)) { m_connectorThread = null; } } m_connectorThread = new Thread(new ThreadStart(Connectorthread)) { Name = "Connector_connectorthread" }; m_connectorThread.Start(); return; }
public override void OnConnectionStateChange(bt.BluetoothGatt gatt, [GeneratedEnum] bt.GattStatus status, [GeneratedEnum] bt.ProfileState newState) { base.OnConnectionStateChange(gatt, status, newState); ConnectionStateChange?.Invoke(this, new ConnectionStateChangeEventArgs(gatt, status, newState)); if (!_servicesDiscovered) { gatt.DiscoverServices(); } }
private void Connectorthread() { StringBuilder rx = new StringBuilder(10000); Boolean bRxInProgress = false; while (!m_bDoDisconnect) { Byte rxbyte; try { rxbyte = (Byte)m_serialPort.ReadByte(); } catch (TimeoutException) { rxbyte = 0; } catch { ConnectionState = EnConnState.BROKEN; ConnectionStateChange?.Invoke(this, null); break; } if (rxbyte > 0) { if (!bRxInProgress) { if ((rxbyte == (Byte)':') || (rxbyte == (Byte)'\'') || (rxbyte == (Byte)'#')) { bRxInProgress = true; rx.Clear(); rx.Append((Char)rxbyte); } } else { if (rxbyte != 0x03) { rx.Append((Char)rxbyte); } else { bRxInProgress = false; String msg = rx.ToString(); if (msg[0] == ':') { NewRespData?.Invoke(this, msg); System.Threading.Monitor.Enter(m_txmsgs); if (m_rxdebit > 0) { --m_rxdebit; } System.Threading.Monitor.Exit(m_txmsgs); } else if (msg[0] == '\'') { NewDisplayData?.Invoke(this, msg); } else if (msg[0] == '#') { NewDAQData?.Invoke(this, msg); } else { NewCorruptData?.Invoke(this, msg); } } } } String txmsg = null; System.Threading.Monitor.Enter(m_txmsgs); if (m_txmsgs.Count > 0) { txmsg = m_txmsgs.Dequeue(); } System.Threading.Monitor.Exit(m_txmsgs); if (txmsg != null) { NewRespData?.Invoke(this, txmsg); Byte[] tmp = Encoding.ASCII.GetBytes(txmsg); Byte[] tx = new Byte[tmp.Length + 1]; tmp.CopyTo(tx, 0); tx[tx.Length - 1] = 10; m_serialPort.Write(tx, 0, tx.Length); } } if (m_serialPort.IsOpen) { m_serialPort.Close(); } m_bConnected = false; ConnectionState = EnConnState.DISCONNECTED; ConnectionStateChange?.Invoke(this, null); return; }