Esempio n. 1
0
        /// <summary>
        /// Send the command to the bluetooth socket. Returns true when command was sent and a response was received.
        /// </summary>
        /// <param name="aCallbackDataReceived">Callback for the received data.</param>
        /// <returns>True when command was sent and a response was received.</returns>
        public bool Send(DataReceivedDelegate aCallbackDataReceived)
        {
            if (Socket.IsConnected)
            {
                lock (fSyncObject)
                {
                    byte[] lCmd = Encoding.ASCII.GetBytes(BuildCommand(CommandPrefix, BaseCommand, CommandParameter));
                    Socket.OutputStream.Write(lCmd, 0, lCmd.Length);
                    Socket.OutputStream.Flush();
                    Thread.Sleep(50);

                    string lResponse = ReadResponse();
                    if (!string.IsNullOrWhiteSpace(lResponse))
                    {
                        string lExtractedData = ValidateAndExtractData(lResponse);
                        if (!string.IsNullOrWhiteSpace(lExtractedData))
                        {
                            if (GetValue(lResponse, out T lValue))
                            {
                                aCallbackDataReceived?.Invoke(lValue);
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 2
0
 public void Register(DataReceivedDelegate callback, bool autoremove)
 {
     lock (_callbacks)
     {
         _callbacks.Add(Tuple.Create(callback, autoremove));
     }
 }
Esempio n. 3
0
        public EV3Receiver(string portName, DataReceivedDelegate dataReceivedDelegate, int interval)
        {
            this.dataReceivedDelegate = dataReceivedDelegate;
            try {
                EV3ComPort.PortName = portName;
                EV3ComPort.BaudRate = 9600;
                EV3ComPort.DataBits = 8;
                EV3ComPort.Parity = Parity.None;
                EV3ComPort.StopBits = StopBits.One;

                EV3ComPort.Open();

                EV3ComPort.ReadTimeout = 20000;

                EV3ComPort.DtrEnable = true;
                EV3ComPort.RtsEnable = true;

                NXTMailbox1CallbackTimer = new System.Timers.Timer(interval);
                NXTMailbox1CallbackTimer.Elapsed += new ElapsedEventHandler(NXTMailbox1CallbackHandler);
                NXTMailbox1CallbackTimer.Enabled = true;

            } catch (System.IO.IOException ex) {
                Console.WriteLine(ex.ToString());
                Console.Read();
                return;

            } catch (ArgumentException ex) {
                Console.WriteLine(ex.ToString());
                Console.Read();
                return;
            }
        }
Esempio n. 4
0
 void m_Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     if (InvokeRequired)
     {
         DataReceivedDelegate d = new DataReceivedDelegate(UpdateTextBox);
         Invoke(d, new object[] { e });
     }
     else
     {
         UpdateTextBox(e);
     }
 }
Esempio n. 5
0
        public SerialPortConnection(
            DataReceivedDelegate dataReceivedDelegate,
            string comPort        = "Com1",
            int baud              = 115200,
            Parity parity         = Parity.None,
            int dataBits          = 8,
            StopBits stopBits     = StopBits.One,
            int receiveBufferSize = 10000)
        {
            Disposed = false;

            this.dataReceivedDelegate = dataReceivedDelegate;

            this.serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits)
            {
                ReadTimeout  = 1000,
                WriteTimeout = 1000,
            };

            this.serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }
Esempio n. 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="dataHandler">Data handler</param>
 /// <param name="statusChangeHandler">Channel status handler</param>
 public SCPClientChannelEventReceiver(DataReceivedDelegate dataHandler, ChannelStatusChangedDelegate statusChangeHandler)
 {
     this._dataHandler = dataHandler;
     this._statusChangeHandler = statusChangeHandler;
 }
Esempio n. 7
0
 public void UnsubscribeFromOnDataReceived(DataReceivedDelegate _func)
 {
     m_onDataReceived -= _func;
 }
Esempio n. 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="dataHandler">Data handler</param>
 /// <param name="statusChangeHandler">Channel status handler</param>
 public SCPClientChannelEventHandler(DataReceivedDelegate dataHandler, ChannelStatusChangedDelegate statusChangeHandler)
 {
     this._dataHandler         = dataHandler;
     this._statusChangeHandler = statusChangeHandler;
 }
Esempio n. 9
0
 public void SubscribeToOnDataReceived(DataReceivedDelegate _func)
 {
     m_onDataReceived -= _func;
     m_onDataReceived += _func;
 }
Esempio n. 10
0
 /// <summary>
 /// Wait for response.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Caller should lock ResponseNotifier before send a request packet,
 /// and this method should be called in the lock-block.
 /// </para>
 /// </remarks>
 /// <param name="responseHandler">delegate which handles response data</param>
 /// <param name="millisecondsTimeout">timeout in milliseconds</param>
 /// <exception cref="SFTPClientTimeoutException">Timeout has occured.</exception>
 /// <exception cref="Exception">an exception which was thrown while executing responseHandler.</exception>
 public void WaitResponse(DataReceivedDelegate responseHandler, int millisecondsTimeout)
 {
     // Note: This lock is not required if the caller has locked ResponseNotifier.
     //       We do this for make clear that Monitor.Wait() is called in the locked context.
     lock (_responseNotifier) {
         _isDataIncomplete = false;
         _dataTotal = 0;
         _responseHandler = responseHandler;
         _responseHandlerException = null;
         bool signaled = Monitor.Wait(_responseNotifier, millisecondsTimeout);
         _responseHandler = null;
         if (_responseHandlerException != null)
             throw new SFTPClientException(_responseHandlerException.Message, _responseHandlerException);
         if (!signaled)
             throw new SFTPClientTimeoutException();
     }
 }
Esempio n. 11
0
 public void registerReceivedDelegate(DataReceivedDelegate pDataReceived)
 {
     #region 数据处理异步委托
     m_pDataReceived = pDataReceived;
     #endregion
 }
Esempio n. 12
0
 /// <summary>
 /// Sets callback delegate for handling received data.
 /// </summary>
 /// <param name="lDataReceivedDelegate">Callback delegate.</param>
 public void SetReceiveHandler(DataReceivedDelegate lDataReceivedDelegate)
 {
     cDataReceivedDelegate = lDataReceivedDelegate;
 }
Esempio n. 13
0
        public TeliaModem(System.Windows.Forms.Control mainThreadOwnder)
        {
            _mainThreadOwnder = mainThreadOwnder;

            dataReceivedDelegate = new DataReceivedDelegate(OnDataReceived);
        }
Esempio n. 14
0
        /// <summary>
        /// Wait for response.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Caller should lock ResponseNotifier before send a request packet,
        /// and this method should be called in the lock-block.
        /// </para>
        /// </remarks>
        /// <param name="responseHandler">delegate which handles response data</param>
        /// <param name="millisecondsTimeout">timeout in milliseconds</param>
        /// <exception cref="SFTPClientTimeoutException">Timeout has occured.</exception>
        /// <exception cref="Exception">an exception which was thrown while executing responseHandler.</exception>
        public void WaitResponse(DataReceivedDelegate responseHandler, int millisecondsTimeout)
        {
            lock (_dataBufferSync) {
                bool processed = false;
                while (!processed) {
                    while (_dataBuffer.Length < 4) {
                        if (!Monitor.Wait(_dataBufferSync, millisecondsTimeout)) {
                            throw new SFTPClientTimeoutException();
                        }
                    }

                    int totalSize = SSHUtil.ReadInt32(_dataBuffer.RawBuffer, _dataBuffer.RawBufferOffset);

                    while (_dataBuffer.Length < 4 + totalSize) {
                        if (!Monitor.Wait(_dataBufferSync, millisecondsTimeout)) {
                            throw new SFTPClientTimeoutException();
                        }
                    }

                    _dataBuffer.RemoveHead(4);      // length field

                    if (totalSize >= 1) {
                        SSH2DataReader reader = new SSH2DataReader(
                                    new DataFragment(_dataBuffer.RawBuffer, _dataBuffer.RawBufferOffset, totalSize));
                        SFTPPacketType packetType = (SFTPPacketType)reader.ReadByte();
                        if (responseHandler != null) {
                            processed = responseHandler(packetType, reader);
                        }
                        else {
                            processed = true;
                        }
                    }

                    _dataBuffer.RemoveHead(totalSize);
                }
            }
        }