前面转载的几篇文章详细介绍了UART、RS-232和RS-485的相关内容,可以知道,串口通信的双方在硬件层面需要约定如波特率、数据位、校验位和停止位等属性,才可以正常收发数据。实际项目中使用串口通信时,一般还需要设计一套通讯协议。那么设计串口通讯协议有什么需要注意的地方呢?本文以iPod、车载CAN解码盒及IoT设备EnOcean的串口通讯协议为例,对通讯协议数据帧的定义做一个简单的介绍和分析。

  首先看Apple提供的文档《iPod_Accessory_Protocol_Interface_Spec_R38.pdf》,其中有关于设计串口通讯协议的详细说明,具体如下。

The owner or creator of a protocol for communication between accessories and iPhone OS applications must define the preferences that are required or optional. Each accessory and application must then negotiate their level of compatibility directly. Apple does not referee protocols, and protocol creators must ensure that their accessories and applications can verify that they support the same features. The following suggestions can help with protocol design:
■ Ensure that the accessory can always resynchronize its data stream in the event of an error.
■ The round-trip latency of commands may vary; do not draw inferences from propagation times. There is no guarantee of delivery timing over the communication link.
■ After a connection has been established, clearly establish which end speaks first.
■ Begin communication with an exchange of meta-information, such as confirmation of the protocol and version. Give both ends the opportunity to declare that they cannot proceed due to protocol incompatibilities.
■ Ensure that the transaction order is clear and that both ends know their position in it. Packets within each stream are guaranteed to be ordered but not free of gaps between packets, so ensure that the stream can be resynchronized after a gap.
■ Ensure that responses can always be matched to their corresponding requests. The simplest forms of reliable communication are call and response. Interleaved communication streams are more complicated and less desirable.
■ Establish an unambiguous indicator of the start of each packet.
■ Add security where needed, but recognize that it slows down and complicates each transaction.
■ Ensure that the other end of each transaction has both received and understood the last packet.
■ Ensure that each packet is received intact and error free by adding a CRC value, checksum, or the like.
■ Ensure that incoming data can be cached until it can be processed.
■ Try to make the protocol extensible for future needs.

Command Packet Format

Use the small packet format for payloads up to 255 bytes. Use the large packet format for payloads greater than 255 bytes.

Small packet format

For command packets whose payloads are 255 bytes or less, use the small packet format.

Byte number

Value

Meaning

0x00

0xFF

Sync byte

0x01

0x55

Packet start byte

0x02

0xNN

Packet payload length

0x03

0xNN

Lingo ID

0x04

0xNN

Command ID

0x05…0xNN

0xNN

Command data

(last byte)

0xNN

Packet payload checksum

Note that the command ID and command data format for packets with currently unspecified lingoes may not follow the format indicated here (1 byte command ID, 0xN bytes command data). Also note that a packet payload length of 0x00 is not valid for the small packet format; it is reserved as a marker for the large packet format.

Large Packet Format
For command packets whose payloads are between 256 bytes and 65535 bytes in length, use the large packet format.

Byte number

Value

Meaning

0x00

0xFF

Sync byte

0x01

0x55

Packet start byte

0x02

0x00

Packet payload length marker

0x03

0xNN

Packet payload length (bits 15:8)

0x04

0xNN

Packet payload length (bits 7:0)

0x05

0xNN

Lingo ID

0x06

0xNN

Command ID

0x07…0xNN

0xNN

Command data

(last byte)

0xNN

Packet payload checksum

Packet Details
The sync byte (0xFF) is not considered part of the packet. It is sent merely to facilitate automatic baud rate detection and correction when using a UART serial port link and, in some cases, to power on the iPod. It is not necessary to send the sync byte when using BT or USB as a link.
The packet payload length is the number of bytes in the packet, not including the sync byte, packet start byte, packet payload length byte, or packet payload checksum byte. That is, it is the length of the command ID, lingo, and command data. Thus, the packet payload data length for a RequestIdentify command would be 0x02. The Lingo ID specifies the broad category that the communication falls under. The Command ID is a more specific indication of the significance of the packet and is interpreted differently depending on the Lingo ID.
Unless otherwise specified, the following rules apply:
■ All packet data fields larger than 8 bits are sent and received in big-endian format; that is, ordered from the most significant byte to the least significant byte.
■ Device command packets that have a valid checksum but contain an invalid parameter, invalid command, or other such failure cause the iPod to respond with an ACK command containing the appropriate error status.
■ A packet with an invalid checksum received by iPod is presumed to be invalid and is ignored. No ACK or other command is sent to the device in response to the invalid packet.

The sum of all the bytes from the packet payload length (or marker, if applicable) to and including the packet payload checksum is 0x00. The checksum must be calculated appropriately, by adding the bytes together as signed 8-bit values, discarding any signed 8-bit overflow, and then negating the sum to create the signed 8-bit checksum byte. All packets received with a nonzero checksum are presumed to be corrupted and will be discarded.

  CAN解码盒和车载导航之间的通讯协议如下图所示。

  

  EnOcean串口通讯协议数据帧格式定义如下图所示。

  可以看到,三个协议的数据帧格式定义虽然有所不同,但基本遵循帧头、数据帧长度、数据类型、数据及校验和的格式。其中iPod在帧头前面还有一个同步字节0xFF,它实际上并不属于数据帧,是为了自动波特率检测和唤醒iPod用的。通常帧头使用一个字节即可,无需使用两个或多个字节作为帧头。有些数据帧的定义没有使用长度,而是使用特殊值作为帧尾,如GPS的NEMA语句,以回车换行作为帧尾。对于GPS模块输出的信息,这没有问题,但对于复杂串口数据的通信,如此定义的局限非常明显,实际数据内容不能存在帧尾的特殊值,否则即会出错。故除使用Asiic码定义的数据帧以外,最好还是以长度属性来明示数据帧的长度,而不要用帧尾符作为数据帧的结束。数据长度使用一个字节还是两个字节来表示,没有特别限制,主要看数据通信的大小。如果单帧数据量都很大,则用2个字节,否则用1个字节,偶尔需要传一段大数据时,分包发送即可。像iPod定义了大小包,更灵活,但处理时稍微麻烦一点点。对于有效数据部分,通常还会碰到有些协议使用转义字节,以去除与帧头相同的数据。这么做,其实既没有必要,又增加了开销。通过数据长度足以排除有效数据部分的数据被误认为是帧头的可能。为了防止因通信错误导致的程序异常,Checksum还是很有必要加上的,有了Checksum就可以在通信出错时,向对方请求重新发送本帧数据或者直接丢弃本帧数据,而不会导致数据解析异常和程序崩溃。像EnOcean协议,在帧头部分也加了校验,是双保险,更可靠,在某些场合还是很有必要的。原来在做车载导航设备时就曾碰到一个严重Bug,因数据帧头错误,导致通讯异常,且长时间不能恢复,原因就是数据帧长度错误地变为60KB多,而实际数据量只有一百多字节,导致接下来所有数据都被认为是那一帧的数据部分。如果帧头也有校验就能规避这个这个问题了。

  除了数据帧格式的定义,数据流的定义也是通讯协议的重要部分,而这涉及具体的业务逻辑,这里不作具体分析,如有兴趣,可以参考协议文档:

  Apple Accessory protocol:http://files.cnblogs.com/files/we-hjb/iPod_Accessory_Protocol_Interface_Spec_R38.pdf

  http://files.cnblogs.com/files/we-hjb/EnOceanSerialProtocol3-V1.30.pdf

Serial Communication Protocol Design Hints And Reference的更多相关文章

  1. RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)

    前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...

  2. Vehicle’s communication protocol

    http://www.crecorder.com/techInfo/commuProtocols.jsp COMMUNICATION PROTOCOLS A “communication protoc ...

  3. RS-232 vs. TTL Serial Communication(转载)

    RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用U ...

  4. Core Java Volume I — 4.10. Class Design Hints

    4.10. Class Design HintsWithout trying to be comprehensive or tedious, we want to end this chapter w ...

  5. MySQL8.0.16新特性:The Communication Protocol In Group Replication

    MGR优雅升级到MySQL8.0.16 传统的升级手段之一,5.7 MGR集群与8.0 MGR集群进行数据传输,程序切换新集群后测试是否正常. 如果不正常,要么将新集群的新增数据同步回旧集群,要么就舍 ...

  6. DB2 和 有道词典冲突: A communication error has been detected. Communication protocol being used: Reply.fill().

    我在本机安装了DB2 9.5. 使用java jdbc连接,一直没有问题. QC for db2 连接 也一直没有问题. 突然有一天 Java程序连接 报错: A communication erro ...

  7. Serial communication on pins TX/RX

    Serial [Communication] Description Used for communication between the Arduino board and a computer o ...

  8. Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization【转】

    https://developer.valvesoftware.com/wiki/Latency_Compensating_Methods_in_Client/Server_In-game_Proto ...

  9. VPW Communication Protocol

    http://www.fastfieros.com/tech/vpw_communication_protocol.htm Breakdown of the j1850 3 byte Header f ...

随机推荐

  1. mvc实现上传图片(上传和预览)webuploader

    笔者看到mvc最近比较流行,而很多使用一些比较旧的的方法上传图片,再次安利一下百度的webuploader控件吧 webuploader第一步要先下载一些插件这点可以在webuploader官网上下载 ...

  2. 异构(兼容dubbo)SOA系统架构(.net)优化升级

    前面一片文章已经提到我司的异构(兼容dubbo)SOA系统架构,解决了不少技术痛点,也还算比较完善,也顺利推广开来. 但作为项目的开发者,自己产品的问题心里是清楚的,离自己满意还是有不小的距离. 在推 ...

  3. Javaweb——过滤器映射

    什么是过滤器? 过滤器:从字面上看,可以理解为将具有杂质的水过滤,留下干净的水.那么从IT的角度上理解.过滤器:是处在源数据(数据库之类的)和目标数据(显示页面)的中间组件.对于Web应用来说,过滤器 ...

  4. .NET 数据类型转换 方法

    using Newtonsoft.Json;using Newtonsoft.Json.Converters;using System.Web.Script.Serialization; /// &l ...

  5. mvc中form表单提交的几种形式

    第一种方式:submit 按钮 提交 <form action="MyDemand" method="post"> <span>关键字: ...

  6. JS中的函数(二):函数参数(你可能不知道的参数传递)

    前言: 函数分为有参有返回值,有参无返回值,无参无返回值,无参有返回值:那么对于无参数的函数你想使用函数的调用怎么办呢?如果你想封装一个代码,实现多种功能,但是形参大于实参或者实参大于形参又该如何?本 ...

  7. javascript 函数初探 (四)--- 回调函数

    回调函数 既然函数与任何被赋值给变量的数据是相同的,那么她当然可以像其他数据那样被定义.删除.拷贝,以及当成参数传递给其它函数. 我们定义一个函数,这个函数有两个函数类型的参数,然后他会分别执行这两个 ...

  8. 免费薪资总额管控系统-JXHR2016

    •工资总额是指按照国家统计局规定的统计口径或企业规定,在一定时期内支付给各类用工的劳动报酬总额 •工资总额,即基本工资,包括岗位工资.各项津补贴 •JXHR2016以薪酬管控为核心,结合人力资源规划. ...

  9. Linux学习心得之 Linux下命令行Android开发环境的搭建

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Linux学习心得之 Linux下命令行Android开发环境的搭建 1. 前言2. Jav ...

  10. java设计模式--策略模式

    策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以在不影响到客户端的情况下发生变化. 本文地址:http:// ...