简体   繁体   English

Qt编程:串口通信模块/插件

[英]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. 我正在使用Qt构建一个主要基于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. 我的应用程序必须通信许多串口设备,如打印机,IC卡读卡器。

  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? 我是否需要编写Qt插件/ webkit插件,或者其他一些方法来执行此操作? Any suggestions are welcome! 欢迎任何建议!

Thanks 谢谢

AFAIK Qt already provides a plugin mechanism. AFAIK Qt已经提供了一个插件机制。

Check the QLibrary class out and the examples there. 检查QLibrary课程和那里的例子。

对于串口部分qextserialport

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

I would suggest one of the PluginManager style plugin methods with C++. 我会建议使用C ++的PluginManager样式插件方法之一。

I'm writing this from 2+ year old memory so it's meant only as a loose guide, not a definitive answer. 我是用2岁以上的记忆写的,所以这只是一个松散的指导,而不是一个明确的答案。

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. 它与我们提供的40多个插件配合得很好。

A search for [DLL plugin C++ class] should find several of the sites for you if you don't like the one I linked. 如果您不喜欢我链接的网站,搜索[DLL插件C ++类]应该会找到几个网站。

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): 创建一个纯虚基类(作为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). 在每个插件中,基于上面的类实现接口,以及使用插件管理器注册/取消注册DLL的方法(请参阅下面的链接)。

Each plugin should go in a separate DLL/SO. 每个插件都应该放在一个单独的DLL / SO中。

See this site for a complete example. 有关完整示例,请参阅此站点

Hope this helps. 希望这可以帮助。 :) :)

What you want is to create a Qt Plugin for your application: 你想要的是为你的应用程序创建一个Qt插件:

http://doc.qt.io/archives/qt-4.7/plugins-howto.html 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. 您需要添加到应用程序中的唯一事情是加载插件的过程并添加一些事件来调用插件方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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