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

📄 xllinfunctions.cpp

📁 汽车领lin总线通讯程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    g_th.xlPortHandle = m_xlPortHandle;
    g_th.hMsgEvent    = m_hMsgEvent; 
    g_th.pListRX      = m_pRXBox;
    g_th.pStatusBox   = m_pStatusBox;
  
    m_hThread = CreateThread(0, 0x1000, RxThread, (LPVOID) &g_th, 0, &ThreadId);
    sprintf(tmp, "CreateThread %d", m_hThread);
    DEBUG(DEBUG_ADV, tmp);
    
  }
  return xlStatus;
}


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

//! linInitMaster

//! initialize the LIN master, set the master DLC's, opens the 
//! message filter and activate the LIN channel (-> bus on).
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::linInitMaster()
{
  XLstatus        xlStatus = XL_ERROR;
  char            tmp[100];
 
  // ---------------------------------------
  // Setup the channel as a MASTER
  // ---------------------------------------

  XLlinStatPar     xlStatPar;

  xlStatPar.LINMode    = XL_LIN_MASTER; 
  xlStatPar.baudrate   = 9600;               // set the baudrate to 9k6
  xlStatPar.LINVersion = XL_LIN_VERSION_1_3;    // use LIN 1.3

  xlStatus = xlLinSetChannelParams(m_xlPortHandle, m_xlChannelMask[MASTER], xlStatPar);

  sprintf(tmp, "Init Master PH: '%d', CM: '0x%I64x', status: %d", m_xlPortHandle, m_xlChannelMask[MASTER], xlStatus );
  DEBUG(DEBUG_ADV, tmp);

  // ---------------------------------------
  // Setup the Master DLC's
  // ---------------------------------------

  unsigned char DLC[64];

  // set the DLC for all ID's to 8
  for (int i=0;i<64;i++) DLC[i] = 8;

  xlStatus = xlLinSetDLC(m_xlPortHandle, m_xlChannelMask[MASTER], DLC);

  // ---------------------------------------
  // Activate the Master Channel
  // ---------------------------------------

  xlStatus = xlActivateChannel(m_xlPortHandle, m_xlChannelMask[MASTER], XL_BUS_TYPE_LIN, XL_ACTIVATE_RESET_CLOCK);
  sprintf(tmp, "Activate Channel, CM: '0x%I64x', status: %d", m_xlChannelMask[MASTER], xlStatus);
  DEBUG(DEBUG_ADV, tmp);

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

  return xlStatus;
}


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

//! linInitSlave

//! initialize the LIN slave, define the slave (id, dlc, data), opens the 
//! message filter and activate the LIN channel (-> bus on).
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::linInitSlave(int linID)
{
  XLstatus        xlStatus = XL_ERROR;
  char            tmp[100];
 
  // ---------------------------------------
  // Setup the channel as a SLAVE
  // ---------------------------------------

  XLlinStatPar     xlStatPar;

  xlStatPar.LINMode    = XL_LIN_SLAVE;
  xlStatPar.baudrate   = 9600;                 // set the baudrate to 9k6
  xlStatPar.LINVersion = XL_LIN_VERSION_1_3;   // use LIN 1.3

  xlStatus = xlLinSetChannelParams(m_xlPortHandle, m_xlChannelMask[SLAVE], xlStatPar);

  sprintf(tmp, "Init Slave PH: '%d', CM: '0x%I64x', status: %d", m_xlPortHandle, m_xlChannelMask[SLAVE], xlStatus );
  DEBUG(DEBUG_ADV, tmp);

  // ---------------------------------------
  // Setup the SLAVE 
  // ---------------------------------------

  unsigned char            data[8];
  unsigned char            id  = linID;
  unsigned char            dlc = 8;

  data[0] = 0x00;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;

  xlStatus = xlLinSetSlave(m_xlPortHandle, m_xlChannelMask[SLAVE], id, data, dlc, XL_LIN_CALC_CHECKSUM);
  sprintf(tmp, "Set Slave ID CM: '0x%I64x', status: %d", m_xlChannelMask[SLAVE], xlStatus);
  DEBUG(DEBUG_ADV, tmp);

  // ---------------------------------------
  // Activate the Slave Channel
  // ---------------------------------------

  xlStatus = xlActivateChannel(m_xlPortHandle, m_xlChannelMask[SLAVE], XL_BUS_TYPE_LIN, XL_ACTIVATE_RESET_CLOCK);
  sprintf(tmp, "Activate Channel CM: '0x%I64x', status: %d", m_xlChannelMask[SLAVE], xlStatus);
  DEBUG(DEBUG_ADV, tmp);

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

  return xlStatus;
}

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

//! linSetSlave

//! change the slave
//!
//////////////////////////////////////////////////////////////////////////// 

