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. Reference a slot to variable in structure
Forum Updated to NodeBB v4.3 + New Features

Reference a slot to variable in structure

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 296 Views 3 Watching
  • 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.
  • T Offline
    T Offline
    TheCrowKaka
    wrote on last edited by
    #1

    I am trying to use a C Library that has this structure.

    typedef struct
    {
    	void (*init)(void);
    	unsigned int (*open)(void);
    	void (*close)(void);
    	unsigned int (*write)(unsigned int dwMilliseconds, unsigned char* pcBuffer);
    	unsigned int (*read)(unsigned int dwMilliseconds, unsigned char* pcBuffer);
    } DEV_IO;
    
    

    I have this class which is used to access the serial port. Header file

    class UART_dev_io {
    public:
        void UART_init();
        uint32_t UART_open();
        void UART_close();
        int32_t UART_read(uint8_t* buffer, uint32_t size);
        int32_t UART_write(uint8_t* buffer, uint32_t size);
        QString COM_PORT;
        QSerialPort *myport;
    };
    
    CPP File:
    void UART_dev_io::UART_init() {
        // UART initialization code
        myport = nullptr;
    
    }
    
    uint32_t UART_dev_io::UART_open() {
        // UART open code
        if(myport)
            myport->close();
        if(QSerialPortInfo::availablePorts().count()>0)    // serial ports present
        {
            myport = new QSerialPort();
            myport->close();
            myport->setPortName(COM_PORT);
            myport->setReadBufferSize(20);
            myport->setBaudRate(921600);
            myport->setDataBits(QSerialPort::Data8);
            myport->setStopBits(QSerialPort::OneStop);
            myport->setParity(QSerialPort::NoParity);
            myport->setFlowControl(QSerialPort::NoFlowControl);
            if(!myport->isOpen())
                myport->open(QIODevice::ReadWrite);
    
            return myport->isOpen();
        }
        return false;
    }
    
    void UART_dev_io::UART_close() {
        if(myport)
        {
            if(myport->isOpen())
                myport->close();
        }
        // UART close code
    }
    
    int32_t UART_dev_io::UART_read(uint8_t *buffer, uint32_t size) {
        QByteArray readdata = myport->readAll();
        buffer = (uint8_t *) readdata.left(64).data();
        if(readdata.length()>=4)
            return readdata.length();
        else
        // UART read code
            return 0;
    }
    
    int32_t UART_dev_io::UART_write(uint8_t *buffer, uint32_t size) {
        // UART write code
        if(myport->write((const char *)buffer,65)>0)
            return 1;
        else
            return 0;
    }
    

    Now the library which I am trying to interface has the following way shown in Python that creates a reference of actual slots to the reference variables in the structure.

    This is the Python Code
        m_DEV_IO = DEV_IO() // This is the variable for the structure
        m_UART_dev_io = UART_dev_io() // This is the variable defined for the class.
    
    This is how the two are referenced in Python
            m_DEV_IO.init = VOIDFUNCTYPE(m_UART_dev_io.UART_init)
            m_DEV_IO.open = UINTFUNCTYPE(m_UART_dev_io.UART_open)
            m_DEV_IO.close = VOIDFUNCTYPE(m_UART_dev_io.UART_close)
            m_DEV_IO.read = RWFUNCTYPE(m_UART_dev_io.UART_read)
            m_DEV_IO.write = RWFUNCTYPE(m_UART_dev_io.UART_write)
            m_UART_dev_io.COM_PORT = args.option[1]
    
    

    Now, I am unable to understand how to do this referencing in Qt. I am using Qt5.15.2

    A Qt Enthusiastic...

    Pl45m4P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Sorry, I don't have a direct answer, but since you are using Python why not make use of PySide ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Sorry, I don't have a direct answer, but since you are using Python why not make use of PySide ?

        T Offline
        T Offline
        TheCrowKaka
        wrote on last edited by
        #3

        @SGaist Thanks for your response.

        However, I am not using Python. I have the Qt Widgets C++ application.
        The external library that I am using has given use examples in Python and not in C++. Hence I was wondering how I could do that what is done in Python in Qt Cpp.

        After studying the library code, I understand that the library is doing reading and writing operations over different ports using the object defined by the structure, which gives the library to remain independent of the physical port but just interface with the class handling the ports.

        Any help in this referencing would be of great help.

        Thanks.

        A Qt Enthusiastic...

        JonBJ 1 Reply Last reply
        0
        • T TheCrowKaka

          @SGaist Thanks for your response.

          However, I am not using Python. I have the Qt Widgets C++ application.
          The external library that I am using has given use examples in Python and not in C++. Hence I was wondering how I could do that what is done in Python in Qt Cpp.

          After studying the library code, I understand that the library is doing reading and writing operations over different ports using the object defined by the structure, which gives the library to remain independent of the physical port but just interface with the class handling the ports.

          Any help in this referencing would be of great help.

          Thanks.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @TheCrowKaka
          Qt has a Python binding just as much as a C++ one. You can write Qt applications, including widgets, with it. https://doc.qt.io/qtforpython-6/ You might be happier with that if you say you already have Python code.

          1 Reply Last reply
          0
          • T TheCrowKaka

            I am trying to use a C Library that has this structure.

            typedef struct
            {
            	void (*init)(void);
            	unsigned int (*open)(void);
            	void (*close)(void);
            	unsigned int (*write)(unsigned int dwMilliseconds, unsigned char* pcBuffer);
            	unsigned int (*read)(unsigned int dwMilliseconds, unsigned char* pcBuffer);
            } DEV_IO;
            
            

            I have this class which is used to access the serial port. Header file

            class UART_dev_io {
            public:
                void UART_init();
                uint32_t UART_open();
                void UART_close();
                int32_t UART_read(uint8_t* buffer, uint32_t size);
                int32_t UART_write(uint8_t* buffer, uint32_t size);
                QString COM_PORT;
                QSerialPort *myport;
            };
            
            CPP File:
            void UART_dev_io::UART_init() {
                // UART initialization code
                myport = nullptr;
            
            }
            
            uint32_t UART_dev_io::UART_open() {
                // UART open code
                if(myport)
                    myport->close();
                if(QSerialPortInfo::availablePorts().count()>0)    // serial ports present
                {
                    myport = new QSerialPort();
                    myport->close();
                    myport->setPortName(COM_PORT);
                    myport->setReadBufferSize(20);
                    myport->setBaudRate(921600);
                    myport->setDataBits(QSerialPort::Data8);
                    myport->setStopBits(QSerialPort::OneStop);
                    myport->setParity(QSerialPort::NoParity);
                    myport->setFlowControl(QSerialPort::NoFlowControl);
                    if(!myport->isOpen())
                        myport->open(QIODevice::ReadWrite);
            
                    return myport->isOpen();
                }
                return false;
            }
            
            void UART_dev_io::UART_close() {
                if(myport)
                {
                    if(myport->isOpen())
                        myport->close();
                }
                // UART close code
            }
            
            int32_t UART_dev_io::UART_read(uint8_t *buffer, uint32_t size) {
                QByteArray readdata = myport->readAll();
                buffer = (uint8_t *) readdata.left(64).data();
                if(readdata.length()>=4)
                    return readdata.length();
                else
                // UART read code
                    return 0;
            }
            
            int32_t UART_dev_io::UART_write(uint8_t *buffer, uint32_t size) {
                // UART write code
                if(myport->write((const char *)buffer,65)>0)
                    return 1;
                else
                    return 0;
            }
            

            Now the library which I am trying to interface has the following way shown in Python that creates a reference of actual slots to the reference variables in the structure.

            This is the Python Code
                m_DEV_IO = DEV_IO() // This is the variable for the structure
                m_UART_dev_io = UART_dev_io() // This is the variable defined for the class.
            
            This is how the two are referenced in Python
                    m_DEV_IO.init = VOIDFUNCTYPE(m_UART_dev_io.UART_init)
                    m_DEV_IO.open = UINTFUNCTYPE(m_UART_dev_io.UART_open)
                    m_DEV_IO.close = VOIDFUNCTYPE(m_UART_dev_io.UART_close)
                    m_DEV_IO.read = RWFUNCTYPE(m_UART_dev_io.UART_read)
                    m_DEV_IO.write = RWFUNCTYPE(m_UART_dev_io.UART_write)
                    m_UART_dev_io.COM_PORT = args.option[1]
            
            

            Now, I am unable to understand how to do this referencing in Qt. I am using Qt5.15.2

            Pl45m4P Online
            Pl45m4P Online
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @TheCrowKaka

            Don't know if I understand correctly, but you have a C Library and the examples how to use it only use Python code, whereas you want to use it in your Qt C++ app?

                m_DEV_IO.init = VOIDFUNCTYPE(m_UART_dev_io.UART_init)
                m_DEV_IO.open = UINTFUNCTYPE(m_UART_dev_io.UART_open)
                m_DEV_IO.close = VOIDFUNCTYPE(m_UART_dev_io.UART_close)
                m_DEV_IO.read = RWFUNCTYPE(m_UART_dev_io.UART_read)
                m_DEV_IO.write = RWFUNCTYPE(m_UART_dev_io.UART_write)
                m_UART_dev_io.COM_PORT = args.option[1]
            

            Since this is most likely the related to the Python wrapper, you don't need that at all...

            Wild guess: Can't you just simply do something like
            DEV_IO.init()?

            What's the reason to use this C Library?
            I mean, you have your own C++ class and you use QSerialPort in it...


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            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