📄 cpuser_test.c.svn-base
字号:
//****************************************************************************//
// File: cpuser_test.c //
// Description: Test CANpie user functions //
// Author: Uwe Koppe //
// e-mail: koppe@microcontrol.net //
// //
//============================================================================//
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU Lesser General Public License as published //
// by the Free Software Foundation; either version 2.1 of the License, or //
// (at your option) any later version. //
//============================================================================//
// //
// Date History //
// ---------- -------------------------------------------------------------- //
// 24.06.2005 Initial version //
// //
//****************************************************************************//
//------------------------------------------------------------------------------
// CVS version information:
// $Id: cpuser_test.c,v 1.1 2005/07/13 09:42:54 microcontrol Exp $
//------------------------------------------------------------------------------
/*----------------------------------------------------------------------------*\
** Include files **
** **
\*----------------------------------------------------------------------------*/
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <can/cpuser.h>
/*----------------------------------------------------------------------------*\
** Definitions **
** **
\*----------------------------------------------------------------------------*/
int runProg = 1;
//----------------------------------------------------------------------
// quit()
// exit this program gracefully
//----------------------------------------------------------------------
void quit(int signal)
{
runProg = 0;
}
//----------------------------------------------------------------------------//
// can_print_frame() //
// //
//----------------------------------------------------------------------------//
void can_print_frame(_TsCpCanMsg * ptsFrameV, char * szBufferV)
{
int slPosT;
uint8_t ubDataCntT;
uint8_t ubDlcT;
//----------------------------------------------------------------
// Format is:
// ID(e): ----- DLC: - Data: -- .. -- (data frame, Ext. ID)
// ID(s): --- DLC: - Data: -- .. -- (data frame, Std. ID)
// ID(s): --- DLC: - RTR (remote frame, Std. ID)
// DLC is set to x if value > 8
//
//----------------------------------------------------------------
// check format of the identifier (Standard / Extended)
//
if(ptsFrameV->ubMsgCtrl & 0x01)
{
slPosT = sprintf( &szBufferV[0], "ID(e): 0x%08X ", ptsFrameV->tuMsgId.ulExt);
}
else
{
slPosT = sprintf( &szBufferV[0], "ID(s): 0x%03X ", ptsFrameV->tuMsgId.uwStd);
}
//----------------------------------------------------------------
// print DLC
//
ubDlcT = ptsFrameV->ubMsgDLC;
if(ubDlcT > 8)
{
slPosT += sprintf( &szBufferV[slPosT], "DLC: x ");
ubDlcT = 8; // limit to highest possible value
}
else
{
slPosT += sprintf( &szBufferV[slPosT], "DLC: %d ", ubDlcT);
}
//----------------------------------------------------------------
// check for remote frame
//
if(ptsFrameV->ubMsgCtrl & 0x02)
{
sprintf( &szBufferV[slPosT], "RTR");
}
else
{
slPosT += sprintf( &szBufferV[slPosT], "Data: ");
for(ubDataCntT = 0; ubDataCntT < ubDlcT; ubDataCntT++)
{
slPosT += sprintf( &szBufferV[slPosT], "%02X ", ptsFrameV->tuMsgData.aubByte[ubDataCntT]);
}
}
}
//----------------------------------------------------------------------------//
// main() //
// //
//----------------------------------------------------------------------------//
int main(int argc, char **argv)
{
_TsCpCanMsg atsRcvFrame[4];
_TsCpCanMsg atsTrmFrame[4];
_TsCpPort tsCanPort1, tsCanPort2;
_U16 uwTrmCntT;
_U16 uwRcvCntT;
_U32 ulFrameCntT;
char szBufferT[64];
signal(SIGINT, quit);
//-------------------------------------------------
//
//
printf("Setup CAN interface 1 to 500 kBit/sec ... ");
if( CpUserDriverInit( CP_CHANNEL_1, 10, 10, 10, &tsCanPort1) != CpErr_OK)
{
printf("failed!\n");
exit(0);
}
if(CpUserBaudrate(&tsCanPort1, CP_BAUD_500K) != CpErr_OK)
{
printf("failed!\n");
exit(0);
}
printf("OK!\n");
atsTrmFrame[0].tuMsgId.uwStd = 0x201;
atsTrmFrame[0].ubMsgDLC = 0x02;
atsTrmFrame[0].ubMsgCtrl = 0x00;
atsTrmFrame[0].tuMsgData.auwWord[0] = 0x2211;
atsTrmFrame[1].tuMsgId.uwStd = 0x322;
atsTrmFrame[1].ubMsgDLC = 0x07;
atsTrmFrame[1].ubMsgCtrl = 0x00;
uwTrmCntT = 2;
CpUserMsgWrite(&tsCanPort1, &atsTrmFrame[0], &uwTrmCntT);
wait(100);
uwTrmCntT = 1;
CpUserMsgWrite(&tsCanPort1, &atsTrmFrame[1], &uwTrmCntT);
uwRcvCntT = 1;
ulFrameCntT = 0;
while(runProg)
{
if( CpUserMsgRead(&tsCanPort1, &atsRcvFrame[0], &uwRcvCntT) == CpErr_OK)
{
ulFrameCntT++;
can_print_frame(&atsRcvFrame[0], &szBufferT[0]);
printf("%06d - %s\n", ulFrameCntT, &szBufferT[0] );
}
}
CpUserDriverRelease(&tsCanPort1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -