📄 xlcandemo.c
字号:
/*------------------------------------------------------------------------------
| File:
| xlCANdemo.C
| Project:
| Sample for XL - Driver Library
| Example application using 'vxlapi.dll'
|-------------------------------------------------------------------------------
| $Author: Marco $ $Locker: $ $Revision: 14 $
| $Header: /VCANDRV/XLAPI/samples/xlCANdemo/xlCANdemo.c 14 15.07.05 9:29 Marco $
|-------------------------------------------------------------------------------
| Copyright (c) 2004 by Vector Informatik GmbH. All rights reserved.
-----------------------------------------------------------------------------*/
#if defined(_Windows) || defined(_MSC_VER) || defined (__GNUC__)
#define STRICT
#include <windows.h>
#endif
#include <stdio.h>
#define UNUSED_PARAM(a) { a=a; }
#define RECEIVE_EVENT_SIZE 1 // DO NOT EDIT! Currently 1 is supported only
#include "vxlapi.h"
/////////////////////////////////////////////////////////////////////////////
// globals
char g_AppName[XL_MAX_LENGTH+1] = "xlCANdemo"; //!< Application name which is displayed in VHWconf
XLportHandle g_xlPortHandle = XL_INVALID_PORTHANDLE; //!< Global porthandle (we use only one!)
XLdriverConfig g_xlDrvConfig; //!< Contains the actual hardware configuration
XLaccess g_xlChannelMask = 0; //!< Global channelmask (includes all founded channels)
XLaccess g_xlPermissionMask = 0; //!< Global permissionmask (includes all founded channels)
unsigned int g_BaudRate = 500000; //!< Default baudrate
int g_silent = 0; //!< flag to visualize the message events (on/off)
unsigned int g_TimerRate = 0; //!< Global timerrate (to toggel)
// tread variables
XLhandle g_hMsgEvent; //!< notification handle for the receive queue
HANDLE g_hRXThread; //!< thread handle (RX)
HANDLE g_hTXThread; //!< thread handle (TX)
int g_RXThreadRun; //!< flag to start/stop the RX thread
int g_TXThreadRun; //!< flag to start/stop the TX thread (for the transmission burst)
////////////////////////////////////////////////////////////////////////////
// functions (Threads)
DWORD WINAPI RxThread( PVOID par );
DWORD WINAPI TxThread( LPVOID par );
////////////////////////////////////////////////////////////////////////////
// functions (prototypes)
void demoHelp(void);
void demoPrintConfig(void);
XLstatus demoCreateRxThread(void);
////////////////////////////////////////////////////////////////////////////
//! demoHelp()
//! shows the program functionality
//!
////////////////////////////////////////////////////////////////////////////
void demoHelp(void) {
printf("\n----------------------------------------------------------\n");
printf("- xlCANdemo - HELP -\n");
printf("----------------------------------------------------------\n");
printf("- Keyboard commands: -\n");
printf("- 't' Transmit a message -\n");
printf("- 'b' Transmit a message burst (toggle) -\n");
printf("- 'm' Transmit a remote message -\n");
printf("- 'g' Request chipstate -\n");
printf("- 's' Start/Stop -\n");
printf("- 'r' Reset clock -\n");
printf("- '+' Select channel (up) -\n");
printf("- '-' Select channel (down) -\n");
printf("- 'i' Select transmit Id (up) -\n");
printf("- 'I' Select transmit Id (down) -\n");
printf("- 'x' Toggle extended/standard Id -\n");
printf("- 'o' Toggle output mode -\n");
printf("- 'a' Toggle timer -\n");
printf("- 'v' Toggle logging to screen -\n");
printf("- 'p' Show hardware configuration -\n");
printf("- 'h' Help -\n");
printf("- 'ESC' Exit -\n");
printf("----------------------------------------------------------\n");
printf("- 'PH'->PortHandle; 'CM'->ChannelMask; 'PM'->Permission -\n");
printf("----------------------------------------------------------\n\n");
}
////////////////////////////////////////////////////////////////////////////
//! demoPrintConfig()
//! shows the actual hardware configuration
//!
////////////////////////////////////////////////////////////////////////////
void demoPrintConfig(void) {
unsigned int i;
char str[XL_MAX_LENGTH + 1]="";
printf("----------------------------------------------------------\n");
printf("- %02d channels Hardware Configuration -\n", g_xlDrvConfig.channelCount);
printf("----------------------------------------------------------\n");
for (i=0; i < g_xlDrvConfig.channelCount; i++) {
printf("- Ch.: %02d, CM:0x%3I64x,",
g_xlDrvConfig.channel[i].channelIndex, g_xlDrvConfig.channel[i].channelMask);
printf(" %20s ", g_xlDrvConfig.channel[i].name);
if (g_xlDrvConfig.channel[i].transceiverType != XL_TRANSCEIVER_TYPE_NONE) {
strncpy( str, g_xlDrvConfig.channel[i].transceiverName, 13 );
printf(" %3s -\n", str);
}
else printf(" no Cab! -\n", str);
}
//0x%I64x
printf("----------------------------------------------------------\n\n");
}
////////////////////////////////////////////////////////////////////////////
//! demoTransmit
//! transmit a CAN message (depending on an ID, channel)
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmit(unsigned int txID, XLaccess xlChanMaskTx)
{
static XLevent xlEvent;
XLstatus xlStatus;
unsigned int messageCount = 1;
memset(&xlEvent, 0, sizeof(xlEvent));
xlEvent.tag = XL_TRANSMIT_MSG;
xlEvent.tagData.msg.id = txID;
xlEvent.tagData.msg.dlc = 8;
xlEvent.tagData.msg.flags = 0;
++xlEvent.tagData.msg.data[0];
xlEvent.tagData.msg.data[1] = 2;
xlEvent.tagData.msg.data[2] = 3;
xlEvent.tagData.msg.data[3] = 4;
xlEvent.tagData.msg.data[4] = 5;
xlEvent.tagData.msg.data[5] = 6;
xlEvent.tagData.msg.data[6] = 7;
xlEvent.tagData.msg.data[7] = 8;
xlStatus = xlCanTransmit(g_xlPortHandle, xlChanMaskTx, &messageCount, &xlEvent);
printf("- Transmit : CM(0x%I64x), %s\n", xlChanMaskTx, xlGetErrorString(xlStatus));
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
//! demoTransmitBurst
//! transmit a message burst (also depending on an IC, channel).
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmitBurst(unsigned int txID)
{
DWORD ThreadId=0;
static unsigned int TXID;
TXID = txID;
if (!g_TXThreadRun) {
printf("- print txID: %d\n", txID);
g_hTXThread = CreateThread(0, 0x1000, TxThread, (LPVOID)&TXID, 0, &ThreadId);
}
else {
g_TXThreadRun = 0;
Sleep(10);
WaitForSingleObject(g_hTXThread, 10);
}
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
//! demoTransmitRemote
//! transmit a remote frame
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoTransmitRemote(unsigned int txID, XLaccess xlChanMaskTx)
{
XLstatus xlStatus;
XLevent xlEvent;
unsigned int messageCount = 1;
xlEvent.tag = XL_TRANSMIT_MSG;
xlEvent.tagData.msg.id = txID;
xlEvent.tagData.msg.flags = XL_CAN_MSG_FLAG_REMOTE_FRAME;
xlEvent.tagData.msg.dlc = 8;
xlStatus = xlCanTransmit(g_xlPortHandle, xlChanMaskTx, &messageCount, &xlEvent);
printf("- Transmit REMOTE : CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
//! demoStartStop
//! toggle the channel activate/deactivate
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoStartStop(int activated)
{
XLstatus xlStatus;
if (activated) {
xlStatus = xlActivateChannel(g_xlPortHandle, g_xlChannelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK);
printf("- ActivateChannel : CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
}
else {
xlStatus = xlDeactivateChannel(g_xlPortHandle, g_xlChannelMask);
printf("- DeativateChannel: CM(0x%I64x), %s\n", g_xlChannelMask, xlGetErrorString(xlStatus));
}
return XL_SUCCESS;
}
////////////////////////////////////////////////////////////////////////////
//! demoSetOutput
//! toggle NORMAL/SILENT mode of a CAN channel
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoSetOutput(int outputMode, XLaccess xlChanMaskTx) {
XLstatus xlStatus;
char *sMode = "NORMAL";
switch(outputMode) {
case XL_OUTPUT_MODE_NORMAL:
sMode = "NORMAL";
break;
case XL_OUTPUT_MODE_SILENT:
sMode = "SILENT";
break;
case XL_OUTPUT_MODE_TX_OFF:
sMode = "SILENT-TXOFF";
break;
}
// to get an effect we deactivate the channel first.
xlStatus = xlDeactivateChannel(g_xlPortHandle, g_xlChannelMask);
xlStatus = xlCanSetChannelOutput(g_xlPortHandle, xlChanMaskTx, outputMode);
printf("- SetChannelOutput: CM(0x%I64x), %s, %s, %d\n", xlChanMaskTx, sMode, xlGetErrorString(xlStatus), outputMode);
// and activate the channel again.
xlStatus = xlActivateChannel(g_xlPortHandle, g_xlChannelMask, XL_BUS_TYPE_CAN, XL_ACTIVATE_RESET_CLOCK);
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
//! demoCreateRxThread
//! set the notification and creates the thread.
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoCreateRxThread(void) {
XLstatus xlStatus = XL_ERROR;
DWORD ThreadId=0;
if (g_xlPortHandle!= XL_INVALID_PORTHANDLE) {
// Send a event for each Msg!!!
xlStatus = xlSetNotification (g_xlPortHandle, &g_hMsgEvent, 1);
g_hRXThread = CreateThread(0, 0x1000, RxThread, (LPVOID) 0, 0, &ThreadId);
}
return xlStatus;
}
////////////////////////////////////////////////////////////////////////////
//! demoInitDriver
//! initializes the driver with one port and all founded channels which
//! have a connected CAN cab/piggy.
//!
////////////////////////////////////////////////////////////////////////////
XLstatus demoInitDriver(void) {
XLstatus xlStatus;
unsigned int i;
// ------------------------------------
// open the driver
// ------------------------------------
xlStatus = xlOpenDriver ();
// ------------------------------------
// get/print the hardware configuration
// ------------------------------------
if(XL_SUCCESS == xlStatus) {
xlStatus = xlGetDriverConfig(&g_xlDrvConfig);
}
if(XL_SUCCESS == xlStatus) {
demoPrintConfig();
printf("Usage: xlCANdemo <BaudRate> <ApplicationName> <Identifier>\n\n");
// ------------------------------------
// select the wanted channels
// ------------------------------------
g_xlChannelMask = 0;
for (i=0; i < g_xlDrvConfig.channelCount; i++) {
// we take all hardware we found and
// check that we have only CAN cabs/piggy's
if (g_xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN) {
g_xlChannelMask |= g_xlDrvConfig.channel[i].channelMask;
}
}
if (!g_xlChannelMask) {
printf("ERROR: no available channels found! (e.g. no CANcabs...)\n\n");
xlStatus = XL_ERROR;
}
}
g_xlPermissionMask = g_xlChannelMask;
// ------------------------------------
// open ONE port including all channels
// ------------------------------------
if(XL_SUCCESS == xlStatus) {
xlStatus = xlOpenPort(&g_xlPortHandle, g_AppName, g_xlChannelMask, &g_xlPermissionMask, 256, XL_INTERFACE_VERSION, XL_BUS_TYPE_CAN);
printf("- OpenPort : CM=0x%I64x, PH=0x%02X, PM=0x%I64x, %s\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -