/// <summary> /// Writes a Generic Message, check ActiveHardware /// </summary> /// <param name="ID"></param> /// <param name="PID"></param> /// <param name="DID"></param> /// <param name="SID"></param> /// <param name="Data"></param> public void WriteMessage(uint ID, uint PID, uint DID, uint SID, Byte[] Data) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion TCLightMsg newMsg = new TCLightMsg(); TCLightTimestamp newTime = new TCLightTimestamp(); newMsg.ID = ID | PID | DID | SID; newMsg.MsgType = MsgTypes.MSGTYPE_EXTENDED; newMsg.Len = 8; newMsg.Data = Data; newTime.millis = 1002; console.AddOutsideMessage(newMsg, newTime); PCANLight.Write(console.GetActiveHardware(), newMsg); }
public TCLightTimestamp BuildMessageTime() { TCLightTimestamp time = new TCLightTimestamp(); time.millis = (uint)DateTime.Now.Millisecond; return(time); }
private void AlignMotorBtn_Click(object sender, EventArgs e) { TCLightMsg MsgToSend = new TCLightMsg(); MsgToSend.ID = 0x013E20E0 | 0x00000001; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; TCLightTimestamp q = new TCLightTimestamp(); q.millis = 1000; console.AddOutsideMessage(MsgToSend, q); PCANLight.Write(ActiveHardware, MsgToSend); }
/// <summary> /// Writes a Generic Message /// </summary> /// <param name="msg"></param> /// <param name="time"></param> public void WriteMessage(TCLightMsg msg, TCLightTimestamp time) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion console.AddOutsideMessage(msg, time); PCANLight.Write(console.GetActiveHardware(), msg); }
/// <summary> /// Sends the Execute Command over the CAN Bus /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ExecuteProfileBtn_Click(object sender, EventArgs e) { #region execute TCLightMsg MsgToSend = new TCLightMsg(); MsgToSend.ID = 0x013EF0E0 | 0x00000001; MsgToSend.Len = 0; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; PCANLight.Write(ActiveHardware, MsgToSend); #endregion TCLightTimestamp q = new TCLightTimestamp(); q.millis = 1000; console.AddOutsideMessage(MsgToSend, q); SendProfileBtn.Enabled = false; ExecuteProfileBtn.Enabled = false; }
private void button1_Click(object sender, EventArgs e) { #region setup TCLightMsg MsgToSend = new TCLightMsg(); MsgToSend.ID = 0x013EE8E0 | 0x00000001; MsgToSend.Len = 1; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; MsgToSend.Data[0] = numPackets; TCLightTimestamp q = new TCLightTimestamp(); q.millis = 1000; console.AddOutsideMessage(MsgToSend, q); PCANLight.Write(ActiveHardware, MsgToSend); #endregion }
private void NPTModeBtn_Click(object sender, EventArgs e) { TCLightMsg MsgToSend = new TCLightMsg(); MsgToSend.ID = 0x01745400 | 0x00000001 | 0x000000E0; MsgToSend.Len = 8; MsgToSend.Data[0] = (byte)0x01; MsgToSend.Data[1] = (byte)0x00; MsgToSend.Data[2] = (byte)0x00; MsgToSend.Data[3] = (byte)0x00; MsgToSend.Data[4] = (byte)0x00; MsgToSend.Data[5] = (byte)0x00; MsgToSend.Data[6] = (byte)0x00; MsgToSend.Data[7] = (byte)0x00; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; TCLightTimestamp q = new TCLightTimestamp(); q.millis = 1000; console.AddOutsideMessage(MsgToSend, q); PCANLight.Write(ActiveHardware, MsgToSend); }
private void ReadMessage() { TCLightMsg MyMsg; TCLightTimestamp MyTimeStamp = null; CANResult Res; // We read at least one time the queue looking for messages. // If a message is found, we look again trying to find more. // If the queue is empty or an error occurr, we get out from // the dowhile statement. // do { // We read the queue looking for messages. // if (chbTimeStamp.Checked) { // We execute the "ReadEx" function of the PCANLight // if "Show Time Stamp" checkbox is selected // Res = PCANLight.ReadEx(ActiveHardware, out MyMsg, out MyTimeStamp); } else { // We execute the "Read" function of the PCANLight // if "Show Time Stamp" checkbox isn't selected // Res = PCANLight.Read(ActiveHardware, out MyMsg); } // A message was received // We process the message(s) // if (Res == CANResult.ERR_OK) { ProcessMessage(MyMsg, MyTimeStamp); } } while (((int)ActiveHardware != -1) && (!Convert.ToBoolean(Res & CANResult.ERR_QRCVEMPTY))); }
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); } }
/// <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 // Tracer.WriteError(TraceGroup.CANBUS, null, "ReadEx wrong version {0}", Ex.Message + "\""); return CANResult.ERR_NO_DLL; } catch (Exception Ex) { // Error: Dll does not exists or the function is not available // Tracer.WriteError(TraceGroup.CANBUS, null, "ReadEx {0}", Ex.Message + "\""); return CANResult.ERR_NO_DLL; } }
/// <summary> /// Sends the Command List over the CAN Bus /// </summary> /// <param name="cmds"></param> /// <param name="numPackets"></param> private void SendCommandList(List <String> cmds, byte numPackets) { #region ValCheck if (cmds == null || numPackets == 0) { return; } #endregion #region setup TCLightMsg MsgToSend = new TCLightMsg(); MsgToSend.ID = 0x013EE8E0 | 0x00000001; MsgToSend.Len = 1; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; MsgToSend.Data[0] = numPackets; TCLightTimestamp q = new TCLightTimestamp(); q.millis = 1000; console.AddOutsideMessage(MsgToSend, q); PCANLight.Write(ActiveHardware, MsgToSend); #endregion #region move MsgToSend.ID = 0x013EE0E0 | 0x00000001; MsgToSend.Len = 8; MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED; byte count = 0; foreach (String cmd in cmds) { String[] cmd2 = cmd.Split('~'); for (int i = 0; i < MsgToSend.Len; i++) { #region Data switch (i) { case 0: ProfileParamsTb.AppendText(cmd + "\r\n"); MsgToSend.Data[0] = count; count++; break; case 1: MsgToSend.Data[1] = numPackets; break; case 2: MsgToSend.Data[2] = BitConverter.GetBytes(Int16.Parse(cmd2[0]))[1]; break; case 3: MsgToSend.Data[3] = BitConverter.GetBytes(Int16.Parse(cmd2[0]))[0]; break; case 4: MsgToSend.Data[4] = BitConverter.GetBytes(UInt16.Parse(cmd2[1]))[1]; break; case 5: MsgToSend.Data[5] = BitConverter.GetBytes(UInt16.Parse(cmd2[1]))[0]; break; case 6: MsgToSend.Data[6] = BitConverter.GetBytes(UInt16.Parse(cmd2[2]))[1]; break; case 7: MsgToSend.Data[7] = BitConverter.GetBytes(UInt16.Parse(cmd2[2]))[0]; break; } #endregion } console.AddOutsideMessage(MsgToSend, q); PCANLight.Write(ActiveHardware, MsgToSend); } #endregion #region Delete //prevents sending repeats, consider disabling if you want such a feature cmds = null; numPackets = 0; #endregion }
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 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); }