Reference a slot to variable in structure
-
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
-
Hi,
Sorry, I don't have a direct answer, but since you are using Python why not make use of PySide ?
-
@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.
-
@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. -
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 useQSerialPort
in it...