/// <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; } } }
protected virtual void OnDataRecieved(DataRecievedArgs e) { DataRecieved(this, e); }
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); } } }
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 = RP121032.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 = RP121032.EncodeJ1587Message(nextJ1587Message); 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 = RP121032.ByteArrayToHexString(message.data); string displayString = "RX J1587 - " + message.TimeStamp + " MID: " + message.MID + " PID: " + message.PID; displayString += " Data: " + RP121032.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); } } }