Can调试工具源码
CANTool通用测试软件是一个专门用来socket can接口进行测试的软件工具,此软件操作简单,容易上手,通过运用此软件可以非常方便的对linux系统的can接口进行测试,从而熟悉can接口的使用方法,并且此为源码,还带有过滤自定义,开启和关闭功能,此源码可以直接运行进行调试。
部分代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "can_control.h"
#include <unistd.h>using namespace std;QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();void SlotRecvData(struct can_frame *frame);void on_ClearDataButton_clicked();private:Ui::MainWindow *ui;Can_Control*pcan0;QString currentCan;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow), pcan0(nullptr), currentCan("can0")
{ui->setupUi(this);setWindowTitle(tr("Can调试工具1.0"));ui->recvTextEdit->setPlaceholderText(tr("帧ID 长度 数据"));ui->sendTextEdit->setText("123 11 22 33 44 55 66 77 88");QStringList canlist = QStringList()<<"can0"<<"can1";ui->CanBox->addItems(canlist);// 使用系统调用函数运行以上命令,也可以自行在终端中运行system(ip_cmd_can0_down);system(ip_cmd_set_can0_params); // 设置参数system(ip_cmd_can0_up); // 开启can0接口pcan0 = new Can_Control(this);pcan0->socket_connect(currentCan.toLatin1().data());connect(pcan0, &Can_Control::SigSendData, this, &MainWindow::SlotRecvData);connect(ui->CanBox, &QComboBox::currentTextChanged, [&](QString txt){if(currentCan != txt){pcan0->SetThreadStatus(true);while(!pcan0->isFinished());currentCan = txt;QString setparam;setparam = QString("ifconfig %1 down").arg(currentCan);system(setparam.toLatin1().data());setparam = QString("ip link set %1 type can bitrate 1000000 triple-sampling on").arg(currentCan);system(setparam.toLatin1().data());setparam = QString("ifconfig %1 up").arg(currentCan);system(setparam.toLatin1().data());pcan0->socket_connect(currentCan.toLatin1().data());pcan0->SetThreadStatus(false);pcan0->start();}});pcan0->start();}MainWindow::~MainWindow()
{pcan0->SetThreadStatus(true);while(!pcan0->isFinished());delete ui;
}void MainWindow::on_pushButton_clicked()
{// Errormsg_Dlg *errordlg=new Errormsg_Dlg();// errordlg->show();QString text = ui->sendTextEdit->toPlainText();QStringList strlist = text.split(' ');struct can_frame frame; //要发送的bufferbool ok;frame.can_id = strlist[0].toUInt(&ok, 16);frame.can_dlc = 8;for(int i = 0; i < 8; i++){frame.data[i] = (uint8_t)strlist[i+1].toUInt(&ok, 16);}//const byte* data, const int count// __u8 send_frame_data[8] = {0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};// memcpy(frame[0].data, send_frame_data, 8);pcan0->send_frame(&frame);//}void MainWindow::SlotRecvData(struct can_frame *frame)
{QString buffer;QByteArray temp((const char*)(frame->data),8);buffer = temp.toHex(' ');QString content = QString("%1 [%2] %3").arg(frame->can_id, 3, 16, QLatin1Char('0')).arg(frame->can_dlc, 2, 10, QLatin1Char('0')).arg(buffer);ui->recvTextEdit->insertPlainText(content+"\n");
}void MainWindow::on_ClearDataButton_clicked()
{ui->recvTextEdit->clear();
}