更好的方式来构建一个数据包 - 逐字节? [英] A Better Way To Build a Packet - Byte by Byte?

查看:109
本文介绍了更好的方式来构建一个数据包 - 逐字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这关系到我的<一个href=\"http://stackoverflow.com/questions/2014391/compute-arithmetic-sum-to-communicate-with-machine-over-serial\">question今天在这里上SO问道。有没有更好的方式来建立一个数据包发送过来的序列,而不是做这样的:

This is related to my question asked here today on SO. Is there a better way to build a packet to send over serial rather than doing this:

unsigned char buff[255];

buff[0] = 0x02
buff[1] = 0x01
buff[2] = 0x03

WriteFile(.., buff,3, &dwBytesWrite,..);

注意:我有大约二十命令发送,因此,如果有一个更好的方式来发送这些字节串行设备以更简洁的方式,而不必指定每个字节,它会是巨大的。每个字节是十六进制的,与最后一个字节是校验和。我要澄清,我知道我必须指定每个字节打造的命令,但有不必指定各阵列位置更好的办法?

Note: I have about twenty commands to send, so if there was a better way to send these bytes to the serial device in a more concise manner rather than having to specify each byte, it would be great. Each byte is hexadecimal, with the last byte being the checksum. I should clarify that I know I will have to specify each byte to build the commands, but is there a better way than having to specify each array position?

推荐答案

您可以初始化静态缓冲区像这样:

You can initialize static buffers like so:

const unsigned char command[] = {0x13, 0x37, 0xf0, 0x0d};

您甚至可以使用这些初始化非const缓冲区,然后通过索引只更换改变字节。

You could even use these to initialize non-const buffers and then replace only changing bytes by index.

这篇关于更好的方式来构建一个数据包 - 逐字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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