Exemplo n.º 1
0
        public async Task Send(byte[] message)
        {
            if (SelectedDevice?.State != DeviceState.Connected)
            {
                return;
            }

            var size = message.Length;

            var lastDataPacketSize       = size % Settings.PacketSize;
            var numberOfCompletePackages = size / Settings.PacketSize;

            for (int i = 0; i < numberOfCompletePackages; i++)
            {
                var data = new ArraySegment <byte>(message, i * Settings.PacketSize, Settings.PacketSize);
                await WriteToDevice(data.ToArray());
            }
            if (lastDataPacketSize > 0)
            {
                var data = new ArraySegment <byte>(message, numberOfCompletePackages * Settings.PacketSize, lastDataPacketSize);
                await WriteToDevice(data.ToArray());
            }

            BytesSent?.Invoke(this, message);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write byte array to client.
        /// </summary>
        /// <param name="buffer"></param>
        public void WriteToClient(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            TcpStream.Write(buffer, 0, buffer.Length);
            BytesSent?.Invoke(this, new TcpServerDataEventArgs(this, buffer, buffer.Length));
            TcpStream.Flush();
        }
Exemplo n.º 3
0
 public void Connect(string portName)
 {
     Disconnect();
     Arduino                = new ArduinoSerial(portName);
     Arduino.BytesSent     += (port, bytes) => BytesSent?.Invoke(port, bytes);
     Arduino.BytesReceived += (port, bytes) => BytesReceived?.Invoke(port, bytes);
     Arduino.Connect(false);
     _thread = new Thread(Loop);
     _thread.IsBackground = true;
     _thread.Start();
 }
Exemplo n.º 4
0
        public ConnectResult TryConnect(string portName, bool sayhello)
        {
            if (portName == "")
            {
                return(ConnectResult.InvalidArgument);
            }
            Disconnect();
            EventWaitHandle ewh    = new EventWaitHandle(false, EventResetMode.AutoReset);
            ConnectResult   result = ConnectResult.None;

            Arduino                = new ArduinoSerial(portName);
            Arduino.BytesSent     += (port, bytes) => BytesSent?.Invoke(port, bytes);
            Arduino.BytesReceived += (port, bytes) => BytesReceived?.Invoke(port, bytes);
            ArduinoSerial.StatusChangedHandler statuschanged = status =>
            {
                lock (ewh)
                {
                    if (result != ConnectResult.None)
                    {
                        return;
                    }
                    if (status == ArduinoSerial.Status.Connected || status == ArduinoSerial.Status.ConnectedUnsafe)
                    {
                        result = ConnectResult.Success;
                        ewh.Set();
                    }
                    if (status == ArduinoSerial.Status.Error)
                    {
                        result = ConnectResult.Error;
                        ewh.Set();
                    }
                }
            };
            Arduino.StatusChanged += statuschanged;
            Arduino.Connect(sayhello);
            if (!ewh.WaitOne(300) && sayhello)
            {
                Arduino.Disconnect();
                Arduino = null;
                return(ConnectResult.Timeout);
            }
            if (result != ConnectResult.Success)
            {
                Arduino.Disconnect();
                Arduino = null;
                return(result);
            }
            Arduino.StatusChanged -= statuschanged;
            _thread = new Thread(Loop);
            _thread.IsBackground = true;
            _thread.Start();
            return(ConnectResult.Success);
        }
Exemplo n.º 5
0
        private void OnBytesSent(INetworkNode To, int NumberOfBytes)
        {
            if ((BytesSent == null) || (To == null) || (NumberOfBytes <= 0))
            {
                return;
            }

            Task.Run(() =>
            {
                BytesSent?.Invoke(this, new InternetBytesTransferredEventArgs
                {
                    Remote    = To,
                    Local     = this,
                    Direction = CommunicationDirection.Outbound,
                    NumBytes  = NumberOfBytes
                });
            });
        }
Exemplo n.º 6
0
 void VideosInsertRequest_ProgressChanged(IUploadProgress Progress)
 {
     BytesSent?.Invoke(Progress.BytesSent);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Write byte array to client.
 /// </summary>
 /// <param name="buffer"></param>
 public void WriteToClient(byte[] buffer)
 {
     TcpStream.Write(buffer, 0, buffer.Length);
     BytesSent?.Invoke(this, new TcpServerDataEventArgs(this, buffer, buffer.Length));
     TcpStream.Flush();
 }