/// <summary> /// Raises the message received event. /// </summary> /// <param name="args">Arguments.</param> protected virtual void OnMessageReceived(MessageReceivedEventArgs args) { logger.Debug(BitConverter.ToString(args.Data)); if (MessageReceived != null) MessageReceived(this, args); }
private void SerialPort_MessageReceived(object sender, MessageReceivedEventArgs args) { if (dataReceived != null) { dataReceived(args.Data); } if (stringReceived != null) { string textMessage = textBuffer + Encoding.UTF8.GetString(args.Data); if (String.IsNullOrEmpty(textEndOfLine[0])) { // raw string receive try { stringReceived(textMessage); } catch { } } else { // text line based string receive textBuffer = ""; if (textMessage.Contains(textEndOfLine[0])) { string[] lines = textMessage.Split(textEndOfLine, StringSplitOptions.RemoveEmptyEntries); for (int l = 0; l < lines.Length - (textMessage.EndsWith(textEndOfLine[0]) ? 0 : 1); l++) { try { stringReceived(lines[l]); } catch { } } if (!textMessage.EndsWith(textEndOfLine[0])) { textBuffer = lines[lines.Length - 1]; } } } } }
/// <summary> /// Raises the message received event. /// </summary> /// <param name="args">Arguments.</param> protected virtual void OnMessageReceived(MessageReceivedEventArgs args) { if (MessageReceived != null) MessageReceived(this, args); }
static void SerialPort_MessageReceived(object sender, MessageReceivedEventArgs args) { Console.WriteLine("Received message: {0}", BitConverter.ToString(args.Data)); // On every message received we send an ACK message back to the device serialPort.SendMessage(new byte[] { 0x06 }); }
private void SerialPort_MessageReceived(object sender, SerialPortLib.MessageReceivedEventArgs args) { ParseSerialData(args.Data); }
private void SerialPort_MessageReceived(object sender, MessageReceivedEventArgs args) { byte[] message = args.Data; bool isSecurityCode = (message.Length == 4 && ((message[1] ^ message[0]) == 0x0F) && ((message[3] ^ message[2]) == 0xFF)); bool isCodeValid = isSecurityCode || (message.Length == 4 && ((message[1] & ~message[0]) == message[1] && (message[3] & ~message[2]) == message[3])); // Repeated messages check if (isCodeValid) { if (lastRfMessage == BitConverter.ToString(message) && (DateTime.Now - lastRfReceivedTs).TotalMilliseconds < minRfRepeatDelayMs) { logger.Warn("Ignoring repeated message within {0}ms", minRfRepeatDelayMs); return; } lastRfMessage = BitConverter.ToString(message); lastRfReceivedTs = DateTime.Now; } OnRfDataReceived(new RfDataReceivedEventArgs(message)); if (isSecurityCode) { var securityEvent = X10RfSecurityEvent.NotSet; Enum.TryParse<X10RfSecurityEvent>(message[2].ToString(), out securityEvent); uint securityAddress = message[0]; //BitConverter.ToUInt32(new byte[] { message[0] }, 0); if (securityEvent != X10RfSecurityEvent.NotSet) { logger.Debug("Security Event {0} Address {1}", securityEvent, securityAddress); OnRfSecurityReceived(new RfSecurityReceivedEventArgs(securityEvent, securityAddress)); } else { logger.Warn("Could not parse security event"); } } else if (isCodeValid) { // Decode received 32 bit message // house code + 4th bit of unit code // unit code (3 bits) + function code // Parse function code var hf = X10RfFunction.NotSet; Enum.TryParse<X10RfFunction>(message[2].ToString(), out hf); // House code (4bit) + unit code (4bit) byte hu = message[0]; // Parse house code var houseCode = X10HouseCode.NotSet; Enum.TryParse<X10HouseCode>((ReverseByte((byte)(hu >> 4)) >> 4).ToString(), out houseCode); switch (hf) { case X10RfFunction.Dim: case X10RfFunction.Bright: logger.Debug("Command {0}", hf); OnRfCommandReceived(new RfCommandReceivedEventArgs(hf, X10HouseCode.NotSet, X10UnitCode.Unit_NotSet)); break; case X10RfFunction.AllLightsOn: case X10RfFunction.AllLightsOff: if (houseCode != X10HouseCode.NotSet) { logger.Debug("Command {0} HouseCode {1}", hf, houseCode); OnRfCommandReceived(new RfCommandReceivedEventArgs(hf, houseCode, X10UnitCode.Unit_NotSet)); } break; case X10RfFunction.NotSet: logger.Warn("Unable to decode function value"); break; default: // Parse unit code string houseUnit = Convert.ToString(hu, 2).PadLeft(8, '0'); string unitFunction = Convert.ToString(message[2], 2).PadLeft(8, '0'); string uc = (Convert.ToInt16(houseUnit.Substring(5, 1) + unitFunction.Substring(1, 1) + unitFunction.Substring(4, 1) + unitFunction.Substring(3, 1), 2) + 1).ToString(); // Parse module function var fn = X10RfFunction.NotSet; Enum.TryParse<X10RfFunction>(unitFunction[2].ToString(), out fn); switch (fn) { case X10RfFunction.On: case X10RfFunction.Off: var unitCode = X10UnitCode.Unit_NotSet; Enum.TryParse<X10UnitCode>("Unit_" + uc.ToString(), out unitCode); if (unitCode != X10UnitCode.Unit_NotSet) { logger.Debug("Command {0} HouseCode {1} UnitCode {2}", fn, houseCode, unitCode.Value()); OnRfCommandReceived(new RfCommandReceivedEventArgs(fn, houseCode, unitCode)); } else { logger.Warn("Could not parse unit code"); } break; } break; } } else if (message.Length == 1 && message[0] == ackReply) { logger.Debug("W800Rf32 is online"); OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(true)); } else { logger.Warn("Bad message received"); } }