串行通信设计模式
我目前的任务是开发一个与步进电机控制器通信的软件模块。该项目是用 C# 编写的,我有一个 C++ dll 来与控制器通信。通信通过串行端口运行。我计划通过 DllImport 导入必要的方法,用 C# 编写整篇文章。关键方法看起来像:
ComSendReceive(pHandle, bufferIn,sizeBufferIn,bufferOut,ref bufferOut)
有几种类型的消息:
- 您发送消息并期望确认(每条消息都不相同,有时没问题,有时完整等。
- 您发送消息并接收消息 - 您可能会收到错误或数据(例如 GET_CONTROLLER_ID)
- 几种其他类型
当然我需要控制超时的通信
我的问题是:是否有任何“设计模式”可用于此类问题?许多开发人员已经解决了常见问题。
贡献一点 - 我在上一份工作中处理了类似的问题,并通过这种方式解决了它:
我有一个与 Com 端口通信的类和一个带有一堆重载构造函数的类 AT_message :
class AT_Message
{
public bool DoResponseCheck;
public string ExpectedResponse;
public AT_COMMAND command;
public string data;
public bool AddCarriageReturn;
...
//Plenty of ctors
}
class UnfriendlyInterface
{
Response SendMessage(AT_Message msg)
{
//Communicates directly with C++ dll, send message, check timeouts etc....
}
}
并且我有一个与主应用程序通信的类,它有人类友好的方法,
class FriendlyInterface
{
bool AutodetectPortAndOpenComm();
Result AnalyzeSignal(byte[] buffer)
{
Response response = UnfriendlyInterface.SendMessage(new Message(AT_Command.PrepareForSignal, (doResponseCheck)true, ExpectedResponse.Ok,Timeout.short);
Response response = UnfriendlyInterface.SendMessage(new Message(buffer,(doResponseCheck)false,Timeout.long);
//.... Other steps
}
//... other methods
}
因为上次我真的很着急,我实现了我想到的第一个解决方案,但现在有没有办法做得更好?我正在通信的设备比以前的设备更复杂,因此如果有一种方法可以做得更好,我想这样做。
I'm currently assigned to a task to develop a software module to communicate with a stepper motor controller. The project is written in C#, I have a C++ dll to communicate with the controller. The communication runs via the Serial port. I'm planning to write the whole piece in C# by importing the necessary methods by DllImport. The key method looks something like :
ComSendReceive(pHandle, bufferIn,sizeBufferIn,bufferOut,ref bufferOut)
There are several types of messages :
- You send message and expect confirmation (not the same for every message, sometimes it's OK, sometimes it's COMPLETE etc..
- You send message and receive message - you can receive an error or data (for instance GET_CONTROLLER_ID)
- Several other types
Of course I need to control the communication for time-outs.
My question is: Is there any "design pattern" to use for that kind of problem? I'm sure this is quite a common problem many developers have solved already.
To contribute a little - I dealt with similar problem in my last job and I solved it this way :
I had a class to communicate with the Com port and a class AT_message with bunch of overloaded constructors :
class AT_Message
{
public bool DoResponseCheck;
public string ExpectedResponse;
public AT_COMMAND command;
public string data;
public bool AddCarriageReturn;
...
//Plenty of ctors
}
class UnfriendlyInterface
{
Response SendMessage(AT_Message msg)
{
//Communicates directly with C++ dll, send message, check timeouts etc....
}
}
And I had a class the main application was communicating with, it had human friendly methods like
class FriendlyInterface
{
bool AutodetectPortAndOpenComm();
Result AnalyzeSignal(byte[] buffer)
{
Response response = UnfriendlyInterface.SendMessage(new Message(AT_Command.PrepareForSignal, (doResponseCheck)true, ExpectedResponse.Ok,Timeout.short);
Response response = UnfriendlyInterface.SendMessage(new Message(buffer,(doResponseCheck)false,Timeout.long);
//.... Other steps
}
//... other methods
}
Since last time I was really in a big hurry, I implemented first solution that came to my mind. But is there a way to do it better? Now the device I'm communicate with is more complex than the previous one so if there's a way how to do it better, I'd like to do it that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来就像教科书上的外观模式。所有这一切的答案是封装您的变体。例如,尝试为给出确认的命令创建一个通用接口,并编写客户端代码来使用该接口。然后具体类型可以决定如何将各种确认解释为统一信号(Ok = Complete = Good,或其他)
这是一篇关于外观模式。另请参阅维基百科文章。
This seems like a textbook facade pattern. The answer to all of this is to encapsulate your variation. For example, try to create a generic interface for commands that give an acknowledgement, and write client code to use that interface. Then concrete types can decide how to interpret various acknowledgements into a uniform signal (Ok = Complete = Good, or whatever)
Here's a good article on the facade pattern. Also see the wikipedia article.