Qt and codesys

ray2020
2020-11-12
2022-03-04
  • ray2020 - 2020-11-12

    I am a new learner with Codesys and Qt. I want to make a very simple GUI that will control a stepper motor through codesys. But I could not find any resources about that. can one has any solution ? or reference ? I also read Qt documentation but its not helping. Thank you

     
  • rickj - 2020-11-13

    Here are some youtube tutorials you may find helpful.
    https://www.youtube.com/results?search_query=codesys+visualization+tutorial

     
    • ray2020 - 2020-11-22

      But I want some example or description that worked with Qt C++ framework also.

       
      • Morberis

        Morberis - 2020-11-23

        You'll need to lookup how Qt gets its data and then look for a tutorial that explains that process. I'm not familiar with Qt but some light googling suggests it supports OPC UA and TCP/IP communication.

        I did find some starter info by just googling "Qt and Codesys" including this project which may or may not be helpful. It seems to use Codesys's network variable UDP protocol.

        But if you just want a simple visualization Codesys's built in visualization tools are more than enough. If you're not specifically looking to learn how to use Qt with Codesys that is.

         

        Last edit: Morberis 2020-11-23
  • Morberis

    Morberis - 2020-11-13

    Here are a couple of the Youtube channels that I know about and which might be helpful for you.

    The official Codesys Channel - some good official videos
    Tohid Alizadeh - Really good tutorials. At this point they are quite extensive
    Real Pars - More generic PLC stuff, alot of Siemens. Good for generalized knowledge
    Jesse Cox - More Wago e!Cockpit but some Codesys related videos. Also some of the e!Cockpit stuff can be applied to Codesys
    Kurt Braun - Again more wago stuff but he has some very interesting integration videos. The DINrPlate dinrail mounting options for SBC's that he uses are very nice. Jesse Cox also uses them but I ran across Kurt Braun first.

    There are more videos or channels that you might find if you do a search about a specific subject like recipe management.

     
  • cedric89 - 2020-12-14

    Hi
    it is easy to share variables using shared memory with Qt, i think

     
    πŸ‘
    1
  • cedric89 - 2020-12-16

    do something like that in Qt to read and map the shared memory
    // Setup the shared memory

    ifndef WINDOWS32

    shmid = 0;
    wSize = 256;
    wOffset = 0;
    strncpy(szName, "Mem1", 4);
    shmid = shm_open(szName, O_RDWR, 0777);
    
    #ifndef DEBUG
    // Set up shared memory
    wSize = 256;
    shmidl9 = getshmid();
    if(shmidl9 >= 0L)
        test = (TEST*) mmap(NULL, wSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmidl9, 0);
    

    endif

     
  • rsudecetin - 2022-03-04

    Did you do this? If you can, can you share your files or code?

     
  • mondinmr

    mondinmr - 2022-03-04

    Shared memory is a right way, but...

    In Windows RTE you can use QSharedMemory, you must run application as administrator and use only GLOBAL shared memory. Shared memory should instantiated by OS application and not from CODESYS due visibility problems.

    In Windows not RTE you can use local memory, you can run app as same user of runtime. Shared memory should instantiated by OS application and not from CODESYS due visibility problems.

    In Linux QSharedMemory has some problem with codesys, you can run OS application from any user. Shared memory should instantiated by OS application and not from CODESYS due visibility problems.

    There is a sample of code, we are using in many field applications.
    This work in Windows and Linux.

    # License CLOSED
    # copyright ADV Integration Srl
    
    #ifndef ADVSAHREDMEMORYMANAGER_H
    #define ADVSAHREDMEMORYMANAGER_H
    
    #include "advabstractmemorymanager.h"
    #include "advmemorymanager_global.h"
    #include <QSharedMemory>
    #include <QDebug>
    #ifdef Q_OS_LINUX
    #include <fcntl.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <sys/mman.h>
    #include <unistd.h>
    #endif
    
    template <class T>
    class ADVSahredMemoryManager : public ADVAbstractMemoryManager<T>
    {
    public:
        explicit ADVSahredMemoryManager(const QString &key) : ADVAbstractMemoryManager<T>(), m_key(key), shm(new QSharedMemory) { instantiateMemory(); }
        virtual ~ADVSahredMemoryManager() { freeMemory(); }
    
        bool isValid() {return m_valid;};
    
    protected:
        void instantiateMemory()
        {
            dataPtr = nullptr;
            shm->setNativeKey(m_key);
            if (attach()) {
                qWarning()<<QObject::tr("Shm exists, attached")<<m_key<<"!";
                m_valid = true;
            } else {
                if (create())
                {
    #ifndef Q_OS_LINUX
                    dataPtr = shm->data();
    #endif
                    memset(dataPtr, 0, sizeof(T));
    
                    qWarning()<<QObject::tr("Shm created")<<m_key;
                    m_valid = true;
                } else {
                    qWarning()<<QObject::tr("Cannot create shm")<<m_key<<"!!!";
                    m_valid = false;
                    return;
                }
            }
            ADVAbstractMemoryManager<T>::setDataPointer(reinterpret_cast<T *>(dataPtr));
        }
    
        void freeMemory()
        {
            if (isAttached()) {
                qDebug()<<"Detach"<<m_key;
                detach();
            }
            delete shm;
        }
    
    private:
        QString m_key;
        QSharedMemory *shm;
        void *dataPtr;
    
        bool m_valid;
    
        bool attach() {
    #ifndef Q_OS_LINUX
            if (shm->attach()) {
                dataPtr = shm->data();
                return true;
            }
            return false;
    #endif
    #ifdef Q_OS_LINUX
            int shm_fd = shm_open(shm->nativeKey().toUtf8().toStdString().c_str(), O_RDWR, 0666);
            if (shm_fd >= 0) {
                dataPtr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
                if (dataPtr)
                    return true;
            }
            return false;
    #endif
        }
        void detach() {
    #ifndef Q_OS_LINUX
            shm->detach();
    #endif
    #ifdef Q_OS_LINUX
            shm_unlink(shm->nativeKey().toUtf8().toStdString().c_str());
    #endif
        }
        bool create() {
    #ifndef Q_OS_LINUX
            return shm->create(sizeof(T));
    #endif
    #ifdef Q_OS_LINUX
            int shm_fd = shm_open(shm->nativeKey().toUtf8().toStdString().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
            if (shm_fd >= 0) {
                int ret = ftruncate(shm_fd, sizeof(T));
                if (ret == 0)
                    dataPtr = mmap(nullptr, sizeof(T), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
                else {
                    qDebug()<<"Error code ftruncate : "<<errno;
                    detach();
                }
                if (dataPtr)
                    return true;
                else
                    detach();
            }
            return false;
    #endif
        }
        bool isAttached() {
    #ifndef Q_OS_LINUX
            return shm->isAttached();
    #endif
    #ifdef Q_OS_LINUX
            return dataPtr != nullptr;
    #endif
        }
    };
    
    #endif // ADVSAHREDMEMORYMANAGER_H
    
     
    • sgronchi - 2022-03-04

      If I'd were in your footsteps, I'd implemented a socket interface instead of a shared memory one.

       
      • rsudecetin - 2022-03-04

        OK, thank you :)

         
  • rsudecetin - 2022-03-04

    include "advabstractmemorymanager.h"

    include "advmemorymanager_global.h"

    These are not found. I can't compile them.

     
    • mondinmr

      mondinmr - 2022-03-04

      It's only an Example!
      You can extract the code you need.
      Included parts are only to integrate shm in our framework.

      Look at private methods. You can take that methods and modify a little bit.

       
      • rsudecetin - 2022-03-04

        Ok thank you so much.

         

Log in to post a comment.