private void StartReceiveCallback(IAsyncResult asyncResult) { Socket socket = asyncResult.AsyncState as Socket; try { socket.EndReceive(asyncResult); buffer = new byte[8192]; int received = socket.Receive(buffer, buffer.Length, 0); if (received <= 0) { Console.WriteLine("No Bytes received, disconnecting.."); throw new SocketException(); } if (buffer.Length > received) { Array.Resize(ref buffer, received); } OnDataReceivedEvent?.Invoke(this, buffer); socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, StartReceiveCallback, null); } catch { OnClientDisconnectedEvent?.Invoke(this); CloseConnection(); } }
private void Receive() { try { while (_isWorking) { if (_serialPort.IsOpen) { var data = _serialPort.ReadLine(); if (OnDataReceivedEvent != null) { OnDataReceivedEvent.Invoke(data); } } else { _isWorking = false; } } _serialPort.Close(); } catch (Exception exception) { if (OnPortErrorEvent != null) { OnPortErrorEvent.Invoke(exception); } } finally { _serialPort.Close(); } }
private void SerialPort_OnDataReceived(object sender, DataReceivedArgs e) { byte[] dataReceived = new byte[e.Data.Length]; foreach (byte b in e.Data) { dataReceived[b] = e.Data[b]; } OnDataReceivedEvent?.Invoke(this, new DataReceivedArgs { Data = dataReceived }); }
private void DoDataWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { OnDataReceivedEvent?.Invoke(this, mResult); mRawDataList.Clear(); }