I want to write communication interface via serial port. (using Qt)
I have QSerialPort class, which provides interface to work with serial port.
I inherited QMySerialPort from QSerialPort class, which added some functionality and dialog window to config port.
Now I want to write 2 protocol classes:
First – low layer class, sendPacket, parseData methods and etc..
Second – high layer: setDeviceID, getDeviceID, onPayload, etc..
How can I do it?
The first thing that comes to mind is create some QSerialInterface meta-class, which will include the objects of QMySerialPort and 2 protocol layers classes.
And I should implement some connection between them.
Such as:
Protocol class:
void setPort(QSerialPort *port) { m_port = port; }
sendPacket method:
m_port->send(local_data);
But I think this approach is wretched.
Do you have any ideas?
Maybe I should use design patterns here?
Maybe you should narrow down the scope of your question to get a more useful answer. That said, there are a couple of things I’d like to point out.
QMySerialPort.QSerialPort, keep in mind that you can useQSerialPortthrough composition instead of inheritance, and in many cases (if not most) the former is preferable.setPortmethod is OK, although I personally would passportas parameter to the constructor instead.