How to always read CAN messages sent to Qt application
-
I am working on a project as follows:
Raspberry pi will send CAN messages to PC (these messages have different data). Corresponding to each message that the PC receives, CANalyzer software sends back a response that is also CAN messages corresponding to each CAN message that the Raspberry Pi sends to it.
This is my program:
The data transfer function is called every 20ms
The receive data function is called every 1ms
In the system I use 2 2515 CAN modulesMainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);timer_20ms = new QTimer(this); connect(timer_20ms , SIGNAL(timeout()), this, SLOT(timerExecute_20ms())); timer->start(20); /* 20ms */ timer_1ms = new QTimer(this); connect(timer_1ms , SIGNAL(timeout()), this, SLOT(timerExecute_1ms())); timer->start(1); /* 1ms */
}
/* CAN data transfer function */
void MainWindow::canTrans(QString ID, QByteArray data)
{
QCanBusFrame frame;
bool ok;// init can QString errorString; QCanBusDevice *device = QCanBus::instance()->createDevice( QStringLiteral("socketcan"), QStringLiteral("can0"),&errorString); device->connectDevice(); frame.setFrameId(ID.toInt(&ok,16)); frame.setPayload(data); device->disconnectDevice();
}
/ * CAN message to send to PC /
void MainWindow::timerExecute_20ms()
{
dab_4d2_d1h_transmit(); / CAN message 1 /
dab_4d2_d2h_transmit(); / CAN message 2 /
wait(5); / Wait 5ms /
dab_4d4_d3h_transmit(); / CAN message 3 */
wait(10);
inv_4d6_d5h_transmit();
inv_4d6_d6h_transmit();
wait(5);
inv_4d8_d7h_transmit();
wait(10);
dcdc_4da_e1h_transmit();
dcdc_4da_e2h_transmit();
wait(5);
dcdc_4dc_e3h_transmit();
wait(5);
dcdc_4dc_e4h_transmit();
}
The canTrans function will be called in every CAN messagevoid MainWindow::can_receive()
{
// init can
QString errorString;
QCanBusDevice *device1 = QCanBus::instance()->createDevice(
QStringLiteral("socketcan"), QStringLiteral("can1"),&errorString);
device1->connectDevice();QCanBusFrame frame1; /* Receive */ QCanBusFrame frameReceive1; device1->framesReceived(); if(device1->waitForFramesReceived(-1)) { frameReceive1 = device1->readFrame(); } device1->disconnectDevice();
}
void MainWindow::timerExecute_1ms()
{
can_receive();
}My problem is appearing as follows:
At the time the PC sends a CAN message but the timerExecute_1ms function has not been called, the CAN message cannot be received.
In the picture above raspberry Pi will not receive CAN message 2
So is there any other way to solve this problem?
Maybe I need something that always receives CAN messages and my job just needs to access it to get data?
I appreciate every answer!