繁体   English   中英

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

[英]Qt programming: serial port communication module/plugin

首先请让我解释一下我要做的事情:

  1. 我正在使用Qt构建一个主要基于webkit的应用程序。 这个应用程序从互联网上获取内容,并通过传统的网络方式呈现给用户。

  2. 我的应用程序必须通信许多串口设备,如打印机,IC卡读卡器。

  3. 这些串口设备具有不同的型号,因此它们具有不同的通信协议。

  4. 我希望我的应用程序与串口设备通信部分分开,这样我只能在不更新所有应用程序的情况下更新通信部分。

我是否需要编写Qt插件/ webkit插件,或者其他一些方法来执行此操作? 欢迎任何建议!

谢谢

AFAIK Qt已经提供了一个插件机制。

检查QLibrary课程和那里的例子。

对于串口部分qextserialport

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

我会建议使用C ++的PluginManager样式插件方法之一。

我是用2岁以上的记忆写的,所以这只是一个松散的指导,而不是一个明确的答案。

我已经包含了一个链接 ,该链接指向我曾经在几年前描述的项目开始的网站。 它与我们提供的40多个插件配合得很好。

如果您不喜欢我链接的网站,搜索[DLL插件C ++类]应该会找到几个网站。

您必须更正您的环境/编译器/操作系统等。

实质上,假设您希望能够打开,读取,写入和关闭插件中的串行端口。

创建一个纯虚基类(作为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;
};

在每个插件中,基于上面的类实现接口,以及使用插件管理器注册/取消注册DLL的方法(请参阅下面的链接)。

每个插件都应该放在一个单独的DLL / SO中。

有关完整示例,请参阅此站点

希望这可以帮助。 :)

你想要的是为你的应用程序创建一个Qt插件:

http://doc.qt.io/archives/qt-4.7/plugins-howto.html

您将能够通过插件扩展您的主应用程序。 您需要添加到应用程序中的唯一事情是加载插件的过程并添加一些事件来调用插件方法。

暂无
暂无

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

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