Qt programming: serial port communication module/plugin
Firstly please let me explain what I am trying to do:
I am using Qt to build an app mainly based on webkit. This app fetches content from internet and present it to user by traditional web way.
My app has to communicate many serial port devices, such as printer开发者_高级运维, IC card reader.
These serial port devices have different models, so that they have different communication protocol.
I want separate my app with the serial port devices communcating part, so that I can only update the communcation part without updating all the app.
Do I need to write a Qt plugin/webkit plugin, or some other way to do this? Any suggestions are welcome!
Thanks
AFAIK Qt already provides a plugin mechanism.
Check the QLibrary class out and the examples there.
For the serial port part qextserialport
Build your communication part in a dll/dynamic library by using TARGET = lib and CONFIG += dll in another qmake file.
I would suggest one of the PluginManager style plugin methods with C++.
I'm writing this from 2+ year old memory so it's meant only as a loose guide, not a definitive answer.
I have included a link to a site I used to get started on a project like you describe a few years ago. It worked well with the 40+ plugins we had available.
A search for [DLL plugin C++ class] should find several of the sites for you if you don't like the one I linked.
You will have to correct for your environment/compiler/OS etc.
In essence, assume you want the ability to Open, Read, Write and Close the serial ports in your plugins.
Create a pure virtual base class (Acts as something declared as an interface in Java):
/* This is the basic plugin header file that every plugin DLL has to include Use your compilers pragmas/keywords to export the entire class from the DLL In Microsoft land the keywords are _declspec( dllexport ) to export the class from the base DLL and __declspec( dllimport ) to import the class into other code. I'm using the MS keywords here because I don't remember how this is done in other compilers. :) */ #if BUILDING_BASE_PLUGIN /* You're compiling the DLL that exports the Plugin Base #define BASE_DLL_EXPORT declspec( dllexport ) #else /* You're compiling code that uses the plugin base #define BASE_DLL_EXPORT declspec( dllimport ) #endif class DLL_EXPORT SerialPortPluginBase { public: enum SerialPortPluginError{ SUCCESS = 0, ERROR_1, ERROR_2, ERROR_ETC }; virtual SerialPortPluginError Open( /*Parameters*/ ) = 0; virtual SerialPortPluginError Read( /*Parameters*/ ) = 0; virtual SerialPortPluginError Write( /*Parameters*/ ) = 0; virtual SerialPortPluginError Close( /*Parameters*/ ) = 0; static std::string pluginName = "SerialPortPluginBase"; static int version; };
In each plugin, implement the interface based on the above class as well as a method to register/unregister the DLL with a plugin manager (see the link below).
Each plugin should go in a separate DLL/SO.
See this site for a complete example.
Hope this helps. :)
What you want is to create a Qt Plugin for your application:
http://doc.qt.io/archives/qt-4.7/plugins-howto.html
You'll be able to extend your main application through a plugin. The only thing you'll need to add to your application is the process of load plugins and add some events to call plugins methods.
精彩评论