如何为具有特定要求的串行通信创建数据包?

时间:2014-03-05 09:56:15

标签: c++ c serial-port byte bit

我想与串口设备通信。各个分组以连续递增的字节序列发送,即分组的第一个字节是PacCmd,分组的最后一个字节是CheckSum。各个字节是INTEL编码的(Little Endian),即首先发送LSB。 INTEL编码用于数据包的各个数据元素由多个字节(PacCmd,Data)组成的任何地方。

数据包描述如下:

DataPac ::= { PacCmd + DataLen + { DataByte } } + CheckSum
PacCmd - 2 Byte unsigned integer = unsigned short
DataLen - 1 Byte unsigned integer = unsigned char/uint8_t
DataByte - x Byte = Data to send/receive = mixed
CheckSum - 1 Byte unsigned integer = unsigned char/uint8_t

这没问题。但是PacCmd是一个问题。它由3个控制位和一个13位整数值组成,即命令。 PacCmd的描述:

PacCmd ::= ( ReqBit | RspBit | MoreDataBit ) + Cmd
ReqBit - 1 Bit: If set, an acknowledgement is requested from the receiver

RspBit - 1 Bit: If set, this data packet is to be interpreted as an acknowledgement

MoreDataBit - 1 Bit: If set, an additional PacCmd field follows subsequent to the data belonging to this command

Cmd - 13 Bits Unsigned Integer: Identification via the content of the data field of this packet (only if the RspBit is not set)

我的问题是:我如何解释Cmd的价值?如何提取3位和13位整数?如何设置3位和13位整数?有关如何提取信息的任何示例代码?只需使用按位运算符&? Little Endian对此有何看法?

0 个答案:

没有答案
相关问题