Beispiel #1
0
        /// <summary>
        /// Returns if CAN message is successfully sent.
        /// </summary>
        /// <param name="message">Message.</param>
        /// <returns></returns>
        public bool SendMessage(CanankaMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message), "Message cannot be null.");
            }

            var bytes = new List <byte>();

            if (message.IsRemoteRequest)
            {
                bytes.Add(message.IsExtended ? (byte)'R' : (byte)'r');
            }
            else
            {
                bytes.Add(message.IsExtended ? (byte)'T' : (byte)'t');
            }
            bytes.AddRange(Encoding.ASCII.GetBytes(message.Id.ToString(message.IsExtended ? "X8" : "X3", CultureInfo.InvariantCulture)));
            bytes.Add((byte)(0x30 + message.Length));
            if (!message.IsRemoteRequest)
            {
                foreach (var b in message.GetData())
                {
                    bytes.AddRange(Encoding.ASCII.GetBytes(b.ToString("X2", CultureInfo.InvariantCulture)));
                }
            }
            bytes.Add(CR);

            var response = WriteCommand(bytes.ToArray());

            return(response?.IsPositive ?? false);
        }
Beispiel #2
0
 /// <summary>
 /// Create a new instance.
 /// </summary>
 /// <param name="message">Message.</param>
 /// <exception cref="ArgumentNullException">Message cannot be null.</exception>
 public CanankaMessageEventArgs(CanankaMessage message)
 {
     Message = message ?? throw new ArgumentNullException(nameof(message), "Message cannot be null.");
 }