Пример #1
0
 private async Task Read()
 {
     try
     {
         byte[] buffer = new byte[port.ReadBufferSize];
         while (port.IsOpen)
         {
             int dataLength = 0;
             try
             {
                 dataLength = await port.BaseStream.ReadAsync(buffer, 0, 1);
             }
             catch (IOException)
             {
                 throw new PortClosedException();
             }
             byte[] data = new byte[dataLength];
             Array.Copy(buffer, data, dataLength);
             BytesReceivedEventArgs eventArgs = new BytesReceivedEventArgs(data);
             OnDataReceived(eventArgs);
         }
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
         OnReadExceptionOccured(eventArgs);
     }
 }
Пример #2
0
        public async Task Read()
        {
            try
            {
                byte[] buffer = new byte[tcpClient.ReceiveBufferSize];
                while (true)
                {
                    if (!tcpClient.Connected)
                    {
                        throw new PortClosedException();
                    }
                    NetworkStream inputStream = tcpClient.GetStream();
                    int           dataLength  = await inputStream.ReadAsync(buffer, 0, 100);

                    if (dataLength == 0)
                    {
                        Close();
                        PortClosedEventArgs portClosedEventArgs = new PortClosedEventArgs("Remote tcp endpoint");
                        OnClose(portClosedEventArgs);
                        break;
                    }
                    byte[] data = new byte[dataLength];
                    Array.Copy(buffer, data, dataLength);
                    BytesReceivedEventArgs eventArgs = new BytesReceivedEventArgs(data);
                    OnDataReceived(eventArgs);
                }
            }
            catch (Exception e)
            {
                ExceptionOccuredEventArgs eventArgs = new ExceptionOccuredEventArgs(e);
                OnReadExceptionOccured(eventArgs);
            }
        }
Пример #3
0
 private void BLEDataReceived(GattCharacteristic gattCharacteristic, GattValueChangedEventArgs eventArgs)
 {
     try
     {
         DataReader dataReader = DataReader.FromBuffer(eventArgs.CharacteristicValue);
         byte[]     data       = new byte[dataReader.UnconsumedBufferLength];
         dataReader.ReadBytes(data);
         BytesReceivedEventArgs bytesReceivedEventArgs = new BytesReceivedEventArgs(data);
         OnDataReceived(bytesReceivedEventArgs);
     }
     catch (Exception e)
     {
         ExceptionOccuredEventArgs exceptionOccuredEventArgs = new ExceptionOccuredEventArgs(e);
         OnWaitForConnectionRequestExceptionOccured(exceptionOccuredEventArgs);
     }
 }
Пример #4
0
        public override void ProcessReceivedData(object port, BytesReceivedEventArgs eventArgs)
        {
            if (packetInReceiving.IsEmpty())
            {
                packetInReceiving = Communication.Packet.CreateNewFromXCP(eventArgs.data, false);
            }
            else
            {
                packetInReceiving.Add(eventArgs.data);
            }

            if (packetInReceiving.IsFinished())
            {
                OnPacketReadyForTransfer(packetInReceiving);
                packetInReceiving.Clear();
            }
        }
Пример #5
0
 //if received byte by byte, sending order might be messed up
 public override void ProcessReceivedData(object port, BytesReceivedEventArgs eventArgs)
 {
     packetInReceiving = Communication.Packet.CreateNewFromRaw(eventArgs.data, false);
     OnPacketReadyForTransfer(packetInReceiving);
     packetInReceiving.Clear();
 }
Пример #6
0
        protected void OnDataReceived(BytesReceivedEventArgs eventArgs)
        {
            EventHandler <BytesReceivedEventArgs> handler = BytesReceived;

            handler?.Invoke(this, eventArgs);
        }