Implement Diagnostic LIN in C Environment

Updated Jul 24, 2018

Reported In

Software

  • Measurement Studio

Driver

  • NI-XNET

Programming Language

  • C

Issue Details

How can I implement diagnostics LIN in Measurements Studio or another C programming environment, using NI-XNET API for C?

 

Solution

In this document, you can find a simple pseudo-code of LIN diagnostic Frame Master request
This is not a complete working example but just a pseudo code to implement LIN Diagnostic Frames with main functions
Requirements: NI-XNET driver
Instructions:

  • Ensure that you have connected the selected LIN ports with a properly terminated cable

Guide lines for LIN Diagnostic implementation:

#include "nixnet.h" // Include NI-XNET library
// Set the interface to master mode and initialize a status variable
u32 l_IsMaster  = 1;
nxStatus_t l_Status = 0;

   // Create an XNET Input Frame In Stream session to read the incoming frames.
   l_Status = nxStatus_t nxCreateSession (
   const char * DatabaseName, //For read session use ":memory:"
   const char * ClusterName,
   const char * List,
   const char * Interface,
   u32 Mode, //nxMode_FrameInStream
   nxSessionRef_t * SessionRef);
   
   if (nxSuccess == l_Status)
   {
       printf("Input session created successfully.\n");
   }
   else
   {
       DisplayErrorAndExit(l_Status, "nxCreateSession");
   }
//Repeat this step to Create an XNET Output Session, using nxMode_FrameOutQueued mode and link to LDF database
if (nxSuccess == l_Status)
   {
      // In this example, the output session enables master mode
       if (0 != l_IsMaster)
       {

           // LIN Termination ON
          l_Status = nxSetProperty ( nxSessionRef_t SessionRef,
   u32 PropertyID,
   u32 PropertySize,
   void * PropertyValue);
if (nxSuccess != l_Status)
           {
               DisplayErrorAndExit(l_Status, "nxSetProperty");
           }

           // Set your Interface as Master
           l_Status = nxSetProperty ()
if (nxSuccess != l_Status)
           {
               DisplayErrorAndExit(l_Status, "nxSetProperty");
           }
// Main loop
// Writes communication states of an XNET session 
l_Status = nxWriteState(m_OutputSessionRef, nxState_LINDiagnosticScheduleChange,                sizeof(LINDiagnosticSchedule_SlaveResp), &LINDiagnosticSchedule_SlaveResp);            if (nxSuccess != l_Status)            
{                DisplayErrorAndExit(l_Status, "nxWriteState");            
}
l_Status = nxReadFrame(m_InputSessionRef, l_Buffer2, sizeof(l_Buffer2), nxTimeout_None, &l_NumBytesIn);                    
Sleep(50);
l_Status = nxWriteState(m_OutputSessionRef, nxState_LINDiagnosticScheduleChange,
               sizeof(LINDiagnosticSchedule_MasterReq), &LINDiagnosticSchedule_MasterReq);
           if (nxSuccess != l_Status)
           {
               DisplayErrorAndExit(l_Status, "nxWriteState");
           }
           
// Get a pointer to the first frame's memory
/* You call this macro as a function, as if it used the following prototype:
      nxFrameVar_t * nxFrameIterate(nxFrameVar_t * frameptr);
   The input parameter must be initialized to point to the header of a valid frame.
   The macro returns a pointer to the header of the next frame in the buffer.
   In other words, the macro will iterate from one variable-length frame to
   the next variable-length frame.
   */
// Get the actual size of the buffer used 
/* nxFrameVar_t * nxFrameIterate(nxFrameVar_t * frameptr);
   The input parameter must be initialized to point to the header of a valid frame.
   The macro returns a pointer to the header of the next frame in the buffer.
   In other words, the macro will iterate from one variable-length frame to
   the next variable-length frame.
   */
// Update the frame data with nxWriteState
// Waits for a certain condition to occur
nxStatus_t _NXFUNC nxWait (
   nxSessionRef_t SessionRef,
   u32 Condition,
   u32 ParamIn,
   f64 Timeout,
   u32 * ParamOut);
// Read the received frames into the buffer with nxReadFrame
// Print Out Final Array of Bytes
// Clear the XNET input session
l_Status = nxClear(m_InputSessionRef);
   if (nxSuccess == l_Status)
   {
      printf("Input session cleared successfully!\n");
   }
   else
   {
      DisplayErrorAndExit(l_Status, "nxClear");
   }
   // Clear the XNET output session with nxClear(m_OutputSessionRef);

Additional Information

If your DUT (Device Under Test) uses software UDS diagnostic services for reading and writing diagnostic information. The implementation is according with Unified Diagnostic Services - ISO 14229

WAS THIS ARTICLE HELPFUL?

Not Helpful