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); }
private void btnWrite_Click(object sender, System.EventArgs e) { TCLightMsg MsgToSend; TextBox CurrentTextBox; CANResult Res; // We create a TCLightMsg message structure // MsgToSend = new TCLightMsg(); // We configurate the Message. The ID (max 0x1FF), // Length of the Data, Message Type (Standard in // this example) and die data // MsgToSend.ID = Convert.ToUInt32(txtID.Text,16); MsgToSend.Len = Convert.ToByte(nudLength.Value); MsgToSend.MsgType = (chbExtended.Checked) ? MsgTypes.MSGTYPE_EXTENDED : MsgTypes.MSGTYPE_STANDARD; // If a remote frame will be sent, the data bytes are not important. // if(chbRemote.Checked) MsgToSend.MsgType |= MsgTypes.MSGTYPE_RTR; else { // We get so much data as the Len of the message // CurrentTextBox = txtData0; for(int i=0; i < MsgToSend.Len; i++) { MsgToSend.Data[i] = Convert.ToByte(CurrentTextBox.Text,16); if(i < 7) CurrentTextBox = (TextBox)this.GetNextControl(CurrentTextBox,true); } } // The message is sent to the configured hardware // Res = PCANLight.Write(ActiveHardware, MsgToSend); // The Hardware was successfully sent // if(Res == CANResult.ERR_OK) txtInfo.Text = "Message was successfully SENT.\r\n"; // An error occurred. We show the error. // else txtInfo.Text = "Error: " + Res.ToString(); }
public MessageStatus(TCLightMsg CanMsg,int Index) { Msg = CanMsg; iIndex = Index; }
/// <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; } }
/// <summary> /// PCANLight Read function /// This function get the next message or the next error from the Receive Queue of /// the CAN Hardware. /// 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> /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns> public static CANResult Read(HardwareType HWType, out TCLightMsg Msg) { 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; CANResult resTemp; Msg = null; try { switch (HWType) { case HardwareType.ISA_1CH: resTemp = (CANResult)PCAN_ISA.Read(out MsgIsa); Msg = new TCLightMsg(MsgIsa); return resTemp; case HardwareType.ISA_2CH: resTemp = (CANResult)PCAN_2ISA.Read(out MsgIsa2); Msg = new TCLightMsg(MsgIsa2); return resTemp; case HardwareType.PCI_1CH: MsgPci = new PCAN_PCI.TPCANMsg(); MsgPci.DATA = new byte[8]; resTemp = (CANResult)PCAN_PCI.Read(out MsgPci); Msg = new TCLightMsg(MsgPci); return resTemp; case HardwareType.PCI_2CH: resTemp = (CANResult)PCAN_2PCI.Read(out MsgPci2); Msg = new TCLightMsg(MsgPci2); return resTemp; case HardwareType.PCC_1CH: resTemp = (CANResult)PCAN_PCC.Read(out MsgPcc); Msg = new TCLightMsg(MsgPcc); return resTemp; case HardwareType.PCC_2CH: resTemp = (CANResult)PCAN_2PCC.Read(out MsgPcc2); Msg = new TCLightMsg(MsgPcc2); return resTemp; case HardwareType.USB_1CH: resTemp = (CANResult)PCAN_USB.Read(out MsgUsb); Msg = new TCLightMsg(MsgUsb); return resTemp; case HardwareType.USB_2CH: resTemp = (CANResult)PCAN_2USB.Read(out MsgUsb2); Msg = new TCLightMsg(MsgUsb2); return resTemp; case HardwareType.DNP: resTemp = (CANResult)PCAN_DNP.Read(out MsgDnp); Msg = new TCLightMsg(MsgDnp); return resTemp; case HardwareType.DNG: resTemp = (CANResult)PCAN_DNG.Read(out MsgDng); Msg = new TCLightMsg(MsgDng); return resTemp; // Hardware is not valid for this function // default: return CANResult.ERR_ILLHW; } } 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; } }
/// <summary> /// PCANLight Write function /// This function Place a CAN message into the Transmit Queue of the CAN Hardware /// </summary> /// <param name="HWType">In which hardware should be written the CAN Message</param>3 /// <param name="MsgToSend">The TCLightMsg message to be written</param> /// <returns>A CANResult value - Error/status of the hardware after execute the function</returns> public static CANResult Write(HardwareType HWType, TCLightMsg MsgToSend) { 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; try { switch (HWType) { case HardwareType.ISA_1CH: MsgIsa = MsgToSend; return (CANResult)PCAN_ISA.Write(ref MsgIsa); case HardwareType.ISA_2CH: MsgIsa2 = MsgToSend; return (CANResult)PCAN_2ISA.Write(ref MsgIsa2); case HardwareType.PCI_1CH: MsgPci = MsgToSend; return (CANResult)PCAN_PCI.Write(ref MsgPci); case HardwareType.PCI_2CH: MsgPci2 = MsgToSend; return (CANResult)PCAN_2PCI.Write(ref MsgPci2); case HardwareType.PCC_1CH: MsgPcc = MsgToSend; return (CANResult)PCAN_PCC.Write(ref MsgPcc); case HardwareType.PCC_2CH: MsgPcc2 = MsgToSend; return (CANResult)PCAN_2PCC.Write(ref MsgPcc2); case HardwareType.USB_1CH: MsgUsb = MsgToSend; return (CANResult)PCAN_USB.Write(ref MsgUsb); case HardwareType.USB_2CH: MsgUsb2 = MsgToSend; return (CANResult)PCAN_2USB.Write(ref MsgUsb2); case HardwareType.DNP: MsgDnp = MsgToSend; return (CANResult)PCAN_DNP.Write(ref MsgDnp); case HardwareType.DNG: MsgDng = MsgToSend; return (CANResult)PCAN_DNG.Write(ref MsgDng); // Hardware is not valid for this function // default: return CANResult.ERR_ILLHW; } } 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; } }