Esempio n. 1
0
        /// <summary> Reads the thread. </summary>
        private static void ReadThread()
        {
            string data = "";

            while (_backgroundRunning)
            {
                try
                {
                    if (_serialPort.BytesToRead > 0)
                    {
                        data += _serialPort.ReadExisting();
                    }
                    int eol = data.IndexOf('\n');
                    while (eol >= 0)
                    {
                        string message = data.Substring(0, eol + 1).Trim(' ', '\r', '\n');
                        data = data.Remove(0, eol + 1);
                        eol  = data.IndexOf('\n');

                        if (!string.IsNullOrEmpty(message))
                        {
                            DataReceived?.Invoke(message);
                            StoreResponse(message);
                        }
                    }
                    Thread.Sleep(50);
                }
                catch (TimeoutException)
                {
                }
            }
        }
Esempio n. 2
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. 3
0
 public void OnDataReceived(string connection, byte[] data)
 {
     DataReceived?.Invoke(connection, data);
 }
 public void OnDataReceived(string connection, ushort remotePort, byte[] data)
 {
     DataReceived?.Invoke(connection, remotePort, data);
 }