Important: Please read the Qt Code of Conduct -

send message with Peak can bus

  • Hello guys,
    i am trying to send a canbus message to a motor using the usb peak device but it doesn't work and I can't figure out where I’am wrong.
    The operating system is Windows 10 with QT5.9.9 and Peak driver 4.2.
    I am attaching the short code that I have implemented:

    #include <QCoreApplication>
    #include <windows.h>
    #include <iostream>
    #include <QCanBus>
    #include <QCanBusDevice>
    #include <QCanBusFrame>
    #include <QByteArray>
    #include <QDebug>
    #include <QString>
    #include <QThread>

    using namespace std;

    int canID;
    bool frameWritten;
    unsigned char ch;
    char *msg;
    unsigned int size = 8;

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    if (QCanBus::instance()->plugins().contains(QStringLiteral("peakcan"))) {
        qDebug() << "plugin available";
    QString errorString;
    QCanBusDevice *device = QCanBus::instance()->createDevice(
        QStringLiteral("peakcan"), QStringLiteral("usb0"), &errorString);
    if (!device) {
        // Error handling goes here
        qDebug() << errorString;

    } else {
    device->setConfigurationParameter(device->BitRateKey, 1000000);

    canID = 0x141;
    QCanBusFrame roxFrame;
    QByteArray payload(size, ch);
    [0] = 0xA4;[1] = 0x00;[2] = 0x90;[3] = 0x01;[4] = 0xFF;[5] = 0xFF;[6] = 0xFF;[7] = 0xFF;
    frameWritten = device->writeFrame(roxFrame);
    cout << "Hello World!" << endl;
    cout << "Rox6Aconsole" << endl;
    cout << "frameWritten = " << frameWritten << endl;
    return a.exec();


    the same message sent with Peak-view works. Can you help me?

  • Lifetime Qt Champion

    @Rossano said in send message with Peak can bus:


    You should check what this returns.
    If it returns false check what tells you.

  • Hello Jsulm, the system compiles regularly and "frameWritten" return true.

  • Lifetime Qt Champion

    @Rossano I'm not talking about compilation. Also how is frameWritten related to device->connectDevice()?
    Please check what device->connectDevice() returns (true or false?). If it returns false check what returns.

  • @Rossano have you tried this example?
    Just to check that your CAN device can be used by Qt

  • does this work?

    #QCanBusFrame roxFrame;
    QByteArray payload;
    payload[0] = 0xA4;
    payload[1] = 0x00;
    payload[2] = 0x90;
    payload[3] = 0x01;
    payload[4] = 0xFF;
    payload[5] = 0xFF;
    payload[6] = 0xFF;
    payload[7] = 0xFF;
    frameWritten = device->writeFrame(canID, payload);

  • Hi jsulm thank you, I tried to do as you advised, and the statement "connected = device-> connectDevice ()" returns true (1)

  • Hi Pablo, I tried to write the array as you indicated (without round brackets), but it gives me compile error -> C: \ Users \ tre-ros \ Documents \ Rox6Aconsole \ main.cpp: 73: error: invalid types' < unresolved overloaded function type> [int] 'for array subscript
    73 | [0] = 0xA4;

  • could it be that in QByteArray some formatting characters need to be deleted? I say this because my array must have eight clean bytes

  • @Rossano said in send message with Peak can bus:

    Hi Pablo, I tried to write the array as you indicated

    Just in case, I suggested to run the Qt CAN example...

  • Thanks Pablo, with "CAN Bus example" it works. Now I have to understand why my code doesn't work

Log in to reply