Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
xlLINFunctions.cpp
Package: xlLinExample.rar [view]
Upload User: dfymccf
Upload Date: 2008-03-14
Package Size: 18k
Code Size: 18k
Category:
SCM
Development Platform:
Visual C++
- /*----------------------------------------------------------------------------
- | 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
- 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;
- }