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?

有帮助吗?

解决方案

If message is a bytestring then its length in bits is 8 * len(message).

len returns number of bytes in message (what is sent over the serial port). sys.getsizeof returns the size of Python object (str type) (size in memory):

>>> len(b'a')
1
>>> sys.getsizeof(b'a')
38
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top