C++应用程序和硬件通信
问题主要是OOP设计问题。 我有一个类处理与硬件的连接和通信(比方说通过 USB)- CommClass
。它有几个方法 - connect()
、disconnect()
、read()
、write()
。 应用程序本身几乎没有其他类想要通过 CommClass 与同一硬件进行通信。 问题是——你通常怎么做? 我的想法很少:
- 在父类或主要创建
CommClass
实例中,调用connect()
并传递指向所有类(构造函数)的指针。最后 -disconnect()
。 - 每个类的每个方法都会在需要时在堆栈中创建一个
CommClass
对象。 - 这里的问题是它必须调用connect()
方法才能每次请求 USB 句柄等等... - 使用
CommClass
中的静态方法...
The problem is mostly OOP design problem.
I have a class which handles the connection and communication with hardware (Let's say via USB) - CommClass
. It has few methods - connect()
, disconnect()
, read()
, write()
.
The application itself has few other classes that want to communicate with the same HW trough CommClass.
The question - how you usually do that?
I have few ideas in mind:
- In the parent class or in main create instance of
CommClass
, callconnect()
and pas a pointer to all the classes (constructors). At the end -disconnect()
. - Each method from each class will create an
CommClass
object in the stack when it needs it. - here the problem is that it has to callconnect()
method in order to request a handle to the USB and so on every time... - Use static methods from
CommClass
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望与设备(假设是 USB 设备)建立单一连接,那么拥有
CommClass
或ICommClass
的一个实例是有意义的(如果您愿意的话)拥有更优雅的设计,并可使用由CommClass
实现的接口。您还可以将连接(类或接口)包装到 singleton 中,这样您就可以确保连接仅建立和处理一次。如果您希望在单线程应用程序中一次使用一个连接,那么这种方法效果最佳。在多线程或多连接环境中,您可以尝试使用对象池设计模式。If you expect to have a single connection to the device (let's say a USB device) then it makes sense to have one single instance of your
CommClass
orICommClass
if you want to have a more elegant design and work with an Interface that is implemented by yourCommClass
. You can also wrap the connection (class or interface) into a singleton, that way you can make sure that the connection is made and disposed only once. This works best if you expect to use a single connection at a time in a single threaded application. In a multi threaded or multi connection environment you could try using a object pool design pattern.我认为最好的解决方案很大程度上取决于您的应用程序的要求和通信连接的性质。在最简单的情况下,您在 (1) 中描述的内容可能就足够了。
就我个人而言,我几乎总是将通信放入单独的线程中。它有点复杂,但它可以提供显着的速度优势,并确保当通信中发生奇怪的情况时(例如 USB 电缆被拉动),您的 UI 不会失去响应
我最常使用的设计(RS232 通信)使用与 (1) 中描述的略有不同的内容。我有一个 CComms 类作为我的主应用程序对象的成员,它创建一个线程来运行通信。然后我有一个非常简单的消息系统,类似于 Windows 使用的消息系统,它处理通信线程之间传递数据的所有线程同步以及主要应用。然后,主应用程序有一些简单的函数来从其他类发送通信消息并将响应发送回相关类。
我希望这会有所帮助...
I think the best solution very much depends on the requirements of your application and the nature of your comms connection. In the simplest case, what you describe in (1) will be probably be adequate.
Personally, I nearly always put comms into a separate thread. It's a bit more complicated, but it can give significant speed benefits and ensures that your UI doesn't go unresponsive when something strange happens in the comms (like the USB cable being pulled)
The design I've used most often (RS232 comms) uses a slight variation on what you describe in (1). I have a CComms class as a member of my main application object, which creates a thread to run the comms in. I then have a very simple message system similar to the one Windows uses which handles all the thread synchronisation passing data between the comms thread and the main application. The main application then has a few simple functions to send comms messages from other classes and to send the responses back to the relevant class.
I hope that helps a little...