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

发布于 2024-10-07 10:31:55 字数 336 浏览 0 评论 0原文

首先请让我解释一下我想要做什么:

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

  2. 我的应用程序需要与许多串口设备进行通信,例如打印机、IC卡读卡器。

  3. 这些串口设备具有不同的型号,从而具有不同的通信协议。

  4. 我想将我的应用程序与串口设备通信部分分开,这样我只能更新通信部分而不更新所有应用程序。

我需要编写 Qt 插件/webkit 插件,还是其他方式来执行此操作?欢迎任何建议!

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

嘦怹 2024-10-14 10:31:55

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

检查 QLibrary 类及其示例。

AFAIK Qt already provides a plugin mechanism.

Check the QLibrary class out and the examples there.

稍尽春風 2024-10-14 10:31:55

对于串口部分qextserialport

For the serial port part qextserialport

用心笑 2024-10-14 10:31:55

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

Build your communication part in a dll/dynamic library by using TARGET = lib and CONFIG += dll in another qmake file.

谈下烟灰 2024-10-14 10:31:55

我建议使用 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 中。

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

希望这有帮助。 :)

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. :)

木槿暧夏七纪年 2024-10-14 10:31:55

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文