📄 canread.c.svn-base
字号:
//****************************************************************************//
// File: canwrite.c //
// Description: Test program for writing CAN messages to a socket //
// 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 //
// ---------- -------------------------------------------------------------- //
// 02.01.2004 Initial version //
// //
//****************************************************************************//
//------------------------------------------------------------------------------
// CVS version information:
// $Id: canread.c,v 1.2 2005/07/01 14:23:00 microcontrol Exp $
//------------------------------------------------------------------------------
/*----------------------------------------------------------------------------*\
** Include files **
** **
\*----------------------------------------------------------------------------*/
#include <sys/socket.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <can/af_can.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)
{
int opt;
int sockfd;
int can_port = 0;
char buffer[80];
int msg_count = 0;
struct sockaddr * ptsSockAddrT;
struct sockaddr_can can_sock;
_TsCpCanMsg frame;
signal(SIGINT, quit);
while((opt = getopt(argc, argv, "p:")) != -1)
{
switch(opt)
{
case 'p':
can_port = atoi(optarg);
break;
case '?':
printf("Unknown option: %c\n", optopt);
break;
}
}
sockfd = socket(AF_CAN, SOCK_STREAM, 23);
if(sockfd <0)
{
printf("Can't open socket AF_CAN\n");
exit(0) ;
}
can_sock.can_family = AF_CAN;
can_sock.can_if = can_port;
can_sock.can_id = 0x00FFFFFF;
ptsSockAddrT = (struct sockaddr*) (&can_sock);
if(connect(sockfd, ptsSockAddrT, sizeof(can_sock)) < 0)
{
printf("Can't connect\n");
exit(0);
}
//-------------------------------------------------
//
//
printf("Read CAN messages ... \n");
while(runProg)
{
if( read(sockfd, &frame, sizeof(_TsCpCanMsg)) > 0)
{
msg_count++;
can_print_frame(&frame, &buffer[0]);
printf("%06d - %s\n", msg_count, &buffer[0] );
}
}
close(sockfd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -