寻找一种更好的方式表示无符号字符数组

时间:2018-10-15 14:38:07

标签: c++ c++98

我有一堆这样的声明:

unsigned char configurePresetDelivery[] = { 0x7E, 0x01, 0x00, 0x20, 0x38, 0x0B, 0x04, 0x03, 0xF2, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3 };
unsigned char beginPresetDelivery[] = { 0x7E, 0x01, 0x00, 0x20, 0x3C, 0x01, 0x04, 0x2B };
unsigned char configureDirectDelivery[] = { 0x7E, 0x01, 0x00, 0x20, 0x37, 0x02, 0X03, 0XF2, 0xD5 };
...

这些是我通过串行端口发送到设备的命令。

会有更好的方法来表示这些吗?在一个结构或一个类之类的东西中?

我仅限于C ++ 98。

谢谢。

1 个答案:

答案 0 :(得分:2)

如何表示命令将很大程度上取决于程序要发送的命令序列。

如果您的程序是完全通用的,并且需要能够发送字面上的任何可能的字节序列,那么可以使用const unsigned char数组(如果需要更明确一点,可以使用const uint8_t数组) )可能是要走的路。

另一方面,如果您的协议中有一些“规则”,您知道它们永远不会更改或需要任何例外,那么您可以编写代码以包含/强制执行这些规则,而不仅仅是盲目发送原始程序员提供的序列(并希望程序员正确键入所有序列)。

例如,如果您知道串行设备始终要求每个命令都以前缀0x7E, 0x01, 0x00, 0x20开头的事实,则可以通过以下方式减少重复(从而减少打字错误)从序列中删除该前缀,并让发送功能自动在其前面添加前缀,例如:

const unsigned char configurePresetDelivery[] = { 0x38, 0x0B, 0x04, 0x03, 0xF2, 0x40, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3 };
const unsigned char beginPresetDelivery[]     = { 0x3C, 0x01, 0x04, 0x2B };
const unsigned char configureDirectDelivery[] = { 0x37, 0x02, 0X03, 0XF2, 0xD5 };

const unsigned char prefix[] = {0x7e, 0x01, 0x00, 0x20};

void send_prefix_and_command(const unsigned char * cmdWithoutPrefix, int numBytes)
{
   send(prefix, sizeof(prefix));
   send(cmdWithoutPrefix, numBytes);
}

[...]

send_prefix_and_command(configurePresetDelivery, sizeof(configurePresetDelivery));

...,然后(进一步介绍),如果您知道某些命令序列将根据运行时参数而变化,那么您可以创建一个命令,而不是手动编码每个变化:生成器函数为您执行此操作(从而将容易出错的生成步骤封装到单个代码位置中,因此只有一个例程可以维护/调试,而不是许多例程)。例如

// This is easier to do using std::vector, so I will use it
std::vector<unsigned char> generatePresetDataCommand(unsigned char presetID, unsigned short presetValue)
{
   // I'm totally making this up just to show an example
   std::vector<unsigned char> ret;
   ret.push_back(0x66);
   ret.push_back(0x67);
   ret.push_back(presetID);
   ret.push_back((presetValue>>8)&0xFF);  // store high-bits of 16-bit value into a byte
   ret.push_back((presetValue>>0)&0xFF);  // store low-bits of 16-bit value into a byte
   return ret;
}

// Convenience wrapper-function so later code can send a vector with less typing
void send_prefix_and_command(const std::vector<unsigned char> & vec)
{
   send_prefix_and_command(&vec[0], vec.size());
}

[...]

// The payoff -- easy one-liner sending of a command with little chance of getting it wrong
send_prefix_and_command(generatePresetDataCommand(42, 32599));