New class --> signal and slots
-
Dear all,
This is probably a very stupid question, but I can't get my head around it.
I have a class which is called Canbus. This class is called from within my Mainwindow.cppIn the Canbus class I initialize the canbus settings.
void Canbus::Can_Init() { /*START CAN0*/ QString cmd1 = "ip"; QStringList opt1; /*ip link set can0 up type can bitrate 250000*/ opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000"; proc1.start(cmd1,opt1); QString cmd2 = "ifconfig"; QStringList opt2; /*ifconfig can0 up*/ opt2 <<"can0"<< "up"; proc2.start(cmd2,opt2); QString cmd3 = "candump"; QStringList opt3; opt3 << "can0"; proc3.start(cmd3,opt3); }
Now I have another function in this class which is called dataReady().
I wish to do the following:connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady()));
I used this before when I was working only into the mainwindow.cpp.
If I want to connect those to in a class, how shouldI do this?The following file is my canbus.h file
#ifndef CANBUS_H #define CANBUS_H #include <QtGui> #include <QProcess> #include <QObject> class Canbus { Q_OBJECT public: Canbus(); ~Canbus(); public slots: void dataReady(); private: void Can_Init(); int buttonPushed(void); QProcess proc1; QProcess proc2; QProcess proc3; QProcess proc4; QProcess proc5; QProcess proc6; }; #endif // CANBUS_H
This is my canbus.cpp file
#include "canbus.h" void Canbus::dataReady() { while(proc3.bytesAvailable()){ QString in = proc3.readLine(); QString out; QString DataString[8]; QString CANbus; QString IDstring[8]; QString IDString; long IDdec[8]; QString IDhex; unsigned long ID; QString dataBytes; int dataCAN[8]; QString DataStringL[4]; short dataCANL[4]; QString printOut; QString regString; bool ok; int regNumb; QString DataHEX; QRegExp reg(" can([01]) ([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F]) \\[([0-9]+)\\] ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+) ([0-9A-F]+)"); if(reg.indexIn(in)>=0){ regNumb = 11; for(int n=0;n<8;n++) { regString = "%" + QString::number(regNumb); DataString[n] = regString; DataString[n] = DataString[n].arg(reg.cap(regNumb)); DataHEX = "0x" + DataString[n]; dataCAN[n] = DataHEX.toUInt(&ok, 16); regNumb++; } if((dataCAN[0])==0x15) { printOut = "GO"; for (int n=0;n<8;n++) { qDebug() << DataString[n]; } } else if ((dataCAN[0])==0x15 & (dataCAN[1])==0x16 & (dataCAN[2])==0x1E & (dataCAN[3])==0xCF & (dataCAN[4])==0x00 & (dataCAN[5])==0x09 & (dataCAN[6])==0x71 & (dataCAN[7])==0x00) { } } } } void Canbus::Can_Init() { /*START CAN0*/ QString cmd1 = "ip"; QStringList opt1; /*ip link set can0 up type can bitrate 250000*/ opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000"; proc1.start(cmd1,opt1); QString cmd2 = "ifconfig"; QStringList opt2; /*ifconfig can0 up*/ opt2 <<"can0"<< "up"; proc2.start(cmd2,opt2); QString cmd3 = "candump"; QStringList opt3; opt3 << "can0"; proc3.start(cmd3,opt3); }
So I have no idea on how to connect the signal and the slot in a class. I searched in the documentation of qt but I don't understand it completely.
Thanks in advance,
Kind regards,
Toon Mertens -
@TMJJ did you take a look at the CAN Bus example already? I guess using the QCanBusDevice and QCanBusFrame classes will simplify your development.
You can even look at this webinar from ICS -
@Pablo-J.-Rogina
Thanks for the reply. Yes indeed that would be the best way. The issue is that I'm developping this for an embedded system which only support qt4.8.
So I can't use it.That's why I'm setting this up.
Kind regards
-
@JonB
Yes that I tried as well. I did the following :void Canbus::Can_Init() { /*START CAN0*/ QString cmd1 = "ip"; QStringList opt1; /*ip link set can0 up type can bitrate 250000*/ opt1 << "link"<< "set"<< "can0"<< "up" << "type" << "can" << "bitrate"<< "250000"; proc1.start(cmd1,opt1); QString cmd2 = "ifconfig"; QStringList opt2; /*ifconfig can0 up*/ opt2 <<"can0"<< "up"; proc2.start(cmd2,opt2); QString cmd3 = "candump"; QStringList opt3; opt3 << "can0"; proc3.start(cmd3,opt3); QObject::connect(&proc3,SIGNAL(readyRead()),this, SLOT(dataReady())); }
So I added it to the bottom. Then I get the following error:
/home/whooper/Documents/Tractor/Fendt_CAN_seeder/Fendt415_CAN_Seeder_V1_0/canbus.cpp:93: error: no matching function for call to 'QObject::connect(QProcess*, const char [13], Canbus* const, const char [13])'
I have no clue what I'm doing
-
Canbus must derive from QObject.
And you should do your connect before QProcess::start(). And you should wait for process to finish until proceeding further. -
I hope you are not going to interpret the output of candump in your application to receive CAN frames!
SocketCAN provides a userspace API which can be perfectly used from C/C++ programs.
As SocketCAN is open source, there are lots of examples how to do this, like the SocketCAN plugin of QtSerialBus or the source code of candump.
Regards