Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top