Example #1
0
        /// <summary>
        /// Method for sending a message to the connected unit.
        /// </summary>
        /// <param name="message">The message to send.</param>
        public void SendMessage(string message)
        {
            BluetoothMessage msg = new BluetoothMessage(this.link, message);

            WT32.SendMessage(msg);
            msg.Dispose();
        }
Example #2
0
        public static void SendMessage(BluetoothMessage message)
        {
            if (!isInitialized)
            {
                throw new InvalidOperationException("WT32: Please call Initialize() before sending messages.");
            }

            sendBuffer = Multiplexing.MUX(message);
            bluetooth.Write(sendBuffer, 0, sendBuffer.Length);
            Debug.Print("WT32: Message \"" + message.Command.Trim() + "\" sent to link " + message.Link + ".");
        }
Example #3
0
        /// <summary>
        /// Method for decapsulating the message from a MUX frame.
        /// </summary>
        /// <param name="bytesReceived">The byte array received over the UART interface.</param>
        /// <returns>The decapsulated message.</returns>
        public static BluetoothMessage DeMUX(byte[] bytesReceived)
        {
            if (bytesReceived[0] != 0xBF)
            {
                throw new ArgumentException("ByteArray is not a MUX-message");
            }

            BluetoothMessage message = new BluetoothMessage();

            message.Link = (Link)bytesReceived[1];
            int messageLength = bytesReceived[3];
            int pos           = 0;

            byte[] messageBuffer = new byte[messageLength];
            for (int i = 0; i < messageLength; i++)
            {
                messageBuffer[i] = bytesReceived[pos++];
            }
            message.Command = new string(Encoding.UTF8.GetChars(messageBuffer)).Trim();
            return(message);
        }
Example #4
0
 private static void ReadData()
 {
     while (true)
     {
         int bytesRead = 0;
         while (bytesRead < bluetooth.BytesToRead)
         {
             byte SOF = ReadByte();                                      // Read byte
             if (SOF == 0xBF)                                            // Check for SOF
             {
                 byte link = ReadByte();                                 // Read link
                 ReadByte();                                             // Read flags - not used
                 int    length        = (int)ReadByte();                 // Read data length
                 byte[] receiveBuffer = new byte[length];
                 bluetooth.Read(receiveBuffer, 0, receiveBuffer.Length); // Read data
                 try
                 {
                     string message = new string(Encoding.UTF8.GetChars(receiveBuffer)).Trim();
                     if ((Link)link == Link.Control)
                     {
                         HandleControlCommand(message);
                     }
                     else
                     {
                         BluetoothMessage receivedMessage = new BluetoothMessage((Link)link, message);
                         if (MessageReceived != null)
                         {
                             MessageReceived(receivedMessage);
                         }
                     }
                 }
                 catch (Exception)
                 {
                     Debug.Print("WT32: Unable to read incoming message.");
                 }
             }
             bytesRead++;
         }
     }
 }
Example #5
0
        /// <summary>
        /// Method for encapsulating a message in a MUX frame.
        /// </summary>
        /// <param name="message">The message to encapsulate.</param>
        /// <returns>Byte array of full MUX frame.</returns>
        public static byte[] MUX(BluetoothMessage message)
        {
            if (message.Command == null)
            {
                throw new ArgumentException("Message is not complete. Check the Command property!");
            }

            byte[] messageBuffer = Encoding.UTF8.GetBytes(message.Command);
            int    messageLength = messageBuffer.Length;
            int    pos           = 0;

            byte[] frameBuffer = new byte[5 + messageLength];
            frameBuffer[pos++] = 0xBF;                // SOF (Always 0xBF)
            frameBuffer[pos++] = (byte)message.Link;  // Link (0xFF for control)
            frameBuffer[pos++] = 0x00;                // Flags (Always 0x00)
            frameBuffer[pos++] = (byte)messageLength; // Length (Data length)
            for (int i = 0; i < messageLength; i++)
            {
                frameBuffer[pos++] = messageBuffer[i];              // Data
            }
            frameBuffer[pos++] = (byte)((byte)message.Link ^ 0xFF); //nLink (Link XOR 0xFF)
            return(frameBuffer);
        }