简体   繁体   English

用于构建读/写包的设计模式

[英]Design-pattern for building reading/writing packages

Let me introduce some initial points: I work with program that is connecting via uart (to the special board) and sending data packages to set/get information about devices in the special board' network. 让我介绍一些初始要点:我使用的程序通过uart连接(到专用板)并发送数据包以设置/获取有关专用板网络中设备的信息。 Package format looks like: 包格式如下:

|Start byte | Device ID | R/W | Parameter| Data | CRC|

So I have several devices and want to generate data packages in the most efficient way. 因此,我有几种设备,并且希望以最有效的方式生成数据包。 My question: is there any appropriate design pattern to generate packages above? 我的问题:上面是否有合适的设计模式来生成软件包? Of course it could be hardcoded, but I try to found more flexible way. 当然可以将其硬编码,但是我尝试找到更灵活的方法。

For writing you could use one of the Creational Bulder patterns. 为了写作,您可以使用一种创新的博尔德模式。 Like Simple or Fluent Builder. 像简单或流利的建设者。 For reading you could use one of the Behavioural Interpreter patterns, like Interpreter or Parser. 为了阅读,您可以使用行为解释器模式之一,例如解释器或解析器。

But your task is utmost simple. 但是您的任务非常简单。 So, I strongly recommend to create a straightforward class and implement the necessary data structures an read/write methods. 因此,我强烈建议创建一个简单的类,并通过读/写方法实现必要的数据结构。 This seems to be the more efficient solution. 这似乎是更有效的解决方案。

If your data structure is more complex, you may use one of the above patterns. 如果您的数据结构更复杂,则可以使用上述模式之一。 But also here, reusing an existing protocol is simpler. 但是在这里,重用现有协议也更简单。

Please consider 请考虑

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

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