Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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->connectDevice();
    device->setConfigurationParameter(device->BitRateKey, 1000000);
    }

    canID = 0x141;
    QCanBusFrame roxFrame;
    roxFrame.setFrameId(canID);
    roxFrame.setFrameType(QCanBusFrame::DataFrame);
    QByteArray payload(size, ch);
    
    
    payload.data()[0] = 0xA4;
    payload.data()[1] = 0x00;
    payload.data()[2] = 0x90;
    payload.data()[3] = 0x01;
    payload.data()[4] = 0xFF;
    payload.data()[5] = 0xFF;
    payload.data()[6] = 0xFF;
    payload.data()[7] = 0xFF;
    
    roxFrame.setPayload(payload);
    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:

    device->connectDevice();

    You should check what this returns.
    If it returns false check what https://doc.qt.io/qt-5/qcanbusdevice.html#errorString 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 https://doc.qt.io/qt-5/qcanbusdevice.html#errorString returns.



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



  • does this work?

    #QCanBusFrame roxFrame;
    #roxFrame.setFrameId(canID);
    #roxFrame.setFrameType(QCanBusFrame::DataFrame);
    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 | payload.data [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