Esempio n. 1
0
        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();
            }
        }
Esempio n. 2
0
        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();
            }
        }
Esempio n. 3
0
 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
     });
 }
Esempio n. 4
0
 private void DoDataWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
     OnDataReceivedEvent?.Invoke(this, mResult);
     mRawDataList.Clear();
 }