public PeriodicMessage(J1939Message msg, double interval, txJ1939Data txDelegate) { _MessageType = PeriodicMessageType.J1939; _timeKeeper = new System.Timers.Timer(interval); SendJ1939Data = txDelegate; _j1939Message = msg; _EventHandler = new ElapsedEventHandler(_timeKeeper_Elapsed); _timeKeeper.Elapsed += _EventHandler; _timeKeeper.Enabled = true; }
public static byte[] EncodeJ1939Message(J1939Message MessageToEncode) { byte i = 0; byte[] returnValue = new byte[MessageToEncode.dataLength + 6]; returnValue[i++] = (byte)(MessageToEncode.PGN & 0xFF); returnValue[i++] = (byte)((MessageToEncode.PGN >> 8) & 0xFF); returnValue[i++] = (byte)((MessageToEncode.PGN >> 16) & 0xFF); returnValue[i++] |= (byte)(MessageToEncode.Priority); returnValue[i++] = (byte)(MessageToEncode.SourceAddress); returnValue[i++] = (byte)(MessageToEncode.DestinationAddress); foreach (byte temp in MessageToEncode.data) { returnValue[i++] = temp; } return(returnValue); }
public void SendData(J1939Message msgToSend) { if (J1939inst != null) { try { byte[] txArray = RP121032.EncodeJ1939Message(msgToSend); UInt32 canID = (UInt32)((msgToSend.Priority << 26) + (msgToSend.PGN << 8) + msgToSend.SourceAddress); string txline = "H TXJ1939, " + msgToSend.TimeStamp + ", " + canID.ToString("X") + ", " + zcrc.ByteArrayToHexString(msgToSend.data); txtTX.AppendText(txline + Environment.NewLine); RP1210_Returns returnTemp = J1939inst.RP1210_SendMessage(txArray, (short)txArray.Length, 0, 0); txtStatus.Text = returnTemp.ToString(); } catch (Exception err) { txtStatus.Text = err.Message.ToString(); } } }
public static J1939Message DecodeJ1939Message(byte[] message) { // If ECHO_MODE is turned off (default) // TS @ [0-3], PGN @ [4-6], How/Pri @ [7], SRC @ [8], DEST @ [9], Data @ [10-(nLength-10)] // If ECHO_MODE is turned on (NOT the default) // TS @ [0-3], EchoByte @ [4], PGN @ [5-7], How/Pri @ [8], SRC @ [9], DEST @ [10], Data @ [11-(nLength-11)] J1939Message decoded = new J1939Message(); decoded.TimeStamp = (UInt32)((message[0] << 24) + (message[1] << 16) + (message[2] << 8) + message[3]); //DPA is big endian? decoded.PGN = (UInt16)((message[6] << 16) + (message[5] << 8) + message[4]); decoded.Priority = message[7]; decoded.SourceAddress = message[8]; decoded.DestinationAddress = message[9]; decoded.dataLength = (UInt16)(message.Length - 10); decoded.data = new byte[decoded.dataLength]; Array.Copy(message, 10, decoded.data, 0, decoded.dataLength); return(decoded); }
/// <summary> /// This function is meant to be called as a seperate through to continuously /// poll the rp12010 message buffer looking for new messages since the driver /// is not event based. This function generates independent J1587 and J1939 /// data recieved events /// </summary> private void PollingDriver() { while (true) { if (J1939inst != null) { byte[] response = J1939inst.RP1210_ReadMessage(0); if (response.Length > 1) { DataRecievedArgs EventArgs = new DataRecievedArgs(); EventArgs.J1939 = true; rp1210.J1939Message message = RP121032.DecodeJ1939Message(response); if (J1939MessageFilter.Count != 0) { if (J1939MessageFilter.Find(x => x.PGN == message.PGN) != null) { EventArgs.RecievedJ1939Message = message; } } else { EventArgs.RecievedJ1939Message = message; } if ((J1939DataRecieved != null) && (EventArgs.RecievedJ1939Message != null)) { J1939DataRecieved(this, EventArgs); } } } else if (J1587inst != null) { } else { break; } } }
private void tmrJ1939_Tick(object sender, EventArgs e) { if (J1939inst != null) { try { while (true) { byte[] response = J1939inst.RP1210_ReadMessage(0); DataRecievedArgs EventArgs = new DataRecievedArgs(); EventArgs.J1939 = true; rp1210.J1939Message message = RP121032.DecodeJ1939Message(response); EventArgs.RecievedJ1939Message = message; string datastring = zcrc.ByteArrayToHexString(message.data); string displayString = "RX J1939 - " + message.TimeStamp + " PGN: " + message.PGN + " SA: " + message.SourceAddress; displayString += " DA: " + message.DestinationAddress + " Pri: " + message.Priority; displayString += " Data: " + datastring + Environment.NewLine; txtRX.AppendText(displayString); datastring = datastring.Remove(datastring.Length - 1, 1); datastring = datastring.Replace(" ", ", "); if (chkLogToFile.Checked) { UInt32 canID = (UInt32)((message.Priority << 26) + (message.PGN << 8) + message.SourceAddress); RxLogger.WriteLine("H RXJ1939, {0:d}, {1:x}, {2}", message.TimeStamp, canID, datastring); } OnDataRecieved(EventArgs); } } catch (Exception err) { txtStatus.Text = err.Message; } if (TxLogger != null) { while (((timeKeeper == null) || (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs)) && !TxLogger.EndOfStream) { string txline = TxLogger.ReadLine(); txtTX.AppendText(txline + Environment.NewLine); txline = txline.Replace(" ", ""); string[] rawdata = txline.Split(new char[] { ',' }); if (!TimeOffsetsCalculated) { TxLogTimeOffsetMs = Convert.ToUInt32(rawdata[1]); timeKeeper = Stopwatch.StartNew(); TimeOffsetsCalculated = true; } else { nextMessageTimeMs = Convert.ToUInt32(rawdata[1]) - TxLogTimeOffsetMs; } if ((rawdata[0] == "HRXJ1939") && (J1939inst != null)) { nextJ1939Message = new J1939Message(); nextJ1939Message.TimeStamp = Convert.ToUInt32(rawdata[1]); nextJ1939Message.SourceAddress = (short)(Convert.ToInt32(rawdata[2], 16) & 0x00FF); nextJ1939Message.Priority = (byte)(Convert.ToInt32(rawdata[2], 16) >> 26); nextJ1939Message.PGN = (UInt16)((Convert.ToInt32(rawdata[2], 16) >> 8) & 0xFFFF); nextJ1939Message.DestinationAddress = 0xFF; string[] strArrayTemp = new string[rawdata.Length - 3]; Array.Copy(rawdata, 3, strArrayTemp, 0, rawdata.Length - 3); nextJ1939Message.data = Array.ConvertAll(strArrayTemp, x => Convert.ToByte(x, 16)); nextJ1939Message.dataLength = (UInt16)(rawdata.Length - 3); if (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs) { byte[] txArray = RP121032.EncodeJ1939Message(nextJ1939Message); RP1210_Returns returnTemp = J1939inst.RP1210_SendMessage(txArray, (short)txArray.Length, 0, 0); txtStatus.Text = returnTemp.ToString(); } } else if ((rawdata[0] == "HRXJ1708") && (J1587inst != null)) { nextJ1587Message = new J1587Message(); nextJ1587Message.TimeStamp = Convert.ToUInt32(rawdata[1]); nextJ1587Message.Priority = 8; nextJ1587Message.MID = Convert.ToByte(rawdata[2], 16); nextJ1587Message.PID = Convert.ToByte(rawdata[3], 16); string[] strArrayTemp = new string[rawdata.Length - 4]; Array.Copy(rawdata, 3, strArrayTemp, 0, rawdata.Length - 4); nextJ1587Message.data = Array.ConvertAll(strArrayTemp, x => Convert.ToByte(x, 16)); nextJ1587Message.dataLength = (UInt16)(rawdata.Length - 4); if (timeKeeper.ElapsedMilliseconds > nextMessageTimeMs) { byte[] txArray = nextJ1587Message.ToArray(); RP1210_Returns returnTemp = J1587inst.RP1210_SendMessage(txArray, (short)(txArray.Length - 1), 0, 0); txtStatus.Text = returnTemp.ToString(); } } } } } if (J1587inst != null) { byte[] response = J1587inst.RP1210_ReadMessage(0); rp1210.J1587Message message = RP121032.DecodeJ1587Message(response); string datastring = zcrc.ByteArrayToHexString(message.data); string displayString = "RX J1587 - " + message.TimeStamp + " MID: " + message.MID + " PID: " + message.PID; displayString += " Data: " + zcrc.ByteArrayToHexString(message.data) + Environment.NewLine; txtRX.AppendText(displayString); datastring = datastring.Remove(datastring.Length - 1, 1); datastring = datastring.Replace(" ", ", "); if (chkLogToFile.Checked) { RxLogger.WriteLine("H RXJ1708, {0:d}, {1:x2}, {2:x2}, {3}", message.TimeStamp, message.MID, message.PID, datastring); } } }
public void SendPeriodicMessage(J1939Message msgToSend, double interval) { PeriodicMessage newMessage = new PeriodicMessage(msgToSend, interval, SendData); PeriodicMessages.Add(newMessage); }