217 lines
3.0 KiB
C
217 lines
3.0 KiB
C
/*
|
|
* can_api.cpp
|
|
*
|
|
* Created on: 2010-5-31
|
|
* Author: zhl
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifdef EMTRONIX_EM9360
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include "can_api.h"
|
|
#elif defined (_WIN32)
|
|
#include <windows.h>
|
|
|
|
#include "can_api.h"
|
|
#elif defined (linux)
|
|
typedef int CAN_BAUDRATE;
|
|
|
|
typedef struct
|
|
{
|
|
unsigned int accept_code;
|
|
unsigned int accept_mask;
|
|
unsigned char filter_mode;
|
|
} accept_filter;
|
|
|
|
|
|
typedef struct
|
|
{
|
|
int NumISTEvents;
|
|
int NumRxDataFrameInt;
|
|
int NumRxDataFramePutRing;
|
|
int NumTxDataFramePutRing;
|
|
int NumTxDataFrameInt;
|
|
int NumTxSuccessful;
|
|
int NumTxFailed;
|
|
int NumErrorWarningInt;
|
|
int NumErrorWarningLevel;
|
|
int NumErrorPassive;
|
|
int NumErrorBusOff;
|
|
int NumSwitchedToNormalOperation;
|
|
int NumOverrunInt;
|
|
int NumBusErrorInt;
|
|
int NumArbitrationLostInt;
|
|
int MaxNumMsgsInRing;
|
|
int RXErrorCounter;
|
|
int TXErrorCounter;
|
|
int Status;
|
|
} drv_statistics;
|
|
//包含socketcan的头文件
|
|
#include <linux/can.h>
|
|
#include <linux/can/raw.h>
|
|
#endif
|
|
#include <fcntl.h>
|
|
|
|
|
|
int CAN_StartChip( int fd )
|
|
{
|
|
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
unsigned long Arg;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_START_CHIP;
|
|
|
|
rc = ioctl(fd, cmd, &Arg);
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
|
|
return rc;
|
|
|
|
#else
|
|
return 0;
|
|
#endif
|
|
|
|
}
|
|
|
|
int CAN_StopChip( int fd )
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
unsigned long Arg;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_STOP_CHIP;
|
|
rc = ioctl(fd, cmd, &Arg);
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
int CAN_SetBaudRate( int fd , CAN_BAUDRATE Baud )
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
int Index;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_SET_BAUD;
|
|
|
|
Index = (int)Baud;
|
|
rc = ioctl(fd, cmd, &Index);
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
#elif defined(_WIN32)
|
|
//设置波特率
|
|
DCB PortDCB;
|
|
|
|
// Get the default port setting information.
|
|
GetCommState ((HANDLE)fd, &PortDCB);
|
|
PortDCB.BaudRate = Baud;
|
|
SetCommState ((HANDLE)fd, &PortDCB);
|
|
|
|
return 0;
|
|
#elif defined (linux)
|
|
return 0;
|
|
#endif
|
|
|
|
}
|
|
|
|
int CAN_SetGlobalAcceptanceFilter( int fd , accept_filter *AcceptanceFilter )
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_SET_FILTER;
|
|
|
|
rc = ioctl(fd, cmd, AcceptanceFilter);
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
int CAN_SelfTest( int fd , int Mode )
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_SET_SELFTEST;
|
|
|
|
rc = ioctl(fd, cmd, &Mode);
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
|
|
int CAN_GetError( int fd , unsigned int *ErrorPtr)
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_GET_ERRORCODE;
|
|
|
|
rc = ioctl(fd, cmd, ErrorPtr );
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
int CAN_GetCANDriverStatistics( int fd , drv_statistics *DriverStatistics )
|
|
{
|
|
#ifdef EMTRONIX_EM9360
|
|
int rc;
|
|
unsigned int cmd;
|
|
|
|
cmd = EM9X60_CAN_IOCTL_GET_STATISTICS;
|
|
|
|
rc = ioctl(fd, cmd, DriverStatistics );
|
|
if(rc < 0)
|
|
{
|
|
return rc;
|
|
}
|
|
return rc;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|