Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. send message with Peak can bus

send message with Peak can bus

Scheduled Pinned Locked Moved Unsolved General and Desktop
can bus
11 Posts 4 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rossano
    25 Oct 2020, 13:51

    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?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 26 Oct 2020, 08:28 last edited by
    #2

    @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.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    2
    • R Offline
      R Offline
      Rossano
      wrote on 26 Oct 2020, 19:21 last edited by
      #3

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

      J P 2 Replies Last reply 27 Oct 2020, 05:20
      0
      • R Rossano
        26 Oct 2020, 19:21

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

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 27 Oct 2020, 05:20 last edited by
        #4

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • R Rossano
          26 Oct 2020, 19:21

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

          P Offline
          P Offline
          Pablo J. Rogina
          wrote on 27 Oct 2020, 13:10 last edited by
          #5

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

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrShawn
            wrote on 27 Oct 2020, 14:43 last edited by
            #6

            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);
            
            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rossano
              wrote on 27 Oct 2020, 19:43 last edited by
              #7

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

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rossano
                wrote on 27 Oct 2020, 19:54 last edited by
                #8

                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;

                P 1 Reply Last reply 27 Oct 2020, 21:45
                0
                • R Offline
                  R Offline
                  Rossano
                  wrote on 27 Oct 2020, 20:06 last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • R Rossano
                    27 Oct 2020, 19:54

                    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;

                    P Offline
                    P Offline
                    Pablo J. Rogina
                    wrote on 27 Oct 2020, 21:45 last edited by
                    #10

                    @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...

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    1
                    • R Offline
                      R Offline
                      Rossano
                      wrote on 27 Oct 2020, 22:45 last edited by
                      #11

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

                      1 Reply Last reply
                      0

                      11/11

                      27 Oct 2020, 22:45

                      • Login

                      • Login or register to search.
                      11 out of 11
                      • First post
                        11/11
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved