/// <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); }
private void SetupStepBtn_Click(object sender, EventArgs e) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region Declarations TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_EXT); #endregion #region Data msg.Data[0] = 0x5; msg.Data[1] = (byte)BitConverter.GetBytes((short)StepStartAbsNtb.Value)[1]; msg.Data[2] = (byte)BitConverter.GetBytes((short)StepStartAbsNtb.Value)[0]; msg.Data[3] = (byte)BitConverter.GetBytes((short)StepIncrNtb.Value)[1]; msg.Data[4] = (byte)BitConverter.GetBytes((short)StepIncrNtb.Value)[0]; msg.Data[5] = (byte)TriggerComboBox1.SelectedIndex; msg.Data[6] = 0; msg.Data[7] = 0; #endregion #region Write writer.WriteMessage(msg); #endregion }
public void RequestSettings() { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region Declarations TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_AWS); #endregion #region Data msg.Data[0] = 0x7; #endregion #region Write writer.WriteMessage(msg); #endregion }
private void ExecuteStepIncrBtn_Click(object sender, EventArgs e) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region Declarations TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_AWS); #endregion #region Data msg.Data[0] = 0x4; msg.Data[1] = 0; msg.Data[2] = 0; msg.Data[3] = 0; msg.Data[4] = 0; msg.Data[5] = 0; msg.Data[6] = 0; msg.Data[7] = 0; #endregion #region Write writer.WriteMessage(msg); #endregion }
private void StepDelayDownBtn_Click(object sender, EventArgs e) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region Declarations TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_EXT); #endregion #region Data msg.Data[0] = (byte)CanTestFuncVals.gbiTestFunctions.GBI_TEST_FUNCTION_INDEX_ADJ_STEP_START_DELAY; msg.Data[1] = 0; msg.Data[2] = 0; msg.Data[3] = 0; msg.Data[4] = 0; msg.Data[5] = 0; msg.Data[6] = 0; msg.Data[7] = 0; #endregion writer.WriteMessage(msg); }
private void TriggerComboBox_SelectedIndexChanged(object sender, EventArgs e) { TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_EXT); msg.Len = 3; msg.Data[0] = 6; if (sender == TriggerComboBox2) { msg.Data[1] = (byte)TriggerComboBox2.SelectedIndex; } else if (sender == TriggerComboBox1) { msg.Data[1] = (byte)TriggerComboBox1.SelectedIndex; } else if (sender == TriggerComboBox3) { msg.Data[1] = (byte)TriggerComboBox3.SelectedIndex; } else if (sender == TriggerComboBox4) { msg.Data[1] = (byte)TriggerComboBox4.SelectedIndex; } writer.WriteMessage(msg); }
private void NPTModeBtn_Click(object sender, EventArgs e) { #region Declarations TCLightMsg NPTMsg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_OPMODE, (uint)PID.CAN_PID_COMMAND, GetDID(), (uint)SID.CAN_SID_EXT); #endregion #region Data NPTMsg.Data[0] = 0x01;//Mode to enter NPTMsg.Data[1] = (byte)DateTime.Now.Month; NPTMsg.Data[2] = (byte)DateTime.Now.Day; NPTMsg.Data[3] = (byte)(DateTime.Now.Year - 2000); NPTMsg.Data[4] = (byte)DateTime.Now.Hour; NPTMsg.Data[5] = (byte)DateTime.Now.Minute; NPTMsg.Data[6] = (byte)DateTime.Now.Second; #endregion #region Write writer.WriteMessage(NPTMsg); #endregion }
// how is this different??? private void StepSendBtn_Click(object sender, EventArgs e) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region Declarations TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_SET_NODE_TEST_FUNCTION, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_AWS); #endregion #region Data msg.Data[0] = 0x2; msg.Data[1] = 0x1; msg.Data[2] = (byte)BitConverter.GetBytes((short)StartValNtb.Value)[1]; msg.Data[3] = (byte)BitConverter.GetBytes((short)StartValNtb.Value)[0]; msg.Data[4] = (byte)BitConverter.GetBytes((short)StepValNtb.Value)[1]; msg.Data[5] = (byte)BitConverter.GetBytes((short)StepValNtb.Value)[0]; msg.Data[6] = (byte)BitConverter.GetBytes((short)StepCountNtb.Value)[0]; msg.Data[7] = 0; #endregion #region Write writer.WriteMessage(msg); #endregion }
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 void WriteMessage(TCLightMsg msg) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion console.AddOutsideMessage(msg, BuildMessageTime()); PCANLight.Write(console.GetActiveHardware(), msg); }
public TCLightMsg BuildMessage(uint ID, uint PID, uint DID, uint SID) { TCLightMsg newMsg = new TCLightMsg(); newMsg.ID = ID | PID | DID | SID; newMsg.MsgType = MsgTypes.MSGTYPE_EXTENDED; newMsg.Len = 8; return(newMsg); }
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); }
public void SendStartResponse(TCLightMsg msg) { //Start Send Seg ConsoleForm.RemoveMsgToResponse((uint)MSG.CAN_BTLD_SVCE_START); //foreach (TCLightMsg msg in BootLoader.ParsedMsgs) //{ // console.AddOutsideMessage(msg, writer.BuildMessageTime()); //} console.AddMsgToResponse((uint)MSG.CAN_BLK_SEND_SEG, SendSegResponse); //writer.WriteMessage(BootLoader.GetMsg(), writer.BuildMessageTime()); }
public TCLightMsg BuildCanBlkSendSegResponse() { TCLightMsg newMsg = new TCLightMsg(); newMsg.ID = (uint)MSG.CAN_BLK_SEND_SEG | (uint)PID.CAN_PID_OTHER | (uint)DID.CAN_DID_EXT | (uint)SID.CAN_SID_THD; newMsg.MsgType = MsgTypes.MSGTYPE_EXTENDED; newMsg.Len = 8; return(newMsg); }
public static TCLightMsg GetMsg() { try { return(ParsedMsgs[++index]); } catch { TCLightMsg msg = new TCLightMsg(); msg.ID = 0; msg.Data[0] = 0; msg.MsgType = MsgTypes.MSGTYPE_EXTENDED; msg.Len = 8; return(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 }
public void SendSegResponse(TCLightMsg msg) { //remove the message ConsoleForm.RemoveMsgToResponse((uint)MSG.CAN_BLK_SEND_SEG); #region Declarations TCLightMsg currMsg; TCLightMsg DescMsg = writer.BuildMessage((uint)MSG.CAN_BLK_SEG_DESC, (uint)PID.CAN_PID_OTHER, BootLoader.GetDID(), (uint)SID.CAN_SID_EXT); bool finished = false; #endregion //Send the segment while (true) { currMsg = BootLoader.GetMsg(); if (currMsg.Data[0] == currMsg.Data[1] && console.MsgEqual((uint)MSG.CAN_BLK_SEG_DATA, currMsg.ID)) { if (finished) { writer.WriteMessage(currMsg); break; } else { console.AddMsgToResponse((uint)MSG.CAN_BLK_SEND_SEG, SendSegResponse); writer.WriteMessage(currMsg); break; } } else { writer.WriteMessage(currMsg); } } }
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 JumpBtLdrBtn_Click(object sender, EventArgs e) { #region Declarations TCLightMsg BtLdJmpMsg = writer.BuildMessage((uint)MSG.CAN_BTLD_JMP_TO_BOOTLDR, (uint)PID.CAN_PID_COMMAND, GetDID(), (uint)SID.CAN_SID_EXT); //TCLightMsg DnldJmpMsg = writer.BuildMessage((uint)MSG.CAN_DNLD_JMP_TO_BOOTLDR, // (uint)PID.CAN_PID_COMMAND, // (uint)DID.CAN_DID_THD, // (uint)SID.CAN_SID_EXT); #endregion #region Write writer.WriteMessage(BtLdJmpMsg); //writer.WriteMessage(DnldJmpMsg, writer.BuildMessageTime()); #endregion }
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> /// Returns an enumeration of Messages to be sent /// </summary> private IEnumerable <TCLightMsg> DownloadParser() { #region Declarations StreamReader DwnldFile; try { DwnldFile = new StreamReader(openFileDialog1.FileName); } catch (ArgumentException) { yield break; } #region FileValCheck if (DwnldFile.Peek() == -1) { yield break; } #endregion //TCLightMsg SendMsg = writer.BuildCanBlkSendSeg(); TCLightMsg StartMsg = writer.BuildMessage((uint)MSG.CAN_BTLD_SVCE_START, (uint)PID.CAN_PID_COMMAND, GetDID(), (uint)SID.CAN_SID_EXT); yield return(StartMsg); String segment; #endregion while ((segment = DwnldFile.ReadLine()) != null)//for each line/segment { //yield return SendMsg; #region Description Message TCLightMsg DescMsg = writer.BuildMessage((uint)MSG.CAN_BLK_SEG_DESC, (uint)PID.CAN_PID_OTHER, GetDID(), (uint)SID.CAN_SID_EXT); DescMsg.Data[0] = Byte.Parse("" + segment.ElementAt(1)); DescMsg.Data[2] = Byte.Parse("" + segment.ElementAt(2) + segment.ElementAt(3), System.Globalization.NumberStyles.AllowHexSpecifier); //3 and 4 here. change 5 and 6 maybe. Change start position // 1 == 2-bytes, 2 == 3-bytes, 3 == 4-bytes //if desc[0] == 1, 2, or 3 if (DescMsg.Data[0] == 1 || DescMsg.Data[0] == 2 || DescMsg.Data[0] == 3) { //for each byte for (int i = 0; i < DescMsg.Data[0] + 1; i++) { DescMsg.Data[DescMsg.Data.Length - 2 - i] = Byte.Parse("" + segment.ElementAt(4 + ((DescMsg.Data[0] - i) * 2)) + segment.ElementAt(5 + ((DescMsg.Data[0] - i) * 2)), System.Globalization.NumberStyles.AllowHexSpecifier); } } //DescMsg.Data[5] = Byte.Parse("" + segment.ElementAt(4) + // segment.ElementAt(5), // System.Globalization.NumberStyles.AllowHexSpecifier); //DescMsg.Data[6] = Byte.Parse("" + segment.ElementAt(6) + // segment.ElementAt(7), // System.Globalization.NumberStyles.AllowHexSpecifier); DescMsg.Data[7] = Byte.Parse("" + segment.ElementAt(segment.Length - 2) + segment.ElementAt(segment.Length - 1), //Length -1 == last index System.Globalization.NumberStyles.AllowHexSpecifier); yield return(DescMsg); #endregion #region Data Message int position = 0; for (byte messageNum = 0; messageNum < (byte)Math.Ceiling(((decimal)(DescMsg.Data[2] - 3) / 6)); messageNum++)//for each message { TCLightMsg DataMsg = writer.BuildMessage((uint)MSG.CAN_BLK_SEG_DATA, (uint)PID.CAN_PID_OTHER, GetDID(), (uint)SID.CAN_SID_EXT); for (byte byteNum = 0; byteNum < 6; byteNum++)//for each byte in message { //the "* 2" is so that we record a byte then move one byte (instead of one character, a half byte) each iteration //in other words, the "* 2" is so that we operate in bytes, not characters position = (3 + DescMsg.Data[0] + messageNum * 6 + byteNum) * 2; //DescMsg.Data[0] = number of bytes stolen by DescMsg for the address if (position < segment.Length - 2) //clips off the Checksum from the DataMsg (stolen by the DescMsg) { DataMsg.Data[2 + byteNum] = Byte.Parse(segment.ElementAt(position).ToString() + segment.ElementAt(1 + position).ToString(), System.Globalization.NumberStyles.AllowHexSpecifier); } else if (DataMsg.Data[0] == (DataMsg.Data[1]))//if last of seg { if (DescMsg.Data[0] > 3) { DataMsg.Len = (byte)(2 + ((DescMsg.Data[2] - 3) % 6)); } //total len minus the bytes stolen by DescMsg, add two (the first two bytes of data are always used for size), all minus ("total divided by 6" floored) x 6 (gets difference ) = len DataMsg.Len = (byte)(2 + ((DescMsg.Data[2] - DescMsg.Data[0] - 2) % 6));// - ((int)Math.Floor(((decimal)DescMsg.Data[2] - 3) / 6) * 6)); } #region CASE: End of Line //if (i == DescMsg.Data[2] - 4)//OR if Last //{ // DataMsg.Data[0] = (byte)Math.Ceiling(((decimal)i)); // DataMsg.Data[1] = (byte)Math.Ceiling(((decimal)DescMsg.Data[2] - 3) / 6); // if (DataMsg.Data[0] != DataMsg.Data[1]) // { // //console.AddMiscMsg("Error: Improper Format. Reached end of line and Lengths do not match (Data[0] and Data[1] are not equal)."); // } // yield return DataMsg; // //console.AddMiscMsg("wwwwwwwwwwwwwwwwwwwwwwwwwwww"); //} #endregion } #region Full Message if (DescMsg.Data[0] == 1 || DescMsg.Data[0] == 2 || DescMsg.Data[0] == 3) { DataMsg.Data[0] = (byte)(messageNum + 1); DataMsg.Data[1] = (byte)Math.Ceiling(((decimal)DescMsg.Data[2] - DescMsg.Data[0] - 2) / 6); yield return(DataMsg); } else if (DescMsg.Data[0] == 0) { } else if (DescMsg.Data[0] > 3) { DataMsg.Data[0] = (byte)(messageNum + 1); DataMsg.Data[1] = (byte)Math.Ceiling(((decimal)DescMsg.Data[2] - 3) / 6); DataMsg.Data[2] = DescMsg.Data[0]; yield return(DataMsg); } #endregion } #endregion } yield break; }
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); }
public MessageStatus(TCLightMsg CanMsg, int Index) { Msg = CanMsg; iIndex = Index; }
public static CANResult SendSync(BusInterfaces busInterface) { CANResult result = CANResult.ERR_BUSOFF; int busIndex = (int)busInterface; if (null == context[busIndex]) { context[busIndex] = new BusContext(); } if (false != context[busIndex].active) { TCLightMsg msg; msg = new TCLightMsg(); msg.ID = (uint)0x080; msg.Len = (byte)0; msg.MsgType = MsgTypes.MSGTYPE_STANDARD; result = PCANLight.Write(context[busIndex].activeHardware, msg); if (CANResult.ERR_OK != result) { Tracer.WriteError(context[busIndex].traceGroup, "", "sync send failure {0}", result.ToString()); } else { Tracer.WriteError(context[busIndex].traceGroup, "", "sync"); } } return (result); }
public static CANResult SendTimestamp(BusInterfaces busInterface, UInt32 timeStamp) { CANResult result = CANResult.ERR_BUSOFF; int busIndex = (int)busInterface; if (null == context[busIndex]) { context[busIndex] = new BusContext(); } if (false != context[busIndex].active) { TCLightMsg msg; msg = new TCLightMsg(); msg.ID = (uint)0x100; msg.Len = (byte)4; msg.MsgType = MsgTypes.MSGTYPE_STANDARD; msg.Data[0] = (byte)((timeStamp >> 0) & 0xFF); msg.Data[1] = (byte)((timeStamp >> 8) & 0xFF); msg.Data[2] = (byte)((timeStamp >> 16) & 0xFF); msg.Data[3] = (byte)((timeStamp >> 24) & 0xFF); result = PCANLight.Write(context[busIndex].activeHardware, msg); if (CANResult.ERR_OK != result) { Tracer.WriteError(context[busIndex].traceGroup, "", "timestamp send failure {0}", timeStamp); } else { Tracer.WriteError(context[busIndex].traceGroup, "", "timestamp"); } } return (result); }
/// <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 SendFileBtn_Click(object sender, EventArgs e) { #region ValCheck if ((int)console.GetActiveHardware() == -1) { return; } #endregion #region File Len char[] currByte = new char[2]; System.IO.StreamReader inFile = new System.IO.StreamReader(openFileDialog1.FileName); UInt32 fileCharLen = (UInt32)(new System.IO.FileInfo(openFileDialog1.FileName)).Length; //while (inFile.Read() >= 0)//feels wrong //{ // fileCharLen++; //} Byte Len = (Byte)(Math.Ceiling((Decimal)((fileCharLen / 12) + 1)));//cast city woo woo inFile = new System.IO.StreamReader(openFileDialog1.FileName); #endregion #region Message Setup TCLightMsg msg = writer.BuildMessage((uint)MSG.CAN_VAL_DBG_INFO, (uint)PID.CAN_PID_COMMAND, (uint)DID.CAN_DID_THD, (uint)SID.CAN_SID_AWS); #endregion #region Data Setup String temp = ""; Byte currPacketNum = 1; int count = 2; #endregion #region Data Loop msg.Data[1] = Len; while (currPacketNum <= Len) //until end { inFile.Read(currByte, 0, 2); //get a Byte //convert Byte-halves to a String temp += currByte[0]; temp += currByte[1]; //convert string to a byte (yes this is inefficient) then add it to the payload at 'count' position msg.Data[count] = Byte.Parse(temp, System.Globalization.NumberStyles.HexNumber); //write latest packet number to byte[0], only the last will be kept msg.Data[0] = currPacketNum; //reset the temporary string temp = ""; if (count == 7)//if payload is full, write the message, reset, and increment the packet number { writer.WriteMessage(msg); count = 2; currPacketNum += 1; } else//else, increment count { count++; } }//end loop //write any remaining values if (count > 2) { for (; count < 7; count++) { msg.Data[count] = 0; } msg.Data[0] = currPacketNum; //write last message writer.WriteMessage(msg); } #endregion inFile.Close(); }
/// <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 // Tracer.WriteError(TraceGroup.CANBUS, null, "Read {0}", 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 // Tracer.WriteError(TraceGroup.CANBUS, null, "Write {0}", Ex.Message + "\""); return CANResult.ERR_NO_DLL; } }
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); }
public static CANResult Send(BusInterfaces busInterface, int id, byte[] data) { CANResult result = CANResult.ERR_BUSOFF; int busIndex = (int)busInterface; if (null == context[busIndex]) { context[busIndex] = new BusContext(); } if (false != context[busIndex].active) { TCLightMsg msg; msg = new TCLightMsg(); msg.ID = (uint)id; msg.Len = (byte)data.Length; msg.MsgType = MsgTypes.MSGTYPE_STANDARD; for (int i = 0; i < data.Length; i++) { msg.Data[i] = data[i]; } result = PCANLight.Write(context[busIndex].activeHardware, msg); if (CANResult.ERR_OK != result) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sb.AppendFormat("{0:X2}", data[i]); } Tracer.WriteError(context[busIndex].traceGroup, "", "send failure {0} data {1}", result, sb.ToString()); } } return (result); }
/// <summary> /// On Trigger, updates the GBI Page with values directly from the node /// </summary> /// <param name="msg">triggering message</param> public void ValNodeTestFunctionResponse(TCLightMsg msg) { if (msg.Data[0] == 8) //Not sure what this signifies, but it needs to be 8 for the GBI page { switch (msg.Data[1]) //literally just a switch value { //updates control items with values directly from the node case 0: console.GBICmd.IdleMv = BitConverter.ToUInt16(new byte[2] { msg.Data[3], msg.Data[2] }, 0); console.GBICmd.StaticMv = BitConverter.ToUInt16(new byte[2] { msg.Data[5], msg.Data[4] }, 0); console.GBICmd.type = msg.Data[6]; console.GBICmd.trigger = msg.Data[7]; console.GBICmd.UpdateAll(); break; case 1: console.GBICmd.startMv = BitConverter.ToUInt16(new byte[2] { msg.Data[3], msg.Data[2] }, 0); console.GBICmd.stopMv = BitConverter.ToUInt16(new byte[2] { msg.Data[5], msg.Data[4] }, 0); console.GBICmd.time = BitConverter.ToUInt16(new byte[2] { msg.Data[7], msg.Data[6] }, 0); break; case 2: console.GBICmd.RampUpTime = msg.Data[2]; console.GBICmd.RampDownTime = msg.Data[3]; console.GBICmd.GBIScanStartDelay = BitConverter.ToUInt16(new byte[2] { msg.Data[5], msg.Data[4] }, 0); console.GBICmd.GBIDetReadyDelay = BitConverter.ToUInt16(new byte[2] { msg.Data[7], msg.Data[6] }, 0); break; case 3: console.GBICmd.StepAbsMv = BitConverter.ToUInt16(new byte[2] { msg.Data[3], msg.Data[2] }, 0); console.GBICmd.StepIncMv = BitConverter.ToUInt16(new byte[2] { msg.Data[5], msg.Data[4] }, 0); console.GBICmd.StepTime = BitConverter.ToUInt16(new byte[2] { msg.Data[7], msg.Data[6] }, 0); console.GBICmd.UpdateAll(); break; case 4: console.GBICmd.DACOffsetMv = BitConverter.ToUInt16(new byte[2] { msg.Data[3], msg.Data[2] }, 0); console.GBICmd.UpdateAll(); break; } } }