commonToolkits_global.h?
#ifndef COMMONTOOLKITS_GLOBAL_H
#define COMMONTOOLKITS_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(COMMONTOOLKITS_LIBRARY)
# define COMMONTOOLKITS_EXPORT Q_DECL_EXPORT
#else
# define COMMONTOOLKITS_EXPORT Q_DECL_IMPORT
#endif
#endif // COMMONTOOLKITS_GLOBAL_H
cansocketlinux.h
#ifndef _CANSOCKET_H
#define _CANSOCKET_H
// The order of the following includes is mandatory, because some
// distributions use sa_family_t in can.h without including socket.h
#include <sys/socket.h>
#include <sys/uio.h>
#include <linux/can.h>
#include <sys/time.h>
#include "commonToolkits_global.h"
#include <string>
#include <vector>
struct CanFrame
{
uint32_t frameId; // 帧id
uint8_t dataLen{0}; // 帧数据包长度
uint8_t data[8]{0}; // 存放can帧的数据包缓冲区
};
class COMMONTOOLKITS_EXPORT CanSocket
{
public:
CanSocket(const std::string &name, bool bInit = false);
~CanSocket();
void initCan(); // 初始化can,主要是设置波特率
bool open(); // 打开can
void close(); // 关闭can
bool writeFrame(const CanFrame& frame); // 发送can数据到can总线
std::vector<CanFrame> readSocket(); // 获取CAN总线的数据包
public:
bool getState() {
if(canSocket == -1) {
return false;
}
return true;
}
private:
bool connectSocket();
sockaddr_can m_address;
char m_ctrlmsg[CMSG_SPACE(sizeof(timeval)) + CMSG_SPACE(sizeof(__u32))];
int canSocket = -1;
std::string canSocketName;
bool canFdOptionEnabled = false;
std::string errMsg;
};
cansocketlinux.cpp?
#include "cansocketlinux.h"
#include "memory.h"
#include "stddef.h"
#include <string>
#include "qglobal.h"
#include <linux/can/raw.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include<linux/sockios.h>
const std::string CAN0_OPEN = "ifconfig can0 up";
const std::string CAN0_CLOSE = "ifconfig can0 down";
const std::string CAN0_CONFIG = "ip link set can0 type can bitrate 250000 triple-sampling on";
const std::string CAN1_OPEN = "ifconfig can1 up";
const std::string CAN1_CLOSE = "ifconfig can1 down";
const std::string CAN1_CONFIG = "ip link set can1 type can bitrate 250000 triple-sampling on";
void CanSocket::initCan()
{
system(CAN0_CONFIG.data());
system(CAN0_OPEN.data());
system(CAN1_CONFIG.data());
system(CAN1_OPEN.data());
}
CanSocket::CanSocket(const std::string &name, bool bInit = false):
canSocketName(name)
{
if(bInit)
{
initCan();
}
}
CanSocket::~CanSocket()
{
close();
}
bool CanSocket::open()
{
if (canSocket == -1) {
if (!connectSocket()) {
close(); // sets UnconnectedState
return false;
}
}
return true;
}
void CanSocket::close()
{
::close(canSocket);
canSocket = -1;
}
bool CanSocket::writeFrame(const CanFrame &frame)
{
if (canSocket== -1)
return false;
if(frame.dataLen > 8) {
return false;
}
canid_t canId = frame.frameId;
canId |= CAN_EFF_FLAG;
int64_t bytesWritten = 0;
can_frame sendframe;
::memset(&sendframe, 0, sizeof(sendframe));
sendframe.can_dlc = frame.dataLen;
sendframe.can_id = canId;
::memcpy(sendframe.data, frame.data, sendframe.can_dlc);
bytesWritten = ::write(canSocket, &sendframe, sizeof(sendframe));
if (bytesWritten < 0) {
return false;
}
return true;
}
std::vector<CanFrame> CanSocket::readSocket()
{
std::vector<CanFrame> newFrames;
for (;;) {
struct can_frame frame;
const int bytesReceived = ::read(canSocket, &frame, sizeof(frame));
if (bytesReceived != sizeof(frame)) {
break;
}
struct timeval timeStamp;
if (ioctl(canSocket, SIOCGSTAMP, &timeStamp) < 0) {
::memset(&timeStamp, 0, sizeof(timeStamp));
}
CanFrame bufferedFrame;
bufferedFrame.frameId = (frame.can_id & CAN_EFF_MASK);
bufferedFrame.dataLen = frame.can_dlc;
if(frame.can_dlc > 8) {
continue;
}
memcpy(bufferedFrame.data, frame.data, frame.can_dlc);
newFrames.push_back(std::move(bufferedFrame));
}
return newFrames;
}
bool CanSocket::connectSocket()
{
struct ifreq interface;
if ((canSocket = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, CAN_RAW)) < 0) {
//errMsg = errno;
return false;
}
int loopback = 0;
setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback));
strcpy(interface.ifr_name, canSocketName.data());
if (ioctl(canSocket, SIOCGIFINDEX, &interface) < 0) {
//errMsg = errno;
return false;
}
m_address.can_addr.tp.rx_id = 0;
m_address.can_addr.tp.tx_id = 0;
m_address.can_family = AF_CAN;
m_address.can_ifindex = interface.ifr_ifindex;
if (bind(canSocket, (struct sockaddr *)(&m_address), sizeof(m_address)) < 0) {
//qDebug("%s", qt_error_string(errno).toLatin1().data());
//errMsg = std::string(qt_error_string(errno).toLocal8Bit().constData());
return false;
}
return true;
}
说明: