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. Do not succeed in reading and writting frames with peak system
Forum Updated to NodeBB v4.3 + New Features

Do not succeed in reading and writting frames with peak system

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 3 Posters 1.8k 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.
  • J Jules31

    Hi all,

    I've bought a PCAN-USB FD system to link my PC to an embedded system. The communication works well between my embedded system and PCAN-VIEW.

    Then, I imported PCANBasic.lib from the PCAN_Basic_API into my Qt Project (Qt 5.12.5 MinGW 64-bit). I copied the PCANBasic.dll in my executable folder. I checked that the plugin is available and that the QcanBusDevice is well connected to the can bus. However, I do not succeed in receiving or sending frames in my Qt Project: no communication with PCAN-VIEW or my embedded system

    Below is my Qt project. Any help would be appreciated.

    #include <windows.h>
    #include <math.h>
    
    #include <QtSerialBus>
    #include <QCanBus>
    #include <QCanBusDevice>
    #include <QCanBusFrame>
    
    #include <QDebug>
    #include <QThread>
    
    can::can()
    {
    
        if (QCanBus::instance()->plugins().contains(QStringLiteral("peakcan"))) {
            // plugin available
            qDebug() << "plugin available";
        }
    
        QString errorString;
    
        const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices(
            QStringLiteral("peakcan"), &errorString);
        if (!errorString.isEmpty())
            qDebug() << errorString;
    
        QCanBusDevice *device = QCanBus::instance()->createDevice(
                    QStringLiteral("peakcan"), QStringLiteral("usb0"), &errorString);
    
        if (!device) {
            // Error handling goes here
            qDebug() << errorString;
        } else {
            device->setConfigurationParameter(5,0);
            device->setConfigurationParameter(4,250000);
            //device->setConfigurationParameter(5,1);
            //device->setConfigurationParameter(6,250000);
            device->connectDevice();
            qDebug()<<device->state(); // ConnectedState
        }
    
        QCanBusFrame frame;
        frame.setFrameId(0x400);
        frame.setFrameType(QCanBusFrame::DataFrame);
        QByteArray payload("AAAA");
        frame.setPayload(payload);
    
        while(1)
        {
        device->writeFrame(frame);
        //Sleep(500);
        }
    
        /*QCanBusFrame frame2;
        frame2.setFrameId(0x500);
        frame2.setFrameType(QCanBusFrame::DataFrame);
    
        while (1)
        {
            if (device->framesAvailable()!=0)
            {
            frame2 = device->readFrame();
            qDebug() << frame2.payload();
            }
    
        }*/
    
    }
    
    
    TEMPLATE = app
    QT += network
    #QT += webkit
    QT += sql
    QT += widgets
    QT += serialbus
    QT += core
    TARGET = proto
    DEPENDPATH += .
    INCLUDEPATH += .
    
    HEADERS += \
        can.h \
    
    SOURCES += \
        can.cpp \
        main.cpp
    
    LIBS += "C:/Users/etudes/Desktop/PCAN_Basic_API/x64/VC_LIB/PCANBasic.lib"
    
    INCLUDEPATH += "C:/Users/etudes/Desktop/PCAN_Basic_API/x64"
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Jules31 said in Do not succeed in reading and writting frames with peak system:

    while(1)
    {
    device->writeFrame(frame);
    //Sleep(500);
    }

    Please do not use endless-loops with Qt!

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

    1 Reply Last reply
    2
    • J Offline
      J Offline
      Jules31
      wrote on last edited by
      #3

      You are right. I've done a hundred tests trying to solve this issue.

      1 Reply Last reply
      0
      • J Jules31

        Hi all,

        I've bought a PCAN-USB FD system to link my PC to an embedded system. The communication works well between my embedded system and PCAN-VIEW.

        Then, I imported PCANBasic.lib from the PCAN_Basic_API into my Qt Project (Qt 5.12.5 MinGW 64-bit). I copied the PCANBasic.dll in my executable folder. I checked that the plugin is available and that the QcanBusDevice is well connected to the can bus. However, I do not succeed in receiving or sending frames in my Qt Project: no communication with PCAN-VIEW or my embedded system

        Below is my Qt project. Any help would be appreciated.

        #include <windows.h>
        #include <math.h>
        
        #include <QtSerialBus>
        #include <QCanBus>
        #include <QCanBusDevice>
        #include <QCanBusFrame>
        
        #include <QDebug>
        #include <QThread>
        
        can::can()
        {
        
            if (QCanBus::instance()->plugins().contains(QStringLiteral("peakcan"))) {
                // plugin available
                qDebug() << "plugin available";
            }
        
            QString errorString;
        
            const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices(
                QStringLiteral("peakcan"), &errorString);
            if (!errorString.isEmpty())
                qDebug() << errorString;
        
            QCanBusDevice *device = QCanBus::instance()->createDevice(
                        QStringLiteral("peakcan"), QStringLiteral("usb0"), &errorString);
        
            if (!device) {
                // Error handling goes here
                qDebug() << errorString;
            } else {
                device->setConfigurationParameter(5,0);
                device->setConfigurationParameter(4,250000);
                //device->setConfigurationParameter(5,1);
                //device->setConfigurationParameter(6,250000);
                device->connectDevice();
                qDebug()<<device->state(); // ConnectedState
            }
        
            QCanBusFrame frame;
            frame.setFrameId(0x400);
            frame.setFrameType(QCanBusFrame::DataFrame);
            QByteArray payload("AAAA");
            frame.setPayload(payload);
        
            while(1)
            {
            device->writeFrame(frame);
            //Sleep(500);
            }
        
            /*QCanBusFrame frame2;
            frame2.setFrameId(0x500);
            frame2.setFrameType(QCanBusFrame::DataFrame);
        
            while (1)
            {
                if (device->framesAvailable()!=0)
                {
                frame2 = device->readFrame();
                qDebug() << frame2.payload();
                }
        
            }*/
        
        }
        
        
        TEMPLATE = app
        QT += network
        #QT += webkit
        QT += sql
        QT += widgets
        QT += serialbus
        QT += core
        TARGET = proto
        DEPENDPATH += .
        INCLUDEPATH += .
        
        HEADERS += \
            can.h \
        
        SOURCES += \
            can.cpp \
            main.cpp
        
        LIBS += "C:/Users/etudes/Desktop/PCAN_Basic_API/x64/VC_LIB/PCANBasic.lib"
        
        INCLUDEPATH += "C:/Users/etudes/Desktop/PCAN_Basic_API/x64"
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Jules31 writeFrame() returns a bool - did you check what you get?
        Also, there are two methods to get error code/string - did you check them?

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

        1 Reply Last reply
        1
        • J Offline
          J Offline
          Jules31
          wrote on last edited by
          #5

          I've done these tests with the following source code:

              while(1)
              {
              qDebug()<<device->writeFrame(frame);
              qDebug()<<device->errorString();
              Sleep(500);
              }
          

          writFrame() returns "true".
          error() returns "QCanBusDevice::NoError".

          aha_1980A 1 Reply Last reply
          0
          • J Jules31

            I've done these tests with the following source code:

                while(1)
                {
                qDebug()<<device->writeFrame(frame);
                qDebug()<<device->errorString();
                Sleep(500);
                }
            

            writFrame() returns "true".
            error() returns "QCanBusDevice::NoError".

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Hi @Jules31,

            please try the CAN example first. You can also start it from Qt Creator.

            Note that for 64 bit Qt you will need a 64 bit pcanbasic.dll.

            As said before, don't use sleep in event driven programs. It will block the event loop and that blocks the correct program execution.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • J Offline
              J Offline
              Jules31
              wrote on last edited by
              #7

              Hi,

              The example does not compile.

              I now use a signal but it still does not word:

              connect(device, &QCanBusDevice::framesReceived, this, &can::onFramesReceived);
              
              void can::onFramesReceived() {
                  qDebug() << "[Frame received] !";
              }
              
              
              jsulmJ aha_1980A 2 Replies Last reply
              0
              • J Jules31

                Hi,

                The example does not compile.

                I now use a signal but it still does not word:

                connect(device, &QCanBusDevice::framesReceived, this, &can::onFramesReceived);
                
                void can::onFramesReceived() {
                    qDebug() << "[Frame received] !";
                }
                
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Jules31 Are you sure the other side is responding?

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

                J 1 Reply Last reply
                0
                • J Jules31

                  Hi,

                  The example does not compile.

                  I now use a signal but it still does not word:

                  connect(device, &QCanBusDevice::framesReceived, this, &can::onFramesReceived);
                  
                  void can::onFramesReceived() {
                      qDebug() << "[Frame received] !";
                  }
                  
                  
                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi @Jules31,

                  The example does not compile.

                  Please post the exact compiler error. The example should compile and run just fine.

                  Also, in your first post you wrote:

                  Then, I imported PCANBasic.lib from the PCAN_Basic_API into my Qt Project

                  That is not needed. The pcanbasic.dll is loaded dynamically at runtime.

                  Qt has to stay free or it will die.

                  J 1 Reply Last reply
                  0
                  • aha_1980A aha_1980

                    Hi @Jules31,

                    The example does not compile.

                    Please post the exact compiler error. The example should compile and run just fine.

                    Also, in your first post you wrote:

                    Then, I imported PCANBasic.lib from the PCAN_Basic_API into my Qt Project

                    That is not needed. The pcanbasic.dll is loaded dynamically at runtime.

                    J Offline
                    J Offline
                    Jules31
                    wrote on last edited by
                    #10

                    @aha_1980

                    I disabled the "ResetCanController" option and I now get the error "Cannot load library pcanbasic".

                    ed88eb9a-b009-473f-9f9c-3d1f958bc440-image.png

                    aha_1980A 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Jules31 Are you sure the other side is responding?

                      J Offline
                      J Offline
                      Jules31
                      wrote on last edited by
                      #11

                      @jsulm
                      Yes, it is responding because I receivre frames with Pcan-View.

                      1 Reply Last reply
                      0
                      • J Jules31

                        @aha_1980

                        I disabled the "ResetCanController" option and I now get the error "Cannot load library pcanbasic".

                        ed88eb9a-b009-473f-9f9c-3d1f958bc440-image.png

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Jules31 said in Do not succeed in reading and writting frames with peak system:

                        I disabled the "ResetCanController" option

                        What is that? Are you using a newer example than the one provided with Qt 5.12?

                        and I now get the error "Cannot load library pcanbasic".

                        As said, check that the DLL has the correct bitness.

                        Regards

                        Qt has to stay free or it will die.

                        J 1 Reply Last reply
                        0
                        • aha_1980A aha_1980

                          @Jules31 said in Do not succeed in reading and writting frames with peak system:

                          I disabled the "ResetCanController" option

                          What is that? Are you using a newer example than the one provided with Qt 5.12?

                          and I now get the error "Cannot load library pcanbasic".

                          As said, check that the DLL has the correct bitness.

                          Regards

                          J Offline
                          J Offline
                          Jules31
                          wrote on last edited by
                          #13

                          @aha_1980
                          That works, I put the correct dll:

                          eb8a4587-9aee-467c-8ece-1fe48eed5c59-image.png

                          73d0ad15-bc3e-41e0-bc87-cdcb05ad5847-image.png

                          However, it still does not work in my Qt Project.

                          J aha_1980A 2 Replies Last reply
                          0
                          • J Jules31

                            @aha_1980
                            That works, I put the correct dll:

                            eb8a4587-9aee-467c-8ece-1fe48eed5c59-image.png

                            73d0ad15-bc3e-41e0-bc87-cdcb05ad5847-image.png

                            However, it still does not work in my Qt Project.

                            J Offline
                            J Offline
                            Jules31
                            wrote on last edited by
                            #14

                            @Jules31

                            Another question: Does the PCAN-Basic_API also works with the PCAN-Ethernet Gateway DR from peak system ?

                            Thanks in advance.

                            1 Reply Last reply
                            0
                            • J Jules31

                              @aha_1980
                              That works, I put the correct dll:

                              eb8a4587-9aee-467c-8ece-1fe48eed5c59-image.png

                              73d0ad15-bc3e-41e0-bc87-cdcb05ad5847-image.png

                              However, it still does not work in my Qt Project.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by aha_1980
                              #15

                              Hi @Jules31,

                              However, it still does not work in my Qt Project.

                              Then please incorporate the fixes we mentioned above and show your modified code.

                              Does the PCAN-Basic_API also works with the PCAN-Ethernet Gateway DR from peak system ?

                              Currently not. Do you have such a device and could provide the needed adoptions to the peakcan plugin?

                              Regards

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                Jules31
                                wrote on last edited by
                                #16

                                Hi,

                                I will incorporate your fixes today.

                                I've just checked on peak-system website and it seems that the access to the CAN channels of a PCAN-Gateway is enabled via the new PCAN-LAN hardware type from PCAN-Basic library:

                                https://www.peak-system.com/PCAN-Basic.239.0.html?&L=1

                                Does anyone has tested to use P-CAN-Gateway with PCAN-Basic library yet ?

                                Thank you

                                1 Reply Last reply
                                0

                                • Login

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