您如何为嵌入式系统设计串行命令协议? [英] How do you design a serial command protocol for an embedded system?

查看:18
本文介绍了您如何为嵌入式系统设计串行命令协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌入式系统,我通过串行方式与之通信.现在的命令结构设计为交互式操作:它显示提示,接受一些命令,并以人类可读的形式显示结果.

I have an embedded system I'm communicating with over serial. The command structure right now is designed to be operated interactively: it displays a prompt, accepts a few commands, and displays results in a human-readable form.

我正在考虑将其更改为更适合机器使用的格式,这样我就可以通过 MATLAB GUI 与它对话,而不会遇到太多麻烦(现在它在交互式提示和不同的消息长度等方面出现问题).

I'm thinking about changing this to a more machine-usable format, so I can talk to it through a MATLAB GUI without too much trouble (right now it's hiccuping on the interactive prompts and varying message lengths, etc.).

那么是否有文档或标准描述了如何为您的嵌入式系统设计一个好的串行命令协议?

So is there a document or standard somewhere that describes how to design a good serial command protocol for your embedded system?

推荐答案

在编写软件以使用 RS232 控制媒体和显示设备方面,我有一些偏好(和讨厌的东西).根据您的硬件,其中一些可能不适用:

I have some preferences (and pet peeves) from writing software to control media and display devices using RS232. Depending on your hardware, some of these may not apply:

  • 我认为让您的协议对自动化更加友好是个好主意.如果您需要交互式界面(命令行或其他),请单独构建它并让它使用自动化协议.我不会太担心让它成为人类可读的,但这取决于你.

  • I think it's a good idea to make your protocol more friendly for automation. If you need an interactive interface (command line or other), build it separately and have it use the automation protocol. I wouldn't worry too much about making it human readable, but it's up to you.

始终返回响应,即使(尤其是)收到无效命令也是如此.ACK 06 美元和 NAK 15 美元这样简单的东西.如果您希望它更易于阅读,或者将其拼写出来.

Always return a response, even (especially) if you get an invalid command. Something simple like $06 for ACK and $15 for NAK. Or spell it out if you want it to be slightly more human readable.

如果您可以设置任何值,请确保有某种方法可以查询相同的值.如果您有很多值,则可能需要一段时间才能全部查询.考虑使用一个或几个同时返回多个值的元查询.

If you can set any value, make sure there's some way to query that same value. If you have a lot of values, it could take a while to query them all. Consider having one or just a few meta-queries that return several values at once.

如果您有无法设置但很重要的信息(型号、序列号、版本、版权等),请确保可以查询这些信息,而不是仅在启动或重置时显示一次.

If you have information that can't be set, but is important (model no, serial no, version, copyright, etc), make sure these can be queried instead of just displaying them once on startup or reset.

永远不要对有效命令做出错误响应.你会认为这个很明显......

Never respond with an error for a valid command. You'd think this one would be obvious...

说到明显,记录你的硬件支持的串行设置.尤其是如果它将被您以外的任何人使用,并且您不希望他们花费前 30 分钟试图弄清楚他们是否因为串行端口、连接、电缆或他们的软件.并不是说我很苦...

Speaking of obvious, document the serial settings your hardware supports. Especially if it's going to be used by anyone other than you and you don't want them to spend the first 30 minutes trying to figure out if they can't talk to the device because of the serial port, the connections, the cable or their software. Not that I'm bitter...

使用绝对命令而不是切换值.例如,为开机和关机设置单独的命令,而不是发送相同的命令并打开和关闭电源.

Use absolute commands instead of toggle values. For example, have separate commands for Power On and Power Off instead of sending the same command and having the power toggle on and off.

响应应包括有关他们响应的命令的信息.这样,任何程序都不需要记住它要求的最后一件事来处理响应(请参阅下面的额外信用选项).

Responses should include information on the command they are responding to. This way any program doesn't need to remember the last thing it asked for in order to deal with the response (see extra credit option below).

如果您的设备支持待机模式(关闭,但并非真正关闭),请确保在您处于此状态时查询仍然有效.

If your device supports a standby mode (off, but not really off), make sure queries still work while you're in this state.

取决于您对数据完整性的偏执程度:

Depending on how paranoid you are about data completeness:

  • 将您的邮件包裹在信封中.报头可以包括一个起始字符、消息的长度和一个结束字符.以防万一您收到部分或格式错误的消息.也许开始 02 美元,结束 03 美元.

  • Wrap your message in an envelope. The header could include a starting character, the lengeth of the message and a closing character. Just in case you get partial or malformed messages. Maybe $02 for the start and $03 for the end.

如果您真的对消息完整性有疑虑,请添加校验和.不过,它们可能会有点痛苦.

If you're really paranoid about message integrity, include a checksum. They can be a bit of a pain, though.

额外学分:

  • 如果您的硬件设置可以手动更改,则可以将更改发送到串行端口,就像用户查询它一样.例如,您可能不希望用户能够更改公共显示器的输入源.

我希望这会有所帮助.

更新:

我忘记了一些重要的事情.在你认真使用它之前,尤其是在你把它交给别人之前,先在一些琐碎的事情上尝试一下,以确保它按照你期望的方式工作,并且(更重要的是)确保你没有遗漏任何东西.如果您在更大的项目中发现问题,则需要更多的时间和精力来解决问题.

I forgot something important. Before you either use this seriously and especially before you give it out to someone else, try it out on something trivial to make sure it works the way you expect it to and (more importantly) to make sure you haven't left anything out. It will take more time and effort to fix things if you find a problem in the middle of a bigger project.

无论您是在设计命令协议、Web 服务、数据库模式还是类等,这都是一个很好的经验法则.

This is a good rule of thumb whether you're designing a command protocol, a web service, a database schema or a class, etc.

这篇关于您如何为嵌入式系统设计串行命令协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