如何保证与外部设备通信的字符位和短路位

发布于 2024-10-08 11:36:40 字数 969 浏览 0 评论 0原文

您好,我正在编写一个库,用于通过 rs-232 串行连接与外部设备进行通信。

我经常需要传达一个包含 8 位 = 1 字节字符或 16 位 = 2 字节数字的命令。我如何以便携的方式做到这一点?

主要问题

从阅读其他问题来看,该标准似乎不保证 1byte = 8bits, (在标准 $1.7/1 中定义)

C中的基本存储单元 + + 内存模型是字节。一个字节至少足够大以包含 基本执行的任何成员 字符集由 连续的位序列, 其中的数量是 实现定义

如何保证char的位数?我的设备需要精确的 8 位,而不是至少 8 位。

我意识到几乎所有实现都有 1byte = 8 位,但我很好奇如何保证它。

Short->2 字节检查

我希望您不介意,我也想针对 Short->2 运行我建议的解决方案。由您进行 2 字节转换。我对字节转换和跨平台可移植性很陌生。

为了保证short的字节数,我想我需要

  1. 执行sizeof(short)。如果 sizeof(short)=2 转换为字节并检查字节顺序(如 此处)

  2. 如果 sizeof(short)>2 则将 Short 转换为字节,检查字节顺序(如 此处),然后检查最高有效字节是否为空并删除它们?

    这是正确的做法吗?有更好的方法吗?

非常感谢

Hello I am writing a library for communicating to an external device via rs-232 serial connection.

Often I have to communicate a command that includes an 8 bit = 1 byte character or a 16 bit = 2 byte number. How do I do this in a portable way?

Main problem

From reading other questions it seems that the standard does not guarantee 1byte = 8bits,
(defined in the Standard $1.7/1)

The fundamental storage unit in the C
+ + memory model is the byte. A byte is at least large enough to contain
any member of the basic execution
character set and is composed of a
contiguous sequence of bits, the
number of which is
implementation-defined.

How can I guarantee the number of bits of char? My device expects 8-bits exactly, rather than at least 8 bits.

I realise that almost all implementations have 1byte = 8 bits but I am curious as to how to guarantee it.

Short->2 byte check

I hope you don't mind, I would also like to run my proposed solution for the short -> 2 byte conversion by you. I am new to byte conversions and cross platform portability.

To guarantee the the number of bytes of the short I guess I am going to need to need to

  1. do a sizeof(short). If sizeof(short)=2 Convert to bytes and check the byte ordering (as here)

  2. if sizeof(short)>2 then convert the short to bytes, check the byte ordering (as here), then check the most significant bytes are empty and remove them?

    Is this the right thing to do? Is there a better way?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ双果 2024-10-15 11:36:40

AFAIK,与串行端口的通信在某种程度上取决于平台/操作系统,因此当您编写其低级部分时,您将非常了解平台、其字节序和 CHAR_BIT。这样一来,这个问题就没有任何意义了。

另外,不要忘记 UART 硬件能够传输 7 或 8 位字,因此它不依赖于系统架构。

编辑:我提到UART的字是固定的(让我们考虑模式3,8位,作为最标准),硬件本身不会发送超过8位,所以通过给它一个< code>send 命令,它将发送正好 8 位,无论机器的 CHAR_BIT 是什么。通过这种方式,通过使用一个 send 来发送 byte

unsigned short i;
send(i); 
send(i>>8);

您可以确定它会做正确的事情。
另外,最好看看 boost.asio 到底在做什么。

AFAIK, communication with the serial port is somehow platform/OS dependent, so when you write the low level part of it, you'll know very well the platform, its endianness and CHAR_BIT. In this way the question does not have any sense.

Also, don't forget that UART hardware is able to transmit 7 or 8 bit words, so it does not depend on the system architecture.

EDIT: I mentioned that the UART's word is fixed (let's consider mode 3 with 8 bits, as the most standard), the hardware itself won't send more than 8 bits, so by giving it one send command, it will send exactly 8 bits, regardless of the machine's CHAR_BIT. In this way, by using one single send for byte and

unsigned short i;
send(i); 
send(i>>8);

you can be sure it will do the right thing.
Also, a good idea would be to see what exactly boost.asio is doing.

瑶笙 2024-10-15 11:36:40

此帖子似乎建议您可以使用 CHAR_BIT代码><限制>。 此页面甚至建议 8 是 char< 中的最小位数/code>...不知道标准中的引用与此有何关系。

对于固定大小的整数类型,如果使用 MSVC2010 或 GCC,您可以依赖 C99 的 (即使在 C++ 中)来定义 (u)int8_t(u)int16_t 保证分别为 8 位和 16 位宽。

This thread seems to suggest you can use CHAR_BIT from <climits>. This page even suggests 8 is the minimum amount of bits in a char... Don't know how the quote from the standard relates to this.

For fixed-size integer types, if using MSVC2010 or GCC, you can rely on C99's <stdint.h> (even in C++) to define (u)int8_t and (u)int16_t which are guaranteed to be exactly 8 and 16 bits wide respectively.

妄断弥空 2024-10-15 11:36:40

标头中的 CHAR_BIT 告诉您字符中的位数。 至少 8。此外,short int 至少使用 16 位来表示其值。这是由最小值范围保证的:

type             can at least represent
---------------------------------------
unsigned char             0...255
signed char            -127...127
unsigned short            0...65535
signed short         -32767...32767
unsigned int              0...65535
signed int           -32767...32767

请参阅此处

关于可移植性,每当我编写以下代码时依赖于 CHAR_BIT==8 我只是这样写:

#include <climits>

#if CHAR_BIT != 8
#error "I expect CHAR_BIT==8"
#endif

正如您所说,这对于几乎所有平台都是如此,如果不是在特定情况下,它将无法编译。这对我来说已经足够便携了。 :-)

CHAR_BIT from the <climits> header tells you the number of bits in a char. This is at least 8. Also, a short int uses at least 16 bits for its value representation. This is guaranteed by the minimum value ranges:

type             can at least represent
---------------------------------------
unsigned char             0...255
signed char            -127...127
unsigned short            0...65535
signed short         -32767...32767
unsigned int              0...65535
signed int           -32767...32767

see here

Regarding portability, whenever I write code that relies on CHAR_BIT==8 I simply write this:

#include <climits>

#if CHAR_BIT != 8
#error "I expect CHAR_BIT==8"
#endif

As you said, this is true for almost all platforms and if it's not in a particular case, it won't compile. That's enough portability for me. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文