private void ProcessMessage(TCLightMsg MyMsg, TCLightTimestamp MyTimeStamp) { bool bFound = false; MessageStatus CurrMessage; // Initialization // CurrMessage = new MessageStatus(); // We search if a message (Smae ID and Type) is // already received or if this is a new message // for(int i=0; i< LastMsgsList.Count; i++) { CurrMessage = (MessageStatus)LastMsgsList[i]; if(CurrMessage.CANMessage.ID == MyMsg.ID) if(CurrMessage.CANMessage.MsgType == MyMsg.MsgType) { bFound = true; break; } } if(bFound) // Messages of this kind are already received; we make an update // ModifyMsgEntry(CurrMessage,MyMsg, MyTimeStamp); else // Messages of this kind are not received; we make a new entry // InsertMsgEntry(MyMsg, MyTimeStamp); }
private void InsertMsgEntry(TCLightMsg NewMsg, TCLightTimestamp MyTimeStamp) { string strNewData,strTemp; ListViewItem CurrItem; MessageStatus CurrMsg; strTemp = strNewData = ""; // Add the new ListView Item with the Type of the message // if((NewMsg.MsgType & MsgTypes.MSGTYPE_EXTENDED) != 0) strTemp = "EXTENDED"; else strTemp = "STANDARD"; if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) == MsgTypes.MSGTYPE_RTR) strTemp += "/RTR"; CurrItem = lstMessages.Items.Add(strTemp); // We format the ID of the message and show it // if((NewMsg.MsgType & MsgTypes.MSGTYPE_EXTENDED) != 0) CurrItem.SubItems.Add(string.Format("{0:X8}h",NewMsg.ID)); else CurrItem.SubItems.Add(string.Format("{0:X3}h",NewMsg.ID)); // We set the length of the Message // CurrItem.SubItems.Add(NewMsg.Len.ToString()); // We format the data of the message. Each data is one // byte of Hexadecimal data // if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) == MsgTypes.MSGTYPE_RTR) strNewData = "Remote Request"; else for(int i=0; i< NewMsg.Len; i++) strNewData += string.Format("{0:X2} ",NewMsg.Data[i]); CurrItem.SubItems.Add(strNewData); // The message is the First, so count is 1 and there // is not any time difference between messages. // CurrItem.SubItems.Add("1"); // Add time stamp information if need be // if (MyTimeStamp != null) CurrItem.SubItems.Add(MyTimeStamp.millis.ToString() + "." + MyTimeStamp.micros.ToString()); // We add this status in the last message list // CurrMsg = new MessageStatus(NewMsg,CurrItem.Index); LastMsgsList.Add(CurrMsg); }
private void ModifyMsgEntry(MessageStatus LastMsg, TCLightMsg NewMsg, TCLightTimestamp MyTimeStamp) { string strLastData, strNewData; MessageStatus NewStatus; ListViewItem CurrItem; uint iLen; int iCount; strNewData = strLastData = ""; // Values from the last messages // CurrItem = lstMessages.Items[LastMsg.Position]; iCount = Convert.ToInt32(CurrItem.SubItems[4].Text); strLastData = CurrItem.SubItems[3].Text; iLen = LastMsg.CANMessage.Len; // New values // if((NewMsg.MsgType & MsgTypes.MSGTYPE_RTR) != 0) strNewData = "Remote Request"; else for(int i=0; i< NewMsg.Len; i++) strNewData += string.Format("{0:X2} ",NewMsg.Data[i]); // Count is updated // iCount += 1; // Set the changes // if(iLen != NewMsg.Len) { iLen = NewMsg.Len; CurrItem.SubItems[2].Text = iLen.ToString(); } if(strLastData != strNewData) CurrItem.SubItems[3].Text = strNewData; CurrItem.SubItems[4].Text = iCount.ToString(); // Update time stamp information if need be // if (MyTimeStamp != null) { String timeStamp = MyTimeStamp.millis.ToString() + "." + MyTimeStamp.micros.ToString(); // Add new SubItem if it doesn't exists // if (CurrItem.SubItems.Count == 5) CurrItem.SubItems.Add(timeStamp); // Else update existing SubItem // else CurrItem.SubItems[5].Text = timeStamp; } // Save the new Status of the message // NOTE: The objects are saved in the same index position which // they use in the Listview // NewStatus = new MessageStatus(NewMsg,LastMsg.Position); LastMsgsList.RemoveAt(LastMsg.Position); LastMsgsList.Insert(LastMsg.Position,NewStatus); }
/// <summary> /// PCANLight ReadEx function /// This function get the next message or the next error from the Receive Queue of /// the CAN Hardware and the time when the message arrived. /// REMARK: /// - Check always the type of the received Message (MSGTYPE_STANDARD,MSGTYPE_RTR, /// MSGTYPE_EXTENDED,MSGTYPE_STATUS) /// - The function will return ERR_OK always that you receive a CAN message successfully /// although if the messages is a MSGTYPE_STATUS message. /// - When a MSGTYPE_STATUS mesasge is got, the ID and Length information of the message /// will be treated as indefined values. Actually information of the received message /// should be interpreted using the first 4 data bytes as follow: /// * Data0 Data1 Data2 Data3 Kind of Error /// 0x00 0x00 0x00 0x02 CAN_ERR_OVERRUN 0x0002 CAN Controller was read to late /// 0x00 0x00 0x00 0x04 CAN_ERR_BUSLIGHT 0x0004 Bus Error: An error counter limit reached (96) /// 0x00 0x00 0x00 0x08 CAN_ERR_BUSHEAVY 0x0008 Bus Error: An error counter limit reached (128) /// 0x00 0x00 0x00 0x10 CAN_ERR_BUSOFF 0x0010 Bus Error: Can Controller went "Bus-Off" /// - If a CAN_ERR_BUSOFF status message is received, the CAN Controller must to be /// initialized again using the Init() function. Otherwise, will be not possible /// to send/receive more messages. /// </summary> /// <param name="HWType">From which hardware should be read a CAN Message</param> /// <param name="Msg">The TCLightMsg structure to store the CAN message</param> /// <param name="RcvTime">The TCLightTimestamp structure to store the timestamp of the CAN message</param> /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns> public static CANResult ReadEx(HardwareType HWType, out TCLightMsg Msg, out TCLightTimestamp RcvTime) { PCAN_ISA.TPCANMsg MsgIsa; PCAN_2ISA.TPCANMsg MsgIsa2; PCAN_PCI.TPCANMsg MsgPci; PCAN_2PCI.TPCANMsg MsgPci2; PCAN_PCC.TPCANMsg MsgPcc; PCAN_2PCC.TPCANMsg MsgPcc2; PCAN_USB.TPCANMsg MsgUsb; PCAN_2USB.TPCANMsg MsgUsb2; PCAN_DNP.TPCANMsg MsgDnp; PCAN_DNG.TPCANMsg MsgDng; PCAN_ISA.TPCANTimestamp RcvTimeIsa; PCAN_2ISA.TPCANTimestamp RcvTimeIsa2; PCAN_PCI.TPCANTimestamp RcvTimePci; PCAN_2PCI.TPCANTimestamp RcvTimePci2; PCAN_PCC.TPCANTimestamp RcvTimePcc; PCAN_2PCC.TPCANTimestamp RcvTimePcc2; PCAN_USB.TPCANTimestamp RcvTimeUsb; PCAN_2USB.TPCANTimestamp RcvTimeUsb2; PCAN_DNP.TPCANTimestamp RcvTimeDnp; PCAN_DNG.TPCANTimestamp RcvTimeDng; CANResult resTemp; Msg = null; RcvTime = null; try { switch (HWType) { case HardwareType.ISA_1CH: resTemp = (CANResult)PCAN_ISA.ReadEx(out MsgIsa, out RcvTimeIsa); Msg = new TCLightMsg(MsgIsa); RcvTime = new TCLightTimestamp(RcvTimeIsa); return resTemp; case HardwareType.ISA_2CH: resTemp = (CANResult)PCAN_2ISA.ReadEx(out MsgIsa2, out RcvTimeIsa2); Msg = new TCLightMsg(MsgIsa2); RcvTime = new TCLightTimestamp(RcvTimeIsa2); return resTemp; case HardwareType.PCI_1CH: resTemp = (CANResult)PCAN_PCI.ReadEx(out MsgPci, out RcvTimePci); Msg = new TCLightMsg(MsgPci); RcvTime = new TCLightTimestamp(RcvTimePci); return resTemp; case HardwareType.PCI_2CH: resTemp = (CANResult)PCAN_2PCI.ReadEx(out MsgPci2, out RcvTimePci2); Msg = new TCLightMsg(MsgPci2); RcvTime = new TCLightTimestamp(RcvTimePci2); return resTemp; case HardwareType.PCC_1CH: resTemp = (CANResult)PCAN_PCC.ReadEx(out MsgPcc, out RcvTimePcc); Msg = new TCLightMsg(MsgPcc); RcvTime = new TCLightTimestamp(RcvTimePcc); return resTemp; case HardwareType.PCC_2CH: resTemp = (CANResult)PCAN_2PCC.ReadEx(out MsgPcc2, out RcvTimePcc2); Msg = new TCLightMsg(MsgPcc2); RcvTime = new TCLightTimestamp(RcvTimePcc2); return resTemp; case HardwareType.USB_1CH: resTemp = (CANResult)PCAN_USB.ReadEx(out MsgUsb, out RcvTimeUsb); Msg = new TCLightMsg(MsgUsb); RcvTime = new TCLightTimestamp(RcvTimeUsb); return resTemp; case HardwareType.USB_2CH: resTemp = (CANResult)PCAN_2USB.ReadEx(out MsgUsb2, out RcvTimeUsb2); Msg = new TCLightMsg(MsgUsb2); RcvTime = new TCLightTimestamp(RcvTimeUsb2); return resTemp; case HardwareType.DNP: resTemp = (CANResult)PCAN_DNP.ReadEx(out MsgDnp, out RcvTimeDnp); Msg = new TCLightMsg(MsgDnp); RcvTime = new TCLightTimestamp(RcvTimeDnp); return resTemp; case HardwareType.DNG: resTemp = (CANResult)PCAN_DNG.ReadEx(out MsgDng, out RcvTimeDng); Msg = new TCLightMsg(MsgDng); RcvTime = new TCLightTimestamp(RcvTimeDng); return resTemp; // Hardware is not valid for this function // default: return CANResult.ERR_ILLHW; } } catch (EntryPointNotFoundException Ex) { // Function is not available in the loaded Dll // System.Windows.Forms.MessageBox.Show("Error: Wrong Version. \"" + Ex.Message + "\""); return CANResult.ERR_NO_DLL; } catch (Exception Ex) { // Error: Dll does not exists or the function is not available // System.Windows.Forms.MessageBox.Show("Error: \"" + Ex.Message + "\""); return CANResult.ERR_NO_DLL; } }