简体   繁体   中英

Qt programming: serial port communication module/plugin

Firstly please let me explain what I am trying to do:

  1. 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.

  2. My app has to communicate many serial port devices, such as printer, IC card reader.

  3. These serial port devices have different models, so that they have different communication protocol.

  4. 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.

对于串口部分qextserialport

在另一个qmake文件中使用TARGET = lib和CONFIG + = dll在dll /动态库中构建通信部分。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM