Skip to content

Instantly share code, notes, and snippets.

@modjo756
Created July 7, 2016 07:17
Show Gist options
  • Save modjo756/1ba122be6a9948ffb155ba6601f1e1d8 to your computer and use it in GitHub Desktop.
Save modjo756/1ba122be6a9948ffb155ba6601f1e1d8 to your computer and use it in GitHub Desktop.
#include "qcan.h"
#include <QDebug>
#include <QProcess>
QCan::QCan(QObject *parent) : QObject(parent), m_canDevice(Q_NULLPTR)
{
m_sCan=structCan();
}
QCan::~QCan()
{
if (m_canDevice)
delete m_canDevice;
}
void QCan::connectDevice(QString device,int bitrates,bool loopBack,bool listenMode)
{
//socket can didn't accept the configuration of bitrates, nned to configure it with ipconfig
//before a new config we close the old
canShellClose(device);
if(!canShellOpen(device,bitrates))
{
qDebug()<<"problem during the configuration of device : "<<device;
return;
}
qDebug()<<"configuration of device : "<<device<<"at bitrates : "<<bitrates<<" ok";
m_canDevice=QCanBus::instance()->createDevice("socketcan",device);
if(!m_canDevice)
{
m_canDevice=Q_NULLPTR;
qDebug()<<"connection "<<device<<" error";
}
else
{
qDebug()<<"connection "<<device<<" ok";
connect(m_canDevice, &QCanBusDevice::errorOccurred,
this, &QCan::receiveError);
connect(m_canDevice, &QCanBusDevice::framesReceived,
this, &QCan::checkMessages);
if(device=="can0")
m_sCan.i_channel=0;
else
m_sCan.i_channel=1;
}
m_canDevice->setConfigurationParameter(QCanBusDevice::LoopbackKey,loopBack);
m_canDevice->setConfigurationParameter(QCanBusDevice::ReceiveOwnKey,listenMode);
if (!m_canDevice->connectDevice()) {
delete m_canDevice;
m_canDevice = Q_NULLPTR;
qDebug()<<"Connection error on device : "<<device;
}
else
{
qDebug()<<"connected to "<<device;
}
}
void QCan::disconnectDevice(QString device)
{
if (!m_canDevice)
return;
canShellClose(device);
m_canDevice->disconnectDevice();
delete m_canDevice;
m_canDevice = Q_NULLPTR;
qDebug()<<"Disconnected to device : "<<device;
}
void QCan::checkMessages()
{
if (!m_canDevice)
return;
const QCanBusFrame frame = m_canDevice->readFrame();
if (frame.payload().isEmpty())
return;
const qint8 dataLength = frame.payload().size();
const qint32 id = frame.frameId();
m_sCan.i_dlc=dataLength;
m_sCan.i_id=id;
QByteArray dataCan = frame.payload();
for(int i = 0;i<dataLength;i++)
{
m_sCan.data[i]=dataCan.at(i);
}
m_sCan.i_timesStamp=frame.timeStamp().microSeconds();
QDateTime canTime;
canTime.setTime_t(frame.timeStamp().seconds());
m_sCan.neoTime=canTime.time();
emit data(m_sCan);
QString view;
if (frame.frameType() == QCanBusFrame::ErrorFrame) {
interpretError(frame);
} else {
view += QLatin1String("Id: ");
view += QString::number(id, 16);
view += QLatin1String(" bytes: ");
view += QString::number(dataLength, 10);
view += QLatin1String(" data: ");
view += frame.payload().toHex();
//view += " micro : ";
//view +=QString::number((long)frame.timeStamp().microSeconds());
view += " time : ";
view += canTime.time().toString("hh:mm.ss");
}
if (frame.frameType() == QCanBusFrame::RemoteRequestFrame) {
qDebug()<<"request frame :"<<view;
} else if (frame.frameType() == QCanBusFrame::ErrorFrame) {
qDebug()<<"error frame"<<view;
} else {
//qDebug()<<"frame :"<<view;
}
}
void QCan::interpretError(const QCanBusFrame &frame)
{
if (!m_canDevice)
return;
qDebug()<<"error : "<< m_canDevice->interpretErrorFrame(frame);
}
bool QCan::canShellOpen(QString &device,int rates)
{
QString command1,command2;
command1="ip link set "+device+" type can bitrate "+QString::number(rates);
command2="ifconfig "+device+" up";
qDebug()<<command1<<endl<<command2;
QProcess sh;
//configure can device
sh.start("sh");
sh.write(command1.toStdString().c_str());
sh.closeWriteChannel();
sh.waitForFinished();
QByteArray output = sh.readAll();
qDebug(output);
sh.close();
//mount device
sh.start("sh");
sh.write(command2.toStdString().c_str());
sh.closeWriteChannel();
sh.waitForFinished();
output = sh.readAll();
qDebug(output);
sh.close();
if(output.isEmpty())
return true;
else
return false;
}
bool QCan::canShellClose(QString &device)
{
QString command;
command="ifconfig "+device+" down";
QProcess sh;
//configure can device
sh.start("sh");
sh.write(command.toStdString().c_str());
sh.closeWriteChannel();
sh.waitForFinished();
QByteArray output = sh.readAll();
qDebug(output);
sh.close();
if(output.isEmpty())
return true;
else
return false;
}
void QCan::receiveError(QCanBusDevice::CanBusError error) const
{
switch (error) {
case QCanBusDevice::ReadError:
case QCanBusDevice::WriteError:
case QCanBusDevice::ConnectionError:
case QCanBusDevice::ConfigurationError:
case QCanBusDevice::UnknownError:
qWarning() << m_canDevice->errorString();
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment