MessageStatus.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /// <summary>
  7. /// Inclusion of PEAK PCAN-Basic namespace
  8. /// </summary>
  9. using TPCANHandle = System.UInt16;
  10. using TPCANBitrateFD = System.String;
  11. using TPCANTimestampFD = System.UInt64;
  12. namespace SIASUN.Autopilot.Device
  13. {
  14. /// <summary>
  15. /// Message Status structure used to show CAN Messages
  16. /// in a ListView
  17. /// </summary>
  18. public class MessageStatus
  19. {
  20. private TPCANMsgFD m_Msg;
  21. private TPCANTimestampFD m_TimeStamp = 0;
  22. private TPCANTimestampFD m_oldTimeStamp = 0;
  23. public MessageStatus(TPCANMsgFD canMsg, TPCANTimestampFD canTimestamp)
  24. {
  25. m_Msg = canMsg;
  26. m_TimeStamp = canTimestamp;
  27. m_oldTimeStamp = canTimestamp;
  28. }
  29. public void Update(TPCANMsgFD canMsg, TPCANTimestampFD canTimestamp)
  30. {
  31. m_Msg = canMsg;
  32. m_TimeStamp = canTimestamp;
  33. m_oldTimeStamp = canTimestamp;
  34. }
  35. public TPCANMsgFD CANMsg
  36. {
  37. get { return m_Msg; }
  38. }
  39. public TPCANTimestampFD Timestamp
  40. {
  41. get { return m_TimeStamp; }
  42. }
  43. public string TypeString
  44. {
  45. get { return GetMsgTypeString(); }
  46. }
  47. public string IdString
  48. {
  49. get { return GetIdString(); }
  50. }
  51. public string DataString
  52. {
  53. get { return GetDataString(); }
  54. }
  55. public string TimeString
  56. {
  57. get { return GetTimeString(); }
  58. }
  59. private string GetTimeString()
  60. {
  61. double fTime;
  62. fTime = (m_TimeStamp / 1000.0);
  63. //if (m_bShowPeriod)
  64. // fTime -= (m_oldTimeStamp / 1000.0);
  65. return fTime.ToString("F1");
  66. }
  67. private string GetDataString()
  68. {
  69. string strTemp;
  70. strTemp = "";
  71. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_RTR) == TPCANMessageType.PCAN_MESSAGE_RTR)
  72. return "Remote Request";
  73. else
  74. for (int i = 0; i < GetLengthFromDLC(m_Msg.DLC, (m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_FD) == 0); i++)
  75. strTemp += string.Format("{0:X2} ", m_Msg.DATA[i]);
  76. return strTemp;
  77. }
  78. private string GetIdString()
  79. {
  80. // We format the ID of the message and show it
  81. //
  82. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_EXTENDED) == TPCANMessageType.PCAN_MESSAGE_EXTENDED)
  83. return string.Format("{0:X8}", m_Msg.ID);
  84. else
  85. return string.Format("{0:X3}", m_Msg.ID);
  86. }
  87. private string GetMsgTypeString()
  88. {
  89. string strTemp;
  90. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_STATUS) == TPCANMessageType.PCAN_MESSAGE_STATUS)
  91. return "STATUS";
  92. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_ERRFRAME) == TPCANMessageType.PCAN_MESSAGE_ERRFRAME)
  93. return "ERROR";
  94. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_EXTENDED) == TPCANMessageType.PCAN_MESSAGE_EXTENDED)
  95. strTemp = "EXT";
  96. else
  97. strTemp = "STD";
  98. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_RTR) == TPCANMessageType.PCAN_MESSAGE_RTR)
  99. strTemp += "/RTR";
  100. else
  101. if ((int)m_Msg.MSGTYPE > (int)TPCANMessageType.PCAN_MESSAGE_EXTENDED)
  102. {
  103. strTemp += " [ ";
  104. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_FD) == TPCANMessageType.PCAN_MESSAGE_FD)
  105. strTemp += " FD";
  106. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_BRS) == TPCANMessageType.PCAN_MESSAGE_BRS)
  107. strTemp += " BRS";
  108. if ((m_Msg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_ESI) == TPCANMessageType.PCAN_MESSAGE_ESI)
  109. strTemp += " ESI";
  110. strTemp += " ]";
  111. }
  112. return strTemp;
  113. }
  114. #region Help functions
  115. /// <summary>
  116. /// Convert a CAN DLC value into the actual data length of the CAN/CAN-FD frame.
  117. /// </summary>
  118. /// <param name="dlc">A value between 0 and 15 (CAN and FD DLC range)</param>
  119. /// <param name="isSTD">A value indicating if the msg is a standard CAN (FD Flag not checked)</param>
  120. /// <returns>The length represented by the DLC</returns>
  121. private int GetLengthFromDLC(int dlc, bool isSTD)
  122. {
  123. if (dlc <= 8)
  124. return dlc;
  125. if (isSTD)
  126. return 8;
  127. switch (dlc)
  128. {
  129. case 9: return 12;
  130. case 10: return 16;
  131. case 11: return 20;
  132. case 12: return 24;
  133. case 13: return 32;
  134. case 14: return 48;
  135. case 15: return 64;
  136. default: return dlc;
  137. }
  138. }
  139. #endregion help functions
  140. }
  141. }