XLstatus CLINFunctions::linSetSlave(byte databyte)
{
  XLstatus        xlStatus = XL_ERROR;
  char            tmp[100];

  unsigned char            data[8];
  unsigned char            id  = 0x04;
  unsigned char            dlc = 8;

  data[0] = databyte;
  data[1] = 0x00;
  data[2] = 0x00;
  data[3] = 0x00;
  data[4] = 0x00;
  data[5] = 0x00;
  data[6] = 0x00;
  data[7] = 0x00;

  xlStatus = xlLinSetSlave(m_xlPortHandle, m_xlChannelMask[SLAVE], id, data, dlc, XL_LIN_CALC_CHECKSUM);
  sprintf(tmp, "Set Slave ID CM: '0x%I64x', status: %d", m_xlChannelMask[SLAVE], xlStatus);
  DEBUG(DEBUG_ADV, tmp);

  return xlStatus;
}

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

//! RxThread

//! thread to readout the message queue and parse the incoming messages
//!
////////////////////////////////////////////////////////////////////////////

DWORD WINAPI RxThread(LPVOID par) 
{

  XLstatus        xlStatus;
  
  //char            tmp[100];
  unsigned int    msgsrx = RECEIVE_EVENT_SIZE;
  XLevent         xlEvent; 
  char            tmp[100];
  CString         str;
  
  g_bThreadRun = TRUE;

  TStruct *g_th;

  g_th = (TStruct*) par;  

  sprintf(tmp, "thread: SetNotification '%d'", g_th->hMsgEvent);
  DEBUG(DEBUG_ADV, tmp);

  while (g_bThreadRun) { 
   
    WaitForSingleObject(g_th->hMsgEvent,10);

    xlStatus = XL_SUCCESS;
    
   
    while (!xlStatus) {
      
      
      msgsrx = RECEIVE_EVENT_SIZE;
      xlStatus = xlReceive(g_th->xlPortHandle, &msgsrx, &xlEvent);
      
      if ( xlStatus!=XL_ERR_QUEUE_IS_EMPTY ) {
          
        //sprintf(tmp, "thread: ReceiveEx tag: '%d'", vEvent2.tag);
        //DEBUG(DEBUG_ADV, tmp);

        switch (xlEvent.tag) {

        // CAN events
        case XL_SYNC_PULSE:
            sprintf(tmp, "SYNC_PULSE: on Ch: '%d'", xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

        case XL_TRANSCEIVER:
            sprintf(tmp, "TRANSCEIVER: on Ch: '%d'", xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

        // LIN events
        
          case XL_LIN_NOANS:
            sprintf(tmp, "LIN NOANS ID: '0x%x' on Ch: '%d', time: %I64u", xlEvent.tagData.linMsgApi.linNoAns.id, xlEvent.chanIndex, xlEvent.timeStamp);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

          case XL_LIN_MSG: {
            CString         str1, sData;
            str = "RX: ";
            if (xlEvent.tagData.linMsgApi.linMsg.flags & XL_LIN_MSGFLAG_TX) str = "TX: ";

            str1="";
            for (int i=0; i<xlEvent.tagData.linMsgApi.linMsg.dlc;i++) {
              str1.Format(_T("%02x"),xlEvent.tagData.linMsgApi.linMsg.data[i]);
              sData = sData + str1;
            }
            
            sprintf(tmp, "ID: 0x%02x, dlc: '%d', Data: 0x%s, time: %I64u, Ch: '%d'", xlEvent.tagData.linMsgApi.linMsg.id, xlEvent.tagData.linMsgApi.linMsg.dlc, sData, xlEvent.timeStamp, xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1, str + tmp);
            break;
          }

          case XL_LIN_SLEEP:
            sprintf(tmp, "LIN SLEEP flag: 0x%x, time: %I64u, Ch: '%d'", xlEvent.tagData.linMsgApi.linSleep.flag, xlEvent.timeStamp, xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

          case XL_LIN_ERRMSG:
            sprintf(tmp, "LIN ERROR, Ch: '%d'", xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

          case XL_LIN_SYNCERR:
            sprintf(tmp, "LIN SYNCERR on Ch: '%d'", xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;

          case XL_LIN_WAKEUP:
            sprintf(tmp, "LIN WAKEUP flags: 0x%x on Ch: '%d'", xlEvent.tagData.linMsgApi.linWakeUp.flag, xlEvent.chanIndex);
            DEBUG(DEBUG_ADV, tmp); 
            g_th->pListRX->InsertString(-1,tmp);
            break;
         }
        ResetEvent(g_th->hMsgEvent);

        //int nCount = pmyListBox->GetCount();
        //if (nCount > 0)
        g_th->pListRX->SetCurSel(g_th->pListRX->GetCount()-1);
        g_th->pStatusBox->SetCurSel(g_th->pStatusBox->GetCount()-1);

      }  
    }
          
  }
  return NO_ERROR; 
}


⌨️ 快捷键说明

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