⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xllinfunctions.cpp

📁 汽车领lin总线通讯程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
| File        : LINFunctions.cpp
| Project     : Vector LIN Example - manage the LIN access
|
| Description : 
|-----------------------------------------------------------------------------
| $Author: J鰎g $    $Locker: $   $Revision: 16 $
| $Header: /VCANDRV/XLAPI/samples/xlLinExample/xlLINFunctions.cpp 16    14.11.05 10:54 J鰎g $
|-----------------------------------------------------------------------------
| Copyright (c) 2004 by Vector Informatik GmbH.  All rights reserved.
|---------------------------------------------------------------------------*/

#include "stdafx.h"
#include "xlLINExample.h"
#include "xlLINFunctions.h"
#include "debug.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// ---------------------------------------------------
// globals
BOOL      g_bThreadRun;
TStruct   g_th;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLINFunctions::CLINFunctions() {
  m_xlPortHandle = XL_INVALID_PORTHANDLE;
  m_xlChannelMask[MASTER] = m_xlChannelMask[SLAVE] = 0;
}

CLINFunctions::~CLINFunctions() 
{
}

////////////////////////////////////////////////////////////////////////////

//! LINGetDevice

//! readout the registry to get the hardware information. If there is no 
//! application which is named "xlLINExample" a new one is generated. 
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::LINGetDevice()
{
  XLstatus         xlStatus = XL_ERROR;
  char            tmp[100];

  xlStatus = xlOpenDriver();
  sprintf(tmp, "xlOpenDriver, stat: %d", xlStatus);
  DEBUG(DEBUG_ADV, tmp);
  if (xlStatus != XL_SUCCESS) return xlStatus;

  xlStatus = linGetChannelMask(MASTER);
  xlStatus = linGetChannelMask(SLAVE);

  if ( (m_xlChannelMask[MASTER] == 0) || (m_xlChannelMask[SLAVE] == 0) ) return XL_ERROR;
  
  return xlStatus;
}

////////////////////////////////////////////////////////////////////////////

//! LINInit

//! LINExample use ONE port for master and slave which is opened. Also a 
//! thread for all incoming messages is created.
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::LINInit(int linID)
{
  XLstatus         xlStatus = XL_ERROR;
  XLaccess         m_xlChannelMask_both;
  XLaccess         xlPermissionMask;
  char             tmp[100];

  // ---------------------------------------
  // Open ONE port for both channels master+slave
  // ---------------------------------------

  // calculate the channelMask for both channel 
  m_xlChannelMask_both = m_xlChannelMask[MASTER] + m_xlChannelMask[SLAVE];
  xlPermissionMask = m_xlChannelMask_both;

  xlStatus = xlOpenPort(&m_xlPortHandle, "LIN Example", m_xlChannelMask_both, &xlPermissionMask, 256, XL_INTERFACE_VERSION, XL_BUS_TYPE_LIN); 
  sprintf(tmp, "xlOpenPort: PortHandle: %d; Permissionmask: 0x%I64x; Status: %d", m_xlPortHandle, xlPermissionMask, xlStatus);
  DEBUG(DEBUG_ADV, tmp);

  m_pStatusBox->InsertString(-1,"xlOpenPort for both");
  
  if (m_xlPortHandle == XL_INVALID_PORTHANDLE) return XL_ERROR;
  if (xlStatus == XL_ERR_INVALID_ACCESS) return xlStatus;


  // ---------------------------------------
  // Create ONE thread for both channels
  // ---------------------------------------

  linCreateRxThread();

  // ---------------------------------------
  // Init each channel (master+slave)
  // ---------------------------------------

  xlStatus = linInitMaster();
  if (xlStatus) {
    m_pStatusBox->InsertString(-1,"Init Master failed!");
    return xlStatus;
  }
  m_pStatusBox->InsertString(-1,"Init Master");

  xlStatus = linInitSlave(linID);
  if (xlStatus) {
    m_pStatusBox->InsertString(-1,"Init Slave failed!");
    return xlStatus;
  }
  m_pStatusBox->InsertString(-1,"Init Slave");
 
  return xlStatus;
}

///////////////////////////////////////////////////////////////////////////

//! LINClose()

//! Close the port
//!
////////////////////////////////////////////////////////////////////////////

XLstatus CLINFunctions::LINClose() {
  XLstatus         xlStatus = XL_SUCCESS;
  XLaccess         xlChannelMask_both = m_xlChannelMask[MASTER] | m_xlChannelMask[SLAVE];
  char            tmp[100];
 
  g_bThreadRun = FALSE;

  // Wait until the thread is done...
  Sleep(100);

  if(XL_INVALID_PORTHANDLE == m_xlPortHandle) {
    return(xlStatus);
  }
 
  if(xlChannelMask_both) {
    xlStatus = xlDeactivateChannel(m_xlPortHandle, xlChannelMask_both);
    sprintf(tmp, "xlDeactivateChannel, status: %d",  xlStatus);
    DEBUG(DEBUG_ADV, tmp);
    if(xlStatus) return xlStatus;
  }

  xlStatus = xlClosePort(m_xlPortHandle);
  sprintf(tmp, "xlClosePort, status: %d",  xlStatus);
  DEBUG(DEBUG_ADV, tmp);
  if(xlStatus) return xlStatus;

  m_xlPortHandle = XL_INVALID_PORTHANDLE;

  xlStatus = xlCloseDriver();
  sprintf(tmp, "xlCloseDriver, status: %d",  xlStatus);
  DEBUG(DEBUG_ADV, tmp);
  if(xlStatus) return xlStatus;

  m_pStatusBox->InsertString(-1,"Close All");

  return xlStatus;
}

///////////////////////////////////////////////////////////////////////////

//! LINSendMasterReq()

//! Sends a master request to the LIN bus.
//!
////////////////////////////////////////////////////////////////////////////

XLstatus CLINFunctions::LINSendMasterReq(BYTE data, int linID)
{
  XLstatus        xlStatus = XL_ERROR;
  char            tmp[100];
  
  // send the master request
  xlStatus = xlLinSendRequest(m_xlPortHandle, m_xlChannelMask[MASTER], linID, 0);
  sprintf(tmp, "SendRq CM: '%I64u', status: %d", m_xlChannelMask[MASTER], xlStatus);
  DEBUG(DEBUG_ADV, tmp);

  m_pStatusBox->InsertString(-1,"Master Request");

  // setup the slave
  xlStatus = linSetSlave(data);
  
  return xlStatus;
}

////////////////////////////////////////////////////////////////////////////

//! linGetChannelMask

//! parse the registry to get the channelmask
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::linGetChannelMask(int LINType)
{
  XLstatus        xlStatus = XL_ERROR;
  char            tmp[100];

  // default values
  unsigned int  hwType    = XL_HWTYPE_CANCARDXL;
  unsigned int  hwIndex   = 0;
  unsigned int  hwChannel = LINType-1;   // because 0->CH1 should be MASTER, 1->CH2 should be SLAVE
  unsigned int  busType   = XL_BUS_TYPE_LIN;   
  unsigned int  i; 
 
  XLdriverConfig  xlDrvConfig;

  xlStatus = xlGetApplConfig("xlLINExample", hwChannel, &hwType, &hwIndex, &hwChannel, busType);              
  
  // Set the params into registry (default values...!)
  if (xlStatus) {
    DEBUG(DEBUG_ADV,"set in registry");

    //check for hardware:
    xlStatus = xlGetDriverConfig(&xlDrvConfig);

    for (i=0; i<xlDrvConfig.channelCount; i++) {

      // check PC for hardware with LINCabs or LINPiggy's 
      if ( xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_LIN) {
        hwType = xlDrvConfig.channel[i].hwType;
        sprintf (tmp, "Found LIN hWType: %d;", hwType);
        DEBUG(DEBUG_ADV,tmp);

         xlStatus = xlSetApplConfig( // Registration of Application with default settings
        "xlLINExample",          // Application Name
        LINType-1,              // Application channel 0 or 1
        hwType,                 // hwType  (CANcardXL...)    
        hwIndex,                // Index of hardware (slot) (0,1,...)
        hwChannel,              // Index of channel (connector) (0,1,...)
        busType);               // the application is for LIN.
      }
    }

  }
  else DEBUG(DEBUG_ADV,"found in registry");
  
  // check for LINPiggy or LINCabs
  xlStatus = xlGetDriverConfig(&xlDrvConfig);

  for (i=0; i<xlDrvConfig.channelCount; i++) {

    // check for LINCabs or LINPiggy's 
    if ( xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_LIN) {

      // and check the right hardwaretype
      if (xlDrvConfig.channel[i].hwType==hwType) {
        m_xlChannelMask[LINType] = xlGetChannelMask(hwType, hwIndex, hwChannel);
      }
    }
  }
  sprintf (tmp, "Init LIN hWType: %d; hWIndex: %d; hwChannel: %d, channelMask: 0x%I64x for %d", hwType, hwIndex, hwChannel, m_xlChannelMask[LINType], LINType);
  DEBUG(DEBUG_ADV,tmp);

  return xlStatus;
}

////////////////////////////////////////////////////////////////////////////

//! linCreateRxThread

//! set the notification and creates the thread.
//!
////////////////////////////////////////////////////////////////////////////

XLstatus CLINFunctions::linCreateRxThread()
{
  
  XLstatus      xlStatus;
  DWORD         ThreadId=0;
  char          tmp[100];
 
  if (m_xlPortHandle!= XL_INVALID_PORTHANDLE) {

    sprintf(tmp, "Found OpenPort: %d", m_xlPortHandle);
    DEBUG(DEBUG_ADV, tmp);

    // Send a event for each Msg!!!
    xlStatus = xlSetNotification (m_xlPortHandle, &m_hMsgEvent, 1);
    sprintf(tmp, "SetNotification '%d', xlStatus: %d", m_hMsgEvent, xlStatus);
    DEBUG(DEBUG_ADV, tmp);

    // for the RxThread

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -