0

I'm trying to figure out the bit rate of an attached serial device. I've configured the device to send binary data at a rate of 10Hz. My plan was to look at one message from the device, count the number of bits, and then multiply by 10 to get bps.

Here is an example of how I count bits in python, using a fragment of one message:

>> message = "\0xdf\0xd0sA_\0xff0D\0x02\0xef0B"
>> 8 * sys.getsizeof(message)
512

And finally my question: should I be counting the hex prefixes "\0x" ? Maybe I should convert the message to a binary representation and then see how long it is?

4

1 回答 1

1

如果message是一个字节串,那么它的比特长度是8 * len(message).

len返回字节数message(通过串行端口发送的内容)。sys.getsizeof返回 Python 对象(str类型)的大小(内存大小):

>>> len(b'a')
1
>>> sys.getsizeof(b'a')
38
于 2014-03-12T01:46:55.203 回答