public void SendCAN(CANMessage canMessage) { if (Verbose) { Console.WriteLine("<-- {0}", canMessage.ToString()); } StringBuilder serialCanMessage = new StringBuilder(); if (canMessage.IsExtended) { serialCanMessage.Append(canMessage.IsRTR ? "R" : "T"); serialCanMessage.Append(canMessage.Id.ToString("X08")); } else { serialCanMessage.Append(canMessage.IsRTR ? "r" : "t"); serialCanMessage.Append(canMessage.Id.ToString("X03")); } if (canMessage.Data != null && canMessage.Data.Length > 0) { serialCanMessage.Append(canMessage.Data.Length.ToString("X")); } else { serialCanMessage.Append("0"); } if (!canMessage.IsRTR) { serialCanMessage.Append(canMessage.Data.ByteArrayToHexString()); } SendData(serialCanMessage.ToString()); Thread.Sleep(DelayAfterWriteMilliseconds); }
private void ParseCANMessage(string dataLine) { if (string.IsNullOrEmpty(dataLine)) { throw new ArgumentException(); } switch (dataLine[0]) { case 'T': case 'R': //Extended frame CANMessage msg = new CANMessage(); if (dataLine.Length >= 10) { //parse address (8 HEX characters) msg.IsExtended = true; msg.IsRTR = dataLine[0] == 'R'; msg.Id = UInt32.Parse(dataLine.Substring(1, 8), System.Globalization.NumberStyles.HexNumber); int dataLength = UInt16.Parse(dataLine.Substring(9, 1), System.Globalization.NumberStyles.HexNumber); if (!msg.IsRTR && dataLength > 0) { //parse data if (dataLine.Length == 10 + dataLength * 2) { msg.Data = dataLine.Substring(10, dataLength * 2).HexStringToByteArray(); } else { //expected more or less data than present throw new FormatException(); } } else { msg.Data = new byte[0]; } if (Verbose) { Console.WriteLine("--> {0}", msg.ToString()); } CANMessageReceived?.Invoke(this, new CANMessageReceivedEventArgs() { CANMessage = msg }); } else { //Ignore illegal format } break; case 't': case 'r': //Basic frame msg = new CANMessage(); if (dataLine.Length >= 5) { //parse address (3 HEX characters) msg.IsExtended = false; msg.IsRTR = dataLine[0] == 'r'; msg.Id = UInt32.Parse(dataLine.Substring(1, 3), System.Globalization.NumberStyles.HexNumber); int dataLength = UInt16.Parse(dataLine.Substring(4, 1), System.Globalization.NumberStyles.HexNumber); if (!msg.IsRTR && dataLength > 0) { //parse data if (dataLine.Length == 5 + dataLength * 2) { msg.Data = dataLine.Substring(5, dataLength * 2).HexStringToByteArray(); } else { //expected more or less data than present throw new FormatException(); } } else { msg.Data = new byte[0]; } } else { throw new FormatException(); } CANMessageReceived?.Invoke(this, new CANMessageReceivedEventArgs() { CANMessage = msg }); break; default: throw new ArgumentException(); } }