How to use QThread with CAN bus?
Solved
General and Desktop
-
Hi, i'm using qt5.5 on my computer which based on QT for my OS system.
I wanna do Sending and Receiving together via CanBus.
And this is my code here.void MainWindow::on_pushButton_SenReceive_clicked() { QThread *thread1=new QThread; QThread *thread2=new QThread; connect(thread1,SIGNAL(started()),this,SLOT(cansend())); connect(thread2,SIGNAL(started()),this,SLOT(candump())); thread1->moveToThread(thread1); thread2->moveToThread(thread2); thread1->start(); thread2->start(); } void MainWindow::cansend() { QString info; qDebug("cansend"); Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 stop",&info); qDebug()<<info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 bitrate 50000 ctrlmode triple-sampling on",&info); qDebug()<<info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can0 start",&info); qDebug()<<info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"cansend can0 -i 0x100 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88"); } void MainWindow::candump() { QString info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 stop",&info); qDebug()<<info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 bitrate 50000 ctrlmode triple-sampling on",&info); qDebug()<<info; Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig can1 start",&info); qDebug()<<info; Process::executeProcessSync(QString("sh1"),QStringList()<<"-c"<<"candump can1 &",&info); qDebug("candump4"); ui->textEditPT_Canbus_43->append(info); }
But when i pushed my button it will not show anything out on the textEditPT_Canbus_43.
P.S. the"sh1"is not the wrong commend, i will do some check if it is sh1.
Did i miss anything?
Please help! -
@victor-wang Did you try it like this:
Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<"/usr/sbin/canconfig"<< "can0"<< "stop",&info);
-
@jsulm
Don't you think problem is rather, this:QThread *thread1=new QThread; thread1->moveToThread(thread1);
I'm not sure this even compiles...
-
@victor-wang Why do you move thread1 object to the thread managed by it? It is quite unusual.
Just remove this linethread1->moveToThread(thread1);